---------Linux Script Programming---until,for,case

Source: Internet
Author: User

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Until cycle

While CONDITION; When the Docondition-turedone is true, enter the cyclic until CONDITION; Do loop body when done is false, enter the loop


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Loop control command, statement: used in the loop body, used on the expression of the condition judgment

Continue [#] Default current "loop body", early end # early End # layer loop body Break [#] Default current "loop", early end # early End # layer loop


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Create a dead loop (note that the CPU clock cycles are exhausted: )

While true; Do loop body doneuntil false; Do Loop body done


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

While traversing each row of the specified file

while read line; Do loop body Done </path/from/somefile

Assigning Rows to line variables

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

C-style for usage

Avoids a for-build list and consumes a lot of memory space

For (control variable initialization; conditional judgment expression; control variable correction expression); Do Loop body done


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Conditional Judgment case

Case variable reference inpattern) statement;; pattern) statement;; .. *) statement;; Esac

Pattern Support Glob



Until practice

Example: for all positive integers within 100 and

Example: 9x9

Example: Add 10 users User1-user10


Continue, break practice

Example 1: The sum of all even numbers within 100 is required to loop through all positive integers within 100

Example 2: Determine the Docker user every 3 seconds, log the logs and exit the loop if the Docker user logs in


While traversing a file exercise

Example: Display the user name of a user with an even ID number for all users


C-style for usage

Example: 9x9 multiplication table

Practice:

1. Display Menu

CPU) Show CPU infomation

MEM) Show Memory information

disk) Show disk information

Quit) quit


2. Prompt the user to select options

3. Display user-selected content


Further to:

The user chooses, and the display does not exit the script after completion, but prompts the user to continue to choose to display other content until quit using quit


Conditional Judgment case

Practice:

1) script acceptable parameters, Start,stop,restart,status

2) Not one of 4, prompt to use format and exit

3) Start, create/var/lock/subsys/script_name, and show "Start successful"

Consider: How to handle a pre-boot once

4) Stop, delete/var/lock/subsys/script_name and show "Stop Complete"

Consider: What should I do if I have stopped in advance?

5) When restart, stop first, then start

Consider: If there is no start, how to deal with

6) How is status,

/var/lock/subsys/script_name file exists, "Script_name is running ..." is displayed.

/var/lock/subsys/script_name file does not exist, "Script_name is stopped ..." is displayed.


Where: Script_name is the current script name. $


Until practice

1.

Example: ask for all positive integers within 100 and #!/bin/bash#declare-i i=1declare-i sum=0until [$i-gt 100]; dosum=$[$sum + $i]let i++doneecho "sum: $sum"

2.

Example: 9x9#!/bin/bash#declare-i i=1until [$i-gt 9]; Dodeclare-i J=1until [$j-gt $i]; Doecho-n-E "${j}x${i}=$ (expr $i \* $j) \ t" let J++doneecholet i++done

3.

Example: Add 10 users user1-user10#!/bin/bash#declare-i i=1declare-i users=0until [$i-gt 10]; Doif! ID user$i &>/dev/null; Thenuseradd user$iecho "ADD user user$i finished" let Users+=1filet i++done


Continue, break practice

1.

Example 1: The sum of all even numbers within 100 is required to loop through all positive integers within 100 #!/bin/bash#declare-i i=0declare-i sum=0until [$i-gt 100]; Dolet i++if [$[$i%2]-eq 1]; Thencontinuefilet sum+= $idoneecho "Even sum: $sum"

Example 2: The Docker user is judged every 3 seconds, and if the Docker user logs in, log the blog and exit the loop #!/bin/bash#if [-z] "-o" $ "= ="--help "]; Thenecho "Usage: $ <username>" Exit 1fiif! ID $ &>/dev/null; Thenecho "is not exist" exit 2fiuntil false; Dosleep 3if who | grep "^$1\>" &>/dev/null; Thenbreakfidoneecho "Login at $ (date +%f_%t)" >>/tmp/user_login.log judgment: # bash-n a.sh run: # bash a.sh Docker & ;


While traversing a file exercise

1.

Example: All users whose ID number is even, display the user name of their user #!/bin/bash#declare-i sum=0while read line; Doif [$[$ (echo $line | cut-d ': '-f3)%2]-eq 0]; Thenecho-e "$ (echo $line | Cut-d ': '-f1,3--output-delimiter= ') \ T "sum=$ (($sum + 1)) Fidone </etc/passwdecho" Even sum of users: $sum "

C-style for usage

1.

Example Demo: #!/bin/bash#declare-i of all positive integers within 100 sum=0for ((i=1;i<=100;i++)); Dolet sum+= $idoneecho "sum: $sum"

2.

Example: 9x9 multiplication table #!/bin/bash#for ((i=1;i<=9;i++)); Dofor ((j=1;j<=i;j++)); DOECHO-E-N "${j}x${i}=$[$i * $j]\t" Doneechodone

3.

Exercise: 1, Show menu CPU) show CPU Infomationmem) show memory Informationdisk) show disk informationquit) Quit2, Prompt user to select option 3, Show user selected content further: The user selects, and the display does not exit the script after completion, but prompts the user to continue to choose to display other content until using quit to exit #!/bin/bash#while true; Docat << eofcpu) show CPU Infomationmem) show memory Informationdisk) show disk informationquit) Quiteofread-p ' Ent Er a your choice: ' keycase $key incpu) x86info; MEM) vmstat-s;; Disk) fdisk-l/dev/[sh]d[a-z];; Quit) exit 0;; *) echo "Only CPU | Mem | Disk | Quit "continue;; Esacdone

Conditional Judgment case

Exercise: 1) script acceptable parameters, &NBSP;START,STOP,RESTART,STATUS2) not one of 4, prompt to use format and exit 3) when start, create/var/lock/subsys/script_name and show "Start successful" Consider: Before starting once, how to handle 4) stop, delete/var/lock/subsys/script_name, and display "Stop complete" consider: if it has stopped beforehand, how to deal with it? 5) Restart, stop, then start to consider : If there is no start, how to handle 6) How is the Status,/var/lock/subsys/script_name file exists, then displays "Script_name is running ..."/var/ Lock/subsys/script_name file does not exist, it displays "script_name is stopped ..." Where: Script_name is the current script name. $0#!/bin/bash# chkconfig: - 88 12# description: lcc.org######################### srvname=$ (echo $0 | sed  ' s,/$,, '  | sed -r  ' s| (. */) ([^/]+) |\2| ') lockfile=/var/lock/subsys/$SrvName ######################## #unset  start stop restart stauts  otherdeclare -i start=1declare -i stop=1declare -i restart=1declare - I stauts=1declare -i other=1case $1 instart) start=0;; stop) stop=0;; restart) restart=0;; status) status=0;; *) other=0;; esacif [  "$Start"  == 0 ]; then[ ! -e  $Lockfile  ] &&  touch  $Lockfile     echo  "$0 start finished" fiif [  "$Stop"  == 0 ]; then[ -e  $Lockfile  ] && rm -rf $ lockfile    echo  "$0 stop finished" fiif [  "$Restart"  ==  0 ]; then[ -e  $Lockfile  ] && rm -rf  $Lockfile      echo  "$0 stop finished" [ ! -e  $Lockfile  ] &&  touch  $Lockfile     echo  "$0 start finished" fiif [  "$ Status  == 0 ]; then    [ -e  $Lockfile  ] & & echo  "$0 is running ..."  | |  echo  "$0 is stopped" fiif [  "$Other"  == 0 ]; then    echo  "Usage:$0 {start|restart|stop|status}"      exit 1fi

 Next step 1, view script Syntax # bash -n x.sh2, give execute permission # chmod +x  x.sh try to join service 1, clear whether the script follows the LSB style # chkconfig: - 88 12# description: lcc.org 2 , copy to the/etc/rc.d/init.d/directory # cp x.sh /etc/init.d/3, join the Service # chkconfig --add x.sh View service: # chkconfig --list x.sh    [[email protected] bin]#  chkconfig --list x.sh    x.sh            0:off1:off2:off3:off4:off5:off6:off let level 2,345 be on state #  chkconfig --levels  2345 x.sh on    [[email protected] bin]# chkconfig --list  x.sh        x.sh            0:off1:off2:on3:on4:on5:on6:off 
Service x.sh Start Services: Service x.sh Stop view State: Service x.sh Status restart: Services x.sh restart

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/06/43/wKiom1m1KrziL2B5AAAdLpFJJvs717.png "title=" Qq20170910200538.png "alt=" Wkiom1m1krzil2b5aaadlpfjjvs717.png "/>

This article is from the "Reading" blog, make sure to keep this source http://sonlich.blog.51cto.com/12825953/1964166

---------Linux Script Programming---until,for,case

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.