如何定义与参数可变数量的shell脚本? [英] How to define a shell script with variable number of arguments?

查看:301
本文介绍了如何定义与参数可变数量的shell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过一个shell脚本来定义呼叫的简单缩写为 GS (ghostscript的)。第一个参数(多个)给所有应合并文件,最后一个给出了输出文件的名称。显然,以下不工作(这只是用于显示的目标):

I would like to define a simple abbreviation of a call to gs (ghostscript) via a shell script. The first argument(s) give all the files that should be merged, the last one gives the name of the output file. Obviously, the following does not work (it's just for showing the goal):

#!/bin/sh
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOUTPUTFILE=$last $1 $2 ...

如何才能做到这一点?

How can this be done?

一会通常通过的MyScript infile1.pdf infile2.pdf ... outfile.pdf 的MyScript的* .pdf outfile.pdf调用此脚本

推荐答案

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.

这篇关于如何定义与参数可变数量的shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆