Linux: Usage of loop statements in bash programming

Source: Internet
Author: User
Linux: circular statement usage in bash Programming 1. if is a single branch statement in the following format: ifcondition; thenstatement & hellip ;.. fi2.if & hellip; else is a dual-branch statement in the following format: ifcondition; thenstatement & hellip ;. elsestat Linux learning path: circular statement usage in bash programming 1.if is a single branch statement in the following format: if condition; then statement ..... Fi 2.if... Else is a dual-branch statement in the following format: if condition; then statement .... Else statement .... Fi 3.if... Elif... Elif... Else is a multi-branch statement in the following format: if condition; then statement .... Elif condition; then statement ..... Elif condition; then statement ........ Else statement .... Fi 4. the while statement is a loop statement. The loop is executed only when the condition is met. if the condition is not met, the loop is exited. the format is as follows: while condition; do statement ..... Done 5. the until statement is also a loop statement. if the condition is not met, the loop does not exist. the format is as follows: until condition; do statement ..... Done 6. case is also a loop statement in the following format: case $ var (variable); in value1 )...... Value2 )..... *) ...... Esac script exercise: 1. calculate the sum of all positive integers that can be divisible by 3 within 100. The code is as follows :#! /Bin/bash declare-I sum = 0 for I in {1 .. 100}; do if [$ [$ I % 3]-eq 0]; then let sum + = $ I fi done echo "the sum is: $ sum" 2. the following code calculates the sum of all odd numbers and all even numbers within 100 :#! /Bin/bash # echo "exercise" declare-I sum1 = 0 declare-I sum2 = 0 for I in {1 .. 100}; do if [$ [$ I % 2]-eq 0]; then let sum1 + = $ I else let sum2 + = $ I fi done echo "the even sum is: $ sum1" echo "the oddnumber sum is: $ sum2" 3. determine the file type in/var/log: if it is a common file, it is a common file; if it is a directory file, it is a directory file; if it is a symbolic link file, otherwise, the file type cannot be identified. the code is as follows :#! /Bin/bash file1 =/var/log/* for file in $ file1; do if [-f $ file]; then echo "$ file is common file" elif [-d $ file]; then echo "$ file is directory file" else echo "$ file is unknow" fi done 4. write a script to show all users with bash by default and/sbin/nologin by default on the current system, and count the total number of users in various shell types. The result is shown as follows: bash, 3 user, they are: root, redhat, gentoo nologn, 2 user, they are: bin, ftp code is as follows :#! /Bin/bash file =/etc/passwd bsh = '/bin/Bash' nobsh ='/sbin/nologin' use = 'cat $ file | cut-d: -F1' declare-I d1 = 0 declare-I d2 = 0 for I in $ use; do s = 'grep "^ $ I:" $ file | cut-d: -f7 'if ["$ s" = $ bsh]; then let d1 = $ d1 + 1 muser = $ I \, $ muser elif ["$ s" = $ nobsh]; then let d2 = $ d2 + 1 suser = $ I \, $ suser fi done echo "BASH, $ d1 users, they are: "echo $ muser echo" NOLOGIN, $ d2 users, they are: "echo $ sus Er 5. write a script: (1) if it does not exist, create the file/tmp/maintenance; if it exists, delete it in advance (2) add the following content to the file/tmp/maintenance: 172.16.0.6 172.16.0.17 172.16.0.20 (3) test whether all hosts in the 172.16.0.0/16 network are online. if the host is in the/tmp/maintenance file, it is displayed as being maintained. Otherwise, it is displayed as unknown. the code is as follows :#! /Bin/bash file =/tmp/maintenace if [-e $ file]; then rm-rf $ file &>/dev/null fi touch $ file cat> $ file <EOF 172.16.0.6 172.16.0.17 172.16.0.20 EOF bnet = 172.16 for net in {0 .. 254}; do for host in {1 .. 254}; do if ping-c1-W1 $ bnet. $ net. $ host &>/dev/null; then echo "$ bnet. $ net. $ host is up. "elif grep" $ bnet. $ net. $ host $ "$ file &>/dev/null; then echo" $ bnet. $ net. $ host is under maintenance." Else echo "$ bnet. $ net. $ host state is unknow. "fi done 6 writes a script to complete the following functions: (1) prompt the user to enter a user name; (2) display a menu for the user, such: U | u show uid g | g show gid s | s show shell q | q quit (3). remind the user to select an option and display the selected content; if the user gives an option that is not prompted above, it will remind the user of the option error, and ask the user to reselect it and execute it. The first method is as follows :#! /Bin/bash read-p "Enter a user name:" username! Id $ username &>/dev/null & echo "Come on, the user you input unexit "& exit 9 cat <eof u | u show uid g | g show gid s | s show shell q | q quit EOF read-p" Enter your choice: "op case $ op in U | u) id-u $ username; G | g) id-g $ username; S | s) grep "^ $ username \>"/etc/passwd | cut-d:-f7; Q | q) exit 8; *) echo "input option wrong, quit "exit 9 esac Method 2: Code :#! /Bin/bash read-p "Enter a user name:" username! Id $ username &>/dev/null & echo "Come on, you input user notexit "& exit 9 cat <eof u | u show uid g | g show gid s | s show shell q | q quit EOF read-p" Enter your option: "op while true; do case $ op in U | u) id-u $ username break G | g) id-g $ username break S | s) grep "^ $ username \>"/etc/passwd | cut-d:-f7 break Q | q) exit 7; *) read-p "Wrong option, enter a right option: "op; esac done 7 write a script: (1) determine whether a specified script is a syntax error. if there is an error, remind the user to type Q or q to ignore the error and exit, any other key can use vim to open the specified script. (2) if there is still an error when you open and edit the script using vim, repeat the content in step 1. otherwise, close and exit normally. The code for the first method is as follows :#! /Bin/bash [! -F $1] & echo "wrong path. "& exit 2 until bash-n $1 &>/dev/null; do read-p" Q | q to quit. others to edit: "opt case $ opt in Q | q) echo" quit... "exit 3 *) vim $1 esac done Method 2: The code is as follows :#! /Bin/bash [! -F $1] & echo "wrong path." & echo "Quit! "& Exit 9 until bash-n $1 &>/dev/null; do read-p" Grammar wrong please enter Q | q to quit. others to edit: "opt case $ opt in Q | q) echo" quit... "exit 3 *) vim $1 bash-n $1 &>/dev/null val =$? ["$ Val"-ne 0] & echo "xiu gai bu cheng gong." esac done third method code :#! /Bin/bash [! -F $1] & echo "Wrong scripts." & exit 4 bash-n $1 &>/dev/null valu =$? Until [$ valu-eq 0]; do read-p "Q | q to quit, others to edit:" op case $ op in Q | q) echo "Quit. "exit 9 *) vim $1 bash-n $1 &>/dev/null valu = $? Esac done 8 writes a script to check whether the redhat user has logged on to the system. if the user has logged on, it notifies the current script executor "redhat is logged on. "; otherwise, the system will perform a test again after five seconds of sleep until it logs on and exits. The first method code is as follows :#! /Bin/bash who | grep "^ redhat \>" &>/dev/null reval = $? Until [$ reval-eq 0]; do sleep 5 who | grep "^ redhat \>" &>/dev/null reval = $? Done echo "redhat is logged on." Method 2: The code is as follows :#! /Bin/bash until who | grep "^ redhat \>" &>/dev/null; do sleep 5 done echo "redhat is logged on" 9 write a script: (1), add 20 users to the system, the name is linuxer1-linuxer20, the password is their username, to use the while loop; (2), requirements: determine whether a user exists before adding a user. if the user already exists, the user will not be added. (3) after the user is added, show each user name and the corresponding UID number and GID number of the linuxer1-linuxer20, such as stu1, UID: 1000, GID: 1000 code :#! /Bin/bash declare-I I = 1 while [$ I-le 20]; do l = linuxer $ I let I ++! Id $ l &>/dev/null & useradd $ l &>/dev/null & echo "the user: $ l "| passwd -- stdin $ l &>/dev/null & echo" a dd user $ l successfully "| echo" The user $ l is exit. "d = 'id-u $ l 'g = 'id-g $ l' echo" $ l, UID: $ d, GID: $ g "done
Related Article

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.