Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #158101

Re: bash script globbing

From "Thomas Schmitt" <scdbackup@gmx.net>
Newsgroups linux.debian.user
Subject Re: bash script globbing
Date 2015-09-20 23:20 +0200
Message-ID <qaUbE-39L-13@gated-at.bofh.it> (permalink)
Organization linux.* mail to news gateway

Show all headers | View raw


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

Back to linux.debian.user | Previous | Next | Find similar | Unroll thread


Thread

Re: bash script globbing "Thomas Schmitt" <scdbackup@gmx.net> - 2015-09-20 23:20 +0200

csiph-web