Get the App
SLTechnology News&Howtos  ›  Servers  › 

Shell programming-- case statement and Loop statement (3)

Shulou Source: shulou.com Published: 2022-06-03 04:16:33 09月11日 Update

The structure of case multi-branch statement case statement executes different command sequences according to different values of variables.

Example 1 keystroke type recognition prompts the user to enter a character to determine whether the character is a letter, a number or other character #! / bin/bashread-p "Please enter a character:" keycase $key in [Amurz] | [Amurz]) echo "you entered a letter"; [0-9]) echo "you entered a number"; *) echo "you entered a special character" esac

Example 2 enter scores and view grade #! / bin/bashread-p "Please enter your grades:" numcase $num in [8-9] [0-9] | 100) echo "excellent";; 7 [0-9]) echo "good";; 6 [0-9]) echo "qualified";; [0-9] | [1-5] [0-9]) echo "unqualified" *) echo "you typed incorrectly" esac

The structure of the for statement reads different variable values and is used to execute the same set of commands one by one.

Example 1 batch add users store the user name in the name.txt file, and each line has an initial password set to 123456 authentication script

#! / bin/bashTMP=$ (cat / root/name.txt) for USER in $TMPdo echo "user is $USER" useradd $USER echo "123456" | passwd-- stdin $USER > / dev/nulldone

Example 2 according to the IP geological inspection host status IP geology is stored in the demo04.txt file, each line uses the ping command to detect the connectivity of each host

#! / bin/bashIP=$ (cat / root/demo04.txt) for ip in $IPdo ping-c 1-s 1-w 3$ ip > / dev/null if [$?-eq 0] then echo "$ip host exists" else echo "$ip host does not exist" fidone

The structure of the while statement repeatedly tests a condition and executes it repeatedly as long as the condition is true

Example 1 adds a total of 5 users, namely stu1,stu2,stu3..., whose user names start with stu and are numbered in numerical order. The initial passwords are all set to 123456 passwd stdin stu$num & > / dev/null let num++done while [$num-le 5] douseradd stu$num echo "123456"

Example 2 guess the commodity price script through the variable RANDOM to get a random number to prompt the user to guess and record the number of times, and then exit the loop #! / bin/bashrandom=$ (expr $RANDOM% 100) tim=0while truedo read-p "Please enter the price of the commodity:" jia let tim++ if [$jia-eq $random] then echo "Congratulations on guessing" echo "you guessed a total of $tim" exit 0 elif [$jia-lt $random] then echo "your guess is smaller than" else echo "your guess is larger" fidone "

Comprehensive examples enter five stores to buy goods respectively. Finally, check the total consumption #! / bin/bashi=1sum=0while [$I-le 5] do echo "enter the $iStore" read-p "enter (yes/no)" doing while [$doing = "yes"] do echo "1: clothes ¥200" echo "2: shoes ¥100" echo "3: gloves ¥75" echo "4: pants ¥150" read-p "Please select Item sequence to be purchased: "num case $num in 1) echo" clothes purchased successfully "expr $[sum+=200] & > / dev/null ; 2) echo "shoes purchased successfully" expr $[sum+=100] & > / dev/null; 3) echo "Gloves purchased successfully" expr $[sum+=75] & > / dev/null *) echo "trousers purchased successfully" expr $[sum+=150 & > / dev/null esac read-p "whether to continue the purchase (yes/no)" doing done let iTunes + if [$doing = "no"] then continue fidone echo "Total Shopping Price: $sum"

The structure of the until statement repeatedly tests a condition and executes it repeatedly as long as the condition is not true

Example 1 calculates the sum of 1-50 and calculates the sum of 1-50 by cyclic accumulation #! / bin/bashsum=0i=0until [$I-eq 51] do let sum+=$i let i++doneecho "the sum of the total is: $sum"

Example 2 sends an online message for the specified user. If the user is not online (not logged in to the system), try every 5 seconds until the parameter is passed to the script #! / bin/bashif [$#-lt 0] then echo "Usage:$0" exit 1figrep "$1" / etc/passwd & > / dev/nullif [$?-eq 0] Then: else echo "user does not exist" fiuntil who | grep "$1" > / dev/nulldo echo "user is not online" sleep 5doneecho $2 | write $1

Thank you for reading!

Tags: User instance input statement character success host commodity condition structure different command score number message script online loop price variable Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Docker Redmi Apple Shulou Information