我有一个复杂的命令,我想做一个shell/bash脚本。我可以很容易地用1美元表示
foo $1 args -o $1.ext
我希望能够向脚本传递多个输入名称。正确的做法是什么?
当然,我想要处理包含空格的文件名。
我有一个复杂的命令,我想做一个shell/bash脚本。我可以很容易地用1美元表示
foo $1 args -o $1.ext
我希望能够向脚本传递多个输入名称。正确的做法是什么?
当然,我想要处理包含空格的文件名。
当前回答
对参数数量变量$#进行循环也可以。
#! /bin/bash
for ((i=1; i<=$#; i++))
do
printf "${!i}\n"
done
./test.sh 1 2 '3 4'
增长:
1
2
3 4
其他回答
在脚本中使用命令格式化任何命令行选项或 参数。
#!/bin/bash
# Extract command line options & values with getopt
#
set -- $(getopt -q ab:cd "$@")
#
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) param="$2"
echo "Found the -b option, with parameter value $param"
shift ;;
-c) echo "Found the -c option" ;;
--) shift
break ;;
*) echo "$1 is not an option";;
esac
shift
使用“$@”来表示所有的参数:
for var in "$@"
do
echo "$var"
done
这将遍历每个参数,并将其打印在单独的行上。$@的行为类似于$*,除了当引用参数时,如果参数中有空格,则会被正确拆分:
sh test.sh 1 2 '3 4'
1
2
3 4
您还可以将它们作为数组元素访问,例如,如果您不想遍历所有这些元素
argc=$#
argv=("$@")
for (( j=0; j<argc; j++ )); do
echo "${argv[j]}"
done
aparse() {
while [[ $# > 0 ]] ; do
case "$1" in
--arg1)
varg1=${2}
shift
;;
--arg2)
varg2=true
;;
esac
shift
done
}
aparse "$@"
对参数数量变量$#进行循环也可以。
#! /bin/bash
for ((i=1; i<=$#; i++))
do
printf "${!i}\n"
done
./test.sh 1 2 '3 4'
增长:
1
2
3 4