我有一个复杂的命令,我想做一个shell/bash脚本。我可以很容易地用1美元表示
foo $1 args -o $1.ext
我希望能够向脚本传递多个输入名称。正确的做法是什么?
当然,我想要处理包含空格的文件名。
我有一个复杂的命令,我想做一个shell/bash脚本。我可以很容易地用1美元表示
foo $1 args -o $1.ext
我希望能够向脚本传递多个输入名称。正确的做法是什么?
当然,我想要处理包含空格的文件名。
当前回答
对于简单的情况,你也可以使用shift。 它将参数列表视为队列。每次移位都会抛出第一个参数 其余每个参数的索引都递减。
#this prints all arguments
while test $# -gt 0
do
echo "$1"
shift
done
其他回答
对于简单的情况,你也可以使用shift。 它将参数列表视为队列。每次移位都会抛出第一个参数 其余每个参数的索引都递减。
#this prints all arguments
while test $# -gt 0
do
echo "$1"
shift
done
放大baz的回答,如果您需要用索引枚举参数列表(例如搜索特定的单词),您可以在不复制列表或更改列表的情况下完成此操作。
假设你想用双破折号("——")分割一个参数列表,并将破折号之前的参数传递给一个命令,将破折号之后的参数传递给另一个命令:
toolwrapper() {
for i in $(seq 1 $#); do
[[ "${!i}" == "--" ]] && break
done || return $? # returns error status if we don't "break"
echo "dashes at $i"
echo "Before dashes: ${@:1:i-1}"
echo "After dashes: ${@:i+1:$#}"
}
结果应该是这样的:
$ toolwrapper args for first tool -- and these are for the second
dashes at 5
Before dashes: args for first tool
After dashes: and these are for the second
对参数数量变量$#进行循环也可以。
#! /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