[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash script globbing



Hi,

rlharris@oplink.net wrote:
>     enscript --media=letter -2 --landscape --borders \
>     --header='$n|A.D. $D{%Y.%m.%d}|$* gmt | Page $% of $=' "$1"
> ...
> when I execute
>    enscript+ *
> in a directory containing several files, enscript prints only the first
> file in the directory.

This is because you use only the first argument "$1" in
your script.

The globbing happens before your script gets started.
The shell parser converts your command
  enscript+ *
to a list of arguments
  enscript+ file1 file2 ...
which your script gets to see as
  "$0"      "$1"  "$2"  ...

As i wrote a few days ago, "$@" (with quotes around it) gives
you the list of arguments: "$1" "$2" ...

According to its man page, enscript is willing to take more
than one filename. So may simply write

    enscript --media=letter -2 --landscape --borders \
    --header='$n|A.D. $D{%Y.%m.%d}|$* gmt | Page $% of $=' "$@"

If enscript would only take one filename per run, you
would have to execute it several times in a loop:

    for i in "$@"
    do
      enscript --media=letter -2 --landscape --borders \
               --header='$n|A.D. $D{%Y.%m.%d}|$* gmt | Page $% of $=' "$i"
    done
  
This would also be the way to go if enscript concatenates
all filenames of a run to a single output file whereas
you might want several output files.


Another way of handling an argument list of variable length
is the variable "$#" which gives the number of arguments
and the command "shift" which deletes "$1" and shifts "$2"
to "$1", "$3" to "$2", and so on. It also counts down "$#".

So you always use "$1" and give it new values by "shift":

    while test "$#" -ge "1"
    do
      enscript --media=letter -2 --landscape --borders \
               --header='$n|A.D. $D{%Y.%m.%d}|$* gmt | Page $% of $=' "$1"
      shift 1
    done


Have a nice day :)

Thomas


Reply to: