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


Groups > linux.debian.user > #158101 > unrolled thread

Re: bash script globbing

Started by"Thomas Schmitt" <scdbackup@gmx.net>
First post2015-09-20 23:20 +0200
Last post2015-09-20 23:20 +0200
Articles 1 — 1 participant

Back to article view | Back to linux.debian.user


Contents

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

#158101 — Re: bash script globbing

From"Thomas Schmitt" <scdbackup@gmx.net>
Date2015-09-20 23:20 +0200
SubjectRe: bash script globbing
Message-ID<qaUbE-39L-13@gated-at.bofh.it>
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

[toc] | [standalone]


Back to top | Article view | linux.debian.user


csiph-web