Bash menu in bash with getopts

A little more sophisticated script than the former bash menu example to get some arguments from command line.

 bash | 
 
 copy code |
?

01
#!/bin/bash
02
03
04
05
while getopts "u:p:" opt; do
06
07
  case $opt in
08
    u)
09
      echo "-u was triggered, Parameter: $OPTARG"
10
      dbuser="$OPTARG"
11
      ;;
12
    p)
13
      echo "-u was triggered, Parameter: $OPTARG"
14
      dbpass="$OPTARG"
15
      ;;
16
    \?)
17
      echo "-u was triggered, Parameter: $OPTARG"
18
      exit 1
19
      ;;
20
    \?)
21
      echo "-u was triggered, Parameter: $OPTARG"
22
      exit 1
23
24
25
      ;;
26
27
  esac
28
29
30
31
done
32
33
# Clear all options and reset the command line
34
shift $(( OPTIND -1 ))

Leave a Reply