Moh*_*dar 3 shell-script wildcards for
有时我想在许多文件上使用我的脚本。
我使用它的目的是:
for etminput in $1
do
#process
done
Run Code Online (Sandbox Code Playgroud)
但这只是给出了第一个输入。如何对每个通配符匹配进行处理?
如果要循环遍历脚本的所有参数,在任何类似 Bourne 的 shell 中,它是:
for i do
something with "$i"
done
Run Code Online (Sandbox Code Playgroud)
你也可以这样做:
for i in "$@"; do
something with "$i"
done
Run Code Online (Sandbox Code Playgroud)
但它更长而且不便携(尽管适用于现代外壳)。
注意:
for i; do
something with "$i"
done
Run Code Online (Sandbox Code Playgroud)
既不是 Bourne 也不是 POSIX 所以应该避免(尽管它在许多 shell 中都有效)
为了完整起见,在非 Bourne shell 中:
@ i = 1
while ($i <= $#argv)
something with $argv[$i]:q
@ i++
end
Run Code Online (Sandbox Code Playgroud)
您不能使用:
foreach i ($argv:q)
something with $i:q
end
Run Code Online (Sandbox Code Playgroud)
因为这会跳过空参数
for (i) something with $i
Run Code Online (Sandbox Code Playgroud)
(rc
通常是贝壳应该是什么样子)。
for (i=$*) something with $i
Run Code Online (Sandbox Code Playgroud)
(es 是 rc 类固醇)。
for i in $argv
something with $i
end
Run Code Online (Sandbox Code Playgroud)
虽然它会接受 Bourne 语法,但它也支持较短的语法,例如:
for i ("$@") something with "$i"
Run Code Online (Sandbox Code Playgroud)