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):
我想通过shell脚本定义一个简单的gs(ghostscript)调用缩写。第一个参数给出了应该合并的所有文件,最后一个给出了输出文件的名称。显然,以下不起作用(它只是为了显示目标):
#!/bin/sh
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOUTPUTFILE=$last $1 $2 ...
How can this be done?
如何才能做到这一点?
One would typically call this script via myscript infile1.pdf infile2.pdf ... outfile.pdf
or myscript *.pdf outfile.pdf
.
人们通常会通过myscript infile1.pdf infile2.pdf ... outfile.pdf或myscript * .pdf outfile.pdf调用此脚本。
3 个解决方案
#1
35
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 whitespace.
bash变量$ @和$ *扩展到命令行参数列表。通常,您需要使用“$ @”(即$ @包围双引号)。如果有人向你的脚本传递一个包含空格的参数,这将是正确的。
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"
This would expand to:
这将扩展到:
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.
有关更多信息,请阅读bash手册页的“特殊参数”部分。
#2
18
To pass the output file as the last argument, use an array:
要将输出文件作为最后一个参数传递,请使用数组:
ARGS=("$@")
# Get the last argument
outputfile=${ARGS[-1]}
# Drop it from the array
unset ARGS[${#ARGS[@]}-1]
exec gs ... -sOUTPUTFILE=$outputfile "${ARGS[@]}"
Before version 4, bash
didn't allow negative subscripts in arrays (and produced the error reported by Marius in the comments), so if you're using 3.x you need to use the much uglier
在版本4之前,bash不允许在数组中使用负下标(并且在评论中产生了Marius报告的错误),所以如果你使用3.x,你需要使用更多的uglier
outputfile=${ARGS[${#ARGS[@]}-1]}
This works for bash 4.x as well.
这也适用于bash 4.x.
#3
7
To access the last argument, in addition to Idelic's answer above, you can also do:
要访问最后一个参数,除了上面的Idelic的答案,您还可以:
echo "${@: $#}"
This reads all of the arguments and prints them starting from the last one. This way, you can also access the N last arguments, for example for the last three arguments:
这将读取所有参数并从最后一个开始打印它们。这样,您还可以访问N个最后的参数,例如最后三个参数:
echo "${@: $#-2}"
$ ./script "what does" "this script" "do" "?"
this script do ?
#1
35
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 whitespace.
bash变量$ @和$ *扩展到命令行参数列表。通常,您需要使用“$ @”(即$ @包围双引号)。如果有人向你的脚本传递一个包含空格的参数,这将是正确的。
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"
This would expand to:
这将扩展到:
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.
有关更多信息,请阅读bash手册页的“特殊参数”部分。
#2
18
To pass the output file as the last argument, use an array:
要将输出文件作为最后一个参数传递,请使用数组:
ARGS=("$@")
# Get the last argument
outputfile=${ARGS[-1]}
# Drop it from the array
unset ARGS[${#ARGS[@]}-1]
exec gs ... -sOUTPUTFILE=$outputfile "${ARGS[@]}"
Before version 4, bash
didn't allow negative subscripts in arrays (and produced the error reported by Marius in the comments), so if you're using 3.x you need to use the much uglier
在版本4之前,bash不允许在数组中使用负下标(并且在评论中产生了Marius报告的错误),所以如果你使用3.x,你需要使用更多的uglier
outputfile=${ARGS[${#ARGS[@]}-1]}
This works for bash 4.x as well.
这也适用于bash 4.x.
#3
7
To access the last argument, in addition to Idelic's answer above, you can also do:
要访问最后一个参数,除了上面的Idelic的答案,您还可以:
echo "${@: $#}"
This reads all of the arguments and prints them starting from the last one. This way, you can also access the N last arguments, for example for the last three arguments:
这将读取所有参数并从最后一个开始打印它们。这样,您还可以访问N个最后的参数,例如最后三个参数:
echo "${@: $#-2}"
$ ./script "what does" "this script" "do" "?"
this script do ?