Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Practical use of case Loop, for Loop and While Loop in Shell programming in Linux system

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Case statement and Loop statement in Shell programming structure of case Multi-Branch statement case statement

Execute different command sequences according to different values of variables

Case variable value in mode 1) command sequence 1; mode 2) command sequence 2;; *) default command sequence esac

Structure diagram of case statement

Example of case statement Operation

Example 1

Keystroke type identification

(1) prompt the user to enter a character

(2) judge whether the character is a letter or a number.

[root@localhost ~] # vim 1.sh #! / bin/bash read-p "Please enter a character:" key case $key in [Amurz] | [Amurz]) echo "you entered a letter";; [0-9]) echo "you entered a number" Esac [root@localhost ~] # chmod + x 1.sh [root@localhost ~] #. / 1.sh

Example 2

Write system service scripts

(1) use start, stop, restart and other parameters to control the service

(2) Service control instructions are passed in through the location variable $1.

[root@localhost ~] # vim myprog.shandra Bingray bashcase "$1" instart) echo-n "is starting the sleep service..." If sleep 7200 & then echo "OK" fi;; stop) echo-n "stopping the sleep service...." Pkill "sleep" & > / dev/nullecho "OK";; status) if pgrep "sleep" & > / dev/null;then echo "sleep service has started." Else echo "sleep service has stopped" fi;; restart) $0 stop $1 start;;*) echo "usage: $0 {start | stop | status | restart}" esac [root@localhost ~] # chmod + x myprog.sh

Circular statement the structure of the for statement reads different variable values and is used to execute the same set of commands one by one, for variable name, in value list, do command sequence, done

Structure diagram of for statement

Example of for statement Operation

Example 1

Add users in batch

(1) the user name is stored in the users.txt file, one for each line

(2) the initial password is set to 123456.

(3) verify the script

[root@localhost opt] # vim users.txt lisi wangwu zhangsan zhaoliu [root@localhost opt] # vim 3.sh #! / bin/bash TMP=$ (cat / opt/users.txt) for USER in $TMP do useradd $USER echo "123456" | passwd-- stdin $USER & > / dev/null done [root@localhost opt] # chmod + x 3.sh [root@localhost opt] #. / 3.sh

Example 2

Check the host status based on the IP address

(1) the IP address is stored in the ipadds.txt file, one for each line

(2) use the ping command to test the connectivity of each host.

[root@localhost ~] # vim ipadds.txt192.168.217.130192.168.178.2192.168.217.129192.168.312.23 [root@localhost ~] # vim ping.sh#1/bin/bashTMP=$ (cat / root/ipadds.txt) for IP in $TMPdo ping-c 3-I 0.2-M 3$ IP & > / dev/null if [$?-eq 0] then echo "$IP is up" else echo "$IP id down" fidone [root@ Localhost ~] # chmod + x ping.sh [root@localhost ~] #. / ping.sh

The structure of the while statement repeatedly tests a condition, repeatedly executing the while conditional test operation do command sequence done as long as the condition is established

Structure diagram of while statement

Example of while statement Operation

Example 1

Add users in batch

(1) the user name starts with stu and is numbered in numerical order

(2) add a total of 20 users, namely stu1, stu2,., stu20

(3) the initial password is set to 123456.

[root@localhost ~] # vim stu.sh #! / bin/bashi=1DP= "stu" while [$I-le 20] do useradd $DP$i echo "123456" | passwd-- stdin $DP$i & > / dev/null let i++done [root@localhost ~] # chmod + x stu.sh [root@localhost ~] #. / stu.sh

Example 2

Guess the price of goods

(1) obtain random numbers through variable RANDOM

(2) prompt the user to guess and record the number of times, and exit the cycle after guessing.

[root@localhost ~] # vim san.shrunken raceme Bash Swatches 0PRENTHUS $(expr $RANDOM% 1000) echo "the actual price of the goods is 09999, guess what?" While truedo read-p "Please enter the number of prices you guessed:" RIN let offers + if [$RIN-eq $PR] then echo "Congratulations on your success!" Echo "your guess is $S" exit 0 elif [$RIN-lt $PR] then echo "your guess is too small" else echo "your guess is too large" fidone [root@localhost ~] # chmod + x san.sh [root@localhost ~] #. / san.sh

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report