Loop statement usage in bash Programming

Source: Internet
Author: User

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 within 100 that can be divisible by 3.Copy codeThe 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. Calculate the sum of all odd numbers within 100 and the sum of all even numbersCopy codeThe Code is as follows :#! /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 indicates it is a directory file;
If it is a symbolic link file, it is a symbolic link file;
Otherwise, the file type cannot be identified;Copy codeThe 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 display all users with bash by default and those with bash by default on the current system respectively.
/Sbin/nologin user
And count the total number of users under various types of shell, the result is shown in the form of bash, 3 user, they
Are: root, redhat, gentoo nologn, 2 user, they are: bin, ftpCopy codeThe 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
Echo "NOLOGIN, $ d2 users, they are :"
Echo $ suser

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.
In the/tmp/maintenance file, it is displayed as being maintained; otherwise, it is displayed as unknown;Copy codeThe 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
Done

6. Write a script to complete the following functions:
(1) prompt the user to enter a user name;
(2) display a menu to 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, the user is notified of an incorrect option, and ask them to reselect and execute;
Method 1:Copy codeThe Code 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:Copy codeThe Code is as follows :#! /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 after the user opens the editing via vim and saves and exits, repeat the content in step 1; otherwise, the user closes and Exits normally.
Method 1Copy codeThe Code 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:Copy codeThe 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

Method 3Copy codeThe Code is as follows :#! /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. Write a script:
Check whether the redhat user has logged on to the system. If yes, notify the current script executor "redhat
Is logged on. "; otherwise, the system tests again after five seconds of sleep until it logs on and exits;
Method 1Copy codeThe 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:Copy codeThe 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 user name, to use the while loop;
(2) requirements: Determine whether the user exists before adding each user. If the user already exists, the user will not be added;
(3), after adding, display each user name of the linuxer1-linuxer20 and the corresponding UID number and GID number, such as stu1, UID: 1000, GID: 1000Copy codeThe Code is as follows :#! /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

This article is from the "knowledge system" blog

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.