1.if is a single branch statement, using the following format:
if condition; Then
Statement
.....
Fi
2.if. Else is a two-branch statement that uses the following format:
if condition; Then
Statement
....
Else
Statement
....
Fi
3.if. Elif...elif...else is a multi-branch statement that uses the following format:
if condition; Then
Statement
....
Elif condition; Then
Statement
.....
Elif condition; Then
Statement
.....
.
.
.
Else
Statement
....
Fi
The 4.while statement is a looping statement that loops only if the condition is satisfied, exits the loop if it is not satisfied, and uses the following format:
While condition; Do
Statement
.....
Done
5.until statement is also a circular statement, when the condition is not satisfied with the case of the loop, the meeting is not loop, using the format as follows:
Until condition; Do
Statement
.....
Done
6.case is also a looping statement, using the following format:
Case $var (variable); Inch
value1)
......
value2)
.....
*)
..
..
..
Esac
Scripting Exercises:
1. Calculates the and 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 all the odd and all the even and all the numbers within 100 and
Copy 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: $sum 1"
echo "The Oddnumber sum is: $sum 2"
3. Determine the type of file under/var/log:
If it is an ordinary document, it is a normal file;
If it is a directory file, it is a directory file;
If it is a symbolic link file, it is a symbolic link file;
Otherwise, the description file type is not recognized;
Copy CodeThe code is as follows:
#!/bin/bash
file1=/var/log/*
For file in $file 1; 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 that shows all default shell-bash users on the current system and the default shell as
Users of/sbin/nologin
and statistics of the total number of users under the shell, showing the result shape such as: Bash,3user,they
Are:root,redhat,gentoo Nologn,2user,they are:bin,ftp
Copy 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= $d
Muser= $I \, $muser
elif ["$s" = $nobsh]; Then
Let d2= $d 2+1
Suser= $I \, $suser
Fi
Done
echo "BASH, $d 1 users, they is:"
Echo $muser
Echo
echo "Nologin, $d 2 users, they is:"
Echo $suser
5. Write a script:
(1) If it does not exist, create the file/tmp/maintenance, if it exists, delete it beforehand.
(2) Add the following in the file/tmp/maintenance:
172.16.0.6
172.16.0.17
172.16.0.20
(3) test if all hosts within the 172.16.0.0/16 network are online, if the host is online
In the/tmp/maintenance file, it shows that it is in a maintenance state; otherwise, its status is 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), prompting the user to enter a user name;
(2), display a menu to the user, shape such as:
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 content of their choice, if the user is given a non-suggested option, then remind the user to give the wrong options, and ask them to re-select the execution;
The first method:
Copy CodeThe code is as follows:
#!/bin/bash
Read-p "Enter A user name:" username
! ID $username &>/dev/null && echo "Come on, the user 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
The second method:
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), to determine whether a specified script is a syntax error, if there is an error, then remind the user to type Q or Q ignore the error and exit, any other key can open the specified script through vim;
(2), if the user through Vim open edit after saving exit still have errors, repeat the 1th step of the content; otherwise, close the exit normally.
The first of these methods
Copy CodeThe code is as follows:
#!/bin/bash
[!-F $] && echo "wrong path." && Exit 2
Until Bash-n $ &>/dev/null; Do
Read-p "Q|q to quit. Others to edit:" opt
Case $opt in
Q|Q)
echo "Quit ..."
Exit 3
*)
Vim $
Esac
Done
The second method:
Copy CodeThe code is as follows:
#!/bin/bash
[!-F $] && echo "wrong path." && echo "quit!" && exit 9
Until Bash-n $ &>/dev/null; Do
Read-p "Grammar wrong" enter Q|q to quit. Others to edit: "opt
Case $opt in
Q|Q)
echo "Quit ..."
Exit 3
*)
Vim $
Bash-n $ &>/dev/null
Val=$?
["$val"-ne 0] && echo "Xiu Gai bu Cheng gong."
Esac
Done
The third method of
Copy CodeThe code is as follows:
#!/bin/bash
[!-F $] && echo "wrong scripts." && Exit 4
Bash-n $ &>/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 $
Bash-n $ &>/dev/null
Valu=$?
Esac
Done
8 Write a script:
Check to see if the Redhat user is logged on to the system and notify the current script performer if logged in Redhat
is logged on. " Otherwise, it will be tested again after 5 seconds of sleep, until it logs out;
The first of these methods
Copy CodeThe code is as follows:
#!/bin/bash
W.H.O. | grep "^redhat\>" &>/dev/null
Reval=$?
Until [$reval-eq 0];d o
Sleep 5
W.H.O. | grep "^redhat\>" &>/dev/null
Reval=$?
Done
echo "Redhat is logged on."
The second method:
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 while loop;
(2), requirements: Before adding each user in advance to determine whether the user exists, if already exists, no longer add this user;
(3), add completed, display linuxer1-linuxer20 each user name and corresponding UID number and GID number, shaped like stu1, uid:1000, gid:1000
Copy 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
Looping statement usage in bash programming