Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 用-ash捕捉字符串中特定单词后的单词_String_Shell_Word_Busybox_Ash - Fatal编程技术网

String 用-ash捕捉字符串中特定单词后的单词

String 用-ash捕捉字符串中特定单词后的单词,string,shell,word,busybox,ash,String,Shell,Word,Busybox,Ash,看起来很复杂。我想知道“频道”后面的号码, 没有sed或awk。是否有便携式8-)支架扩展到 只需在“频道”后面加上这个词,类似于: get_channel() { local wifidev="$1" set -- $( iw dev "$wifidev" info ) # e.g. 'Interface wlan0 ifindex 6 wdev 0x2 addr 10:6f:3f:0e:31:8e type IBSS wiphy 0 channel 11 (2462

看起来很复杂。我想知道“频道”后面的号码, 没有sed或awk。是否有便携式8-)支架扩展到 只需在“频道”后面加上这个词,类似于:

get_channel()
{
    local wifidev="$1"

    set -- $( iw dev "$wifidev" info )
    # e.g. 'Interface wlan0 ifindex 6 wdev 0x2 addr 10:6f:3f:0e:31:8e type IBSS wiphy 0 channel 11 (2462 MHz) NO HT'

    case "$@" in
        *" channel "*)
            while [ "$1" != "channel" ]; do shift; done
            echo "$2"
        ;;
        *)
            return 1
        ;;
    esac
}
也许还有更好的办法

编辑:根据建议,我们目前正在使用这种方法:

channel="${@#*channel }"
set -- $channel
echo $1

它足够快和安全(+便携)-但可能还有一个不错的吗?

这似乎没有任何作用:

set -- $( iw dev "$dev" info )
mychan="$@"
mychan="${mychan#*channel }"
case "$mychan" in
    [0-9]*)
        echo "${mychan%% *}"
    ;;
esac
我猜特殊参数$@太特殊了

首先将位置参数存储在tmp变量中

${@#* channel }

那很好。您需要使用第二个大括号扩展,即
channel=${channel%%*}
删除数字后面的剩余部分。祝你好运。你最好在…中使用
case“$*”,因为
“$@”
会扩展成多个单词,并导致case语句出现语法错误。你是对的(虽然在这个阶段,它已经被清理过了,但做得更好并没有坏处)。奇怪的是,这在这里不起作用:
echo“${tmp#*channel}”;echo“${tmp%*}”
=
1(2412 MHz)HT20
1(2412 MHz)
-您可以证明,只需将字符串“Interface…”@BastianBittorf馈送给“tmp”,第二个需要一个双
%
——编辑的answerPOSIX.1-2008tc1将保留使用
${parameter#pattern}
上的
*
@
特殊参数未指定。jilles:这意味着它不可移植/无法保证工作?因此最好使用glenn建议的临时变量?事实上,不同shell之间的行为不同(例如,将替换应用于第一个元素、所有元素或所有元素与第一个字符$IFS的串联)。
local tmp="$*"
tmp=${tmp#* channel }
if [ "$tmp" = "$*" ]; then
    # no "channel" here
    return 1
fi
echo ${tmp%% *}