linux-l: Parsing von Argumenten

Peter J. Weyers weyers at ifh.de
Di Jan 27 11:40:54 CET 1998


On Mon, 26 Jan 1998, Carsten Wartmann wrote:

> Hallo,
> 
> ich würde mein Script gerne noch mit dem Parsing von Argumenten
> versehen. Im Momment ist es noch einfach, weil nur zwei Argument da
> sind, aber ich brauche min. 3 und will auch mal eines, zwei oder drei
> weglassen können.
> 
> Gibts da schon eine Standard-Konstruktion für?

Also ich bastel mir meist etwas, das auf diesem Geruest aufgebaut ist (Als
beispiel nehme ich mal den Anfang eines Scripts, das zu einem Directory
voller Bilder eine html-Seite mit Thumbnail Images macht):

#! /bin/ksh

# could be the name of the script:
PRG_NAME=$0 

# set defaults
HTMLFILE=index.html
DOCTITLE="Picture List"
PAGETITLE="Picture List"
PICFILES=""

while [ "$1" ]
do
 case "$1" in
  -file)      HTMLFILE="$2"
              shift 2
              ;;
  -title)     DOCTITLE="$2"
              PAGETITLE="$2"
              shift 2
              ;;
  -cols)      TABCOLS="$2"
              shift 2
              ;;
  -width)     WIDTH="$2"
              shift 2
              echo -n "$PRG_NAME: Warning: Already existing pictures " >&2
              echo    "won't be remade!" >&2
              ;;
  *_dir.gif) echo "$PRG_NAME: skipping $1"
              shift
              ;;
  -?|-h|-help)echo available options:
              echo " -file  <string>       Filename of HTML Document"
              echo " -title <string>       Title of HTML Document"
              echo " -cols  <number>       Number of columns of HTML
table"
              echo " -width <number>       Width (or height) of pictures"
 
              exit
              ;;
  -*)         echo "$PRG_NAME: unknown option: $1" >&2
              exit
              ;;
  *)          PICFILES="$PICFILES $1"
              shift
              ;;
 esac
done

Ist -glaube ich- flexibler als 'getopt'.

Gruss,

Peter
 ,--------------------------------------------------------------------.
 |Phone:  033762/77-452 , 030/2093-7812  | http://www.ifh.de/~weyers/ |
 `--------------------------------------------------------------------'






Mehr Informationen über die Mailingliste linux-l