如何查找传递给Bash脚本的参数数量?
这就是我目前所拥有的:
#!/bin/bash
i=0
for var in "$@"
do
i=i+1
done
Run Code Online (Sandbox Code Playgroud)
还有其他(更好)的方法吗?
zsa*_*ank 266
参数的数量是 $#
在此页面上搜索以了解更多信息:http: //tldp.org/LDP/abs/html/internalvariables.html#ARGLIST
Pau*_*ce. 96
#!/bin/bash
echo "The number of arguments is: $#"
a=${@}
echo "The total length of all arguments is: ${#a}: "
count=0
for var in "$@"
do
echo "The length of argument '$var' is: ${#var}"
(( count++ ))
(( accum += ${#var} ))
done
echo "The counted number of arguments is: $count"
echo "The accumulated length of all arguments is: $accum"
Run Code Online (Sandbox Code Playgroud)