Ram*_*n K 80 unix shell loops for-loop cut
假设我有一个Unix shell变量,如下所示
variable=abc,def,ghij
Run Code Online (Sandbox Code Playgroud)
我想使用for循环提取所有值(abc
,def
和ghij
)并将每个值传递给过程.
该脚本应允许从中提取任意数量的逗号分隔值$variable
.
ner*_*ric 110
不搞乱IFS
不调用外部命令
variable=abc,def,ghij
for i in ${variable//,/ }
do
# call your procedure/other scripts here below
echo "$i"
done
Run Code Online (Sandbox Code Playgroud)
使用bash字符串操作http://www.tldp.org/LDP/abs/html/string-manipulation.html
ana*_*ron 100
您可以使用以下脚本动态遍历变量,无论它有多少个字段,只要它只用逗号分隔即可.
variable=abc,def,ghij
for i in $(echo $variable | sed "s/,/ /g")
do
# call your procedure/other scripts here below
echo "$i"
done
Run Code Online (Sandbox Code Playgroud)
而不是echo "$i"
上面的调用,在for循环内部do
和done
内部之间,您可以调用您的过程proc "$i"
.
更新:如果变量的值不包含空格,则上述代码段有效.如果您有这样的要求,请使用其中一个可以更改的解决方案,IFS
然后解析您的变量.
希望这可以帮助.
fed*_*qui 48
如果设置不同的字段分隔符,则可以直接使用for
循环:
IFS=","
for v in $variable
do
# things with "$v" ...
done
Run Code Online (Sandbox Code Playgroud)
您还可以将值存储在数组中,然后按照如何在Bash中的分隔符上拆分字符串中的指示循环它?:
IFS=, read -ra values <<< "$variable"
for v in "${values[@]}"
do
# things with "$v"
done
Run Code Online (Sandbox Code Playgroud)
$ variable="abc,def,ghij"
$ IFS=","
$ for v in $variable
> do
> echo "var is $v"
> done
var is abc
var is def
var is ghij
Run Code Online (Sandbox Code Playgroud)
您可以在此解决方案中找到更广泛的方法,如何迭代逗号分隔列表并为每个条目执行命令.
第二种方法的例子:
$ IFS=, read -ra vals <<< "abc,def,ghij"
$ printf "%s\n" "${vals[@]}"
abc
def
ghij
$ for v in "${vals[@]}"; do echo "$v --"; done
abc --
def --
ghij --
Run Code Online (Sandbox Code Playgroud)
小智 12
我认为从语法上讲,这更干净,并且也通过了 shell 检查 linting
variable=abc,def,ghij
for i in ${variable//,/ }
do
# call your procedure/other scripts here below
echo "$i"
done
Run Code Online (Sandbox Code Playgroud)
#/bin/bash
TESTSTR="abc,def,ghij"
for i in $(echo $TESTSTR | tr ',' '\n')
do
echo $i
done
Run Code Online (Sandbox Code Playgroud)
我更喜欢使用 tr 而不是 sed,因为 sed 在某些情况下会遇到特殊字符(如 \r \n)的问题。
其他解决方案是将 IFS 设置为某个分隔符