(1) Case statement: Select structure case, SWITCH in value1) statement ...;; The double semicolon ends. value2) statement ...; *) statement ...;; Esac//esac end. (2) Shift parameter description: After the shift command is executed, the command-line arguments in the position variable ($1~$9) are passed to the left in turn. Example: #!/bin/bash#debug=0add=0del=0for I in ' seq 0 $# '; Do//Explanation: Seqs 1 $# means from 1 to $ #个数字. If [$#-gt 0]; Thencase $ in-v|--verbose) debug=1shift;; -H|--HELP) echo "Usage: ' basename '--add user_list--del user_list-v|--verbose-h|--help" Exit 0;; --add) Add=1addusers=$2shift 2;; --del) Del=1delusers=$2shift 2;; *) echo "Usage: ' basename $--add user_list--del user_list-v|--verbose-h|--help" Exit 7;; Esacfidoneif [$ADD-eq 1]; Thenfor USER in ' echo $ADDUSERS | Sed ' [email protected],@ @g '; Doif ID $USER &>/dev/null; then[$DEBUG-eq 1] && echo "$USER exists." Elseuseradd $USER [$DEBUG-eq 1] && echo "Add USER $USER finished." Fidonefiif [$DEL-eq 1]; Thenfor USER in ' echo $DELUSERS | Sed ' [email protected],@ @g '; Doif ID $USER &>/dev/null; Thenuserdel-r $USER [$DEBUG-eq 1] && ECho "Delete $USER finished." else[$DEBUG-eq 1] && echo "$USER not exist." Fidonefi Exercise: Write a script showlogged.sh, which uses the format: Showlogged.sh-v-c-h|--help where the-h option can only be used alone to display help information; the-C option displays the number of users logged on on the current system If you use the-V option at the same time, both the number of simultaneous users logged in and the information about the logged-on user are displayed, such as logged Users:4.they are:root tty2 02:41root pts/1 Mar 8 08:36 (172.16.100 .177) Root PTS/5 Mar 8 07:56 (172.16.100.177) Hadoop pts/6 Mar 8 09:16 (172.16.100.177) #!/bin/bash#declare-i Shownum=0decla Re-i showusers=0for i in ' seq 1 $# '; Do//Explanation: Seqs 1 $# means from 1 to $ #个数字. If [$#-gt 0]; Thencase $ in-h|--help) echo "Usage: ' basename '-h|--help-c|--count-v|--verbose" Exit 0; -v|--verbose) let Showusers=1shift;; -c|--count) let Shownum=1shift;; *) echo "Usage: ' basename $-h|--help-c|--count-v|--verbose" Exit 8;; Esacfidoneif [$SHOWNUM-eq 1]; Thenecho "logged users: ' Who | Wc-l '. " If [$SHOWUSERS-eq 1]; Thenecho "They is:" Whofifi Debug Script: Bash-x test.sh
Linux Basic 16-bash Programming (case statements and scripting options)