How to define a shell script with variable number of arguments?

后端 未结 3 558
栀梦
栀梦 2020-12-08 02:22

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, t

3条回答
  •  无人及你
    2020-12-08 03:05

    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.

    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.

提交回复
热议问题