Linux Basic 16-bash Programming (case statements and scripting options)

Source: Internet
Author: User
Tags case statement

(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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.