Bash/Scripting/Parse Args: Unterschied zwischen den Versionen

Aus SchnallIchNet
Wechseln zu: Navigation, Suche
 
(4 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
 +
__FORCETOC__
 +
 +
== Parse with getopts (bash-builtin) ==
 +
 +
<pre>
 +
while getopts "fhm" OPTION; do
 +
  case $OPTION in
 +
      f) FORCE=1 ;;
 +
      h) print_help; exit ;;
 +
      m) BASE="MM" ;;
 +
      *) echo "Unknown Argument $OPTION given"; exit 1;;
 +
  esac
 +
done
 +
</pre>
 +
 +
 +
== Parse in 'while'-loop ==
 
<pre>
 
<pre>
 
#!/bin/sh
 
#!/bin/sh
  
while [ $1 ]; do
+
while [ "$1" ]; do
  case $1 in
+
  optarg=""
      -u)
+
  opt=""
        shift;
+
  case "$1" in
        MYUSER=$1
+
      -*=*)  
        shift;
+
          opt=`echo "$1" | sed 's/=.*//'`
      ;;
+
          optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
      --quota)
+
      ;;
        shift;
+
        *)  
        Q=$1
+
          opt=$1
        shift;
+
      ;;
      ;;
+
      -h)
+
        echo "my help text "
+
      ;;
+
      *)
+
        echo "Unknown option $1"
+
        echo "Press enter to countinue"
+
        read dummy
+
      ;;
+
 
   esac
 
   esac
 +
 +
  case $opt in
 +
      -u)
 +
          shift; # shift the '-u'
 +
          MYUSER=$1
 +
          shift; # shift the val
 +
      ;;
 +
 +
      --user)
 +
          MYUSER=$optarg;
 +
          shift; # shift the '--user=val'
 +
      ;;
 +
 +
      -h|*)
 +
          echo "my help text "
 +
          exit 1
 +
      ;;
 +
    esac
 
done
 
done
  
Zeile 31: Zeile 57:
 
</pre>
 
</pre>
  
test
+
 
 +
== Parse in 'select'-loop ==
 +
 
 +
<pre>
 +
select opt in $@; do
 +
  if [ "$opt" = "Quit" ]; then
 +
      echo done
 +
      exit
 +
  elif [ "$opt" = "Hello" ]; then
 +
      echo Hello World
 +
  else
 +
      clear
 +
      echo bad option
 +
  fi
 +
done
 +
</pre>             
 +
 
 +
If you run this script you'll see that it is a programmer's dream for text based menus. You'll probably notice that it's very similar to the 'for' construction, only rather than looping for each 'word' in $OPTIONS, it prompts the user. but use '''case''' construct in the select-loop instead...
  
 
=Siehe auch=
 
=Siehe auch=
[[Bash:Skeleton|Vorlage Bash-Script]]
+
[[Bash/Scripting/Skeleton|Vorlage Bash-Script]] <br/>
 +
[[Bash/Scripting/Variables|Gueligkeitsbereiche von Variablen]]<br/>

Aktuelle Version vom 24. September 2012, 06:03 Uhr


Parse with getopts (bash-builtin)

while getopts "fhm" OPTION; do
   case $OPTION in
      f) FORCE=1 ;;
      h) print_help; exit ;;
      m) BASE="MM" ;;
      *) echo "Unknown Argument $OPTION given"; exit 1;;
   esac
done


Parse in 'while'-loop

#!/bin/sh

while [ "$1" ]; do
   optarg=""
   opt=""
   case "$1" in
       -*=*) 
          opt=`echo "$1" | sed 's/=.*//'`
          optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
       ;;
        *) 
          opt=$1
       ;;
   esac

   case $opt in
       -u)
          shift; # shift the '-u'
          MYUSER=$1
          shift; # shift the val
       ;;

       --user)
          MYUSER=$optarg;
          shift; # shift the '--user=val'
       ;;

       -h|*)
          echo "my help text "
          exit 1
       ;;
    esac
done

echo "\$MYUSER = $MYUSER"
echo "\$Q = $Q"

exit 0


Parse in 'select'-loop

select opt in $@; do
   if [ "$opt" = "Quit" ]; then
      echo done
      exit
   elif [ "$opt" = "Hello" ]; then
      echo Hello World
   else
      clear
      echo bad option
   fi
done

If you run this script you'll see that it is a programmer's dream for text based menus. You'll probably notice that it's very similar to the 'for' construction, only rather than looping for each 'word' in $OPTIONS, it prompts the user. but use case construct in the select-loop instead...

Siehe auch

Vorlage Bash-Script
Gueligkeitsbereiche von Variablen