且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何定义与参数可变数量的shell脚本?

更新时间:2023-12-04 22:59:46

bash的变量 $ @ $ * 进军的命令行参数的清单。一般来说,你会希望使用$ @(即 $ @ 用双引号包围)。如果有人通过你的脚本包含参数,这种会做正确的事。

The bash variables $@ and $* expand into the list of command line arguments. Generally, you will want to use "$@" (that is, $@ surrounded by double quotes). This will do the right thing if someone passes your script an argument containing.

所以,如果你在你的脚本是这样:

So if you had this in your script:

outputfile=$1
shift
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOUTPUTFILE=$outputfile "$@"

和你叫你的脚本是这样的:

And you called your script like this:

myscript out.pdf foo.ps bar.ps "another file.ps"

这将扩展为:

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOUTPUTFILE=out.pdf foo.ps bar.ps "another file.ps"

阅读庆典手册页的特殊参数部分以获取更多信息。

Read the "Special Parameters" section of the bash man page for more information.