【发布时间】:2013-10-26 20:15:28
【问题描述】:
我正在尝试编写一个 shell 脚本“echo.by”,它会根据用户的选择多次回显其参数。例如,如果用户输入命令行
echo.by 5 Play it again, Sam. <return>
脚本应该打印出来
Play it again, Sam.
Play it again, Sam.
Play it again, Sam.
Play it again, Sam.
Play it again, Sam.
但是,我不知道如何仅打印再次播放,Sam 并排除第一个参数。 $* 命令打印所有内容,所以我最终得到了
5 Play it again, Sam.
5 Play it again, Sam.
5 Play it again, Sam.
5 Play it again, Sam.
5 Play it again, Sam.
我的脚本需要能够容纳第一个数字之后的任何脚本,所以我不能只告诉 shell 回显$2 $3 $4 $5。
这是我的脚本:
count = 0
while test $count -lt $1
do
echo $*
count = `expr $count + 1`
done
【问题讨论】:
-
仅供参考 --
count = 0正在运行程序count,参数为=和0。如果你想做一个作业,你需要省略空格:count=0。此外,((count++))比count=$(( count + 1 ))更容易编写(同样,=周围不允许有空格)。 -
...还有——引号;使用它们。
echo "$*",不是echo $*。如果有人在您的参数中传递了一个带引号的“*”,您不希望它被当前目录中的文件列表替换,对吗?猜猜如果你不引用你的论点会发生什么......