Circular statement usage in bash programming _linux shell

Source: Internet
Author: User
Tags sleep
1.if is a single branch statement, using the following format:
if condition; Then
Statement
.....
Fi
2.if ... else is a two-spoke statement, using the following format:
if condition; Then
Statement
....
Else
Statement
....
Fi
3.if ... elif...elif...else is a multiple-spoke statement, using the following format:
if condition; Then
Statement
....
Elif condition; Then
Statement
.....
Elif condition; Then
Statement
.....
.
.
.
Else
Statement
....
Fi
A 4.while statement is a circular statement that loops when conditions are met and exits the loop if it is not satisfied, using the following format:
While condition; Todo
Statement
.....
Done
5.until statement is also a circular statement, when the condition is not satisfied with the loop, the satisfaction does not cycle, the use of the format is as follows:
Until condition; Todo
Statement
.....
Done
6.case is also a circular statement, using the following format:
Case $var (variable); In
value1)
......

value2)
.....

*)

..
..
..
Esac

Scripting Exercises:

1. Calculate the and of all positive integers divisible by 3 within 100.
Copy Code code as follows:

#!/bin/bash
Declare-i sum=0
For I in {1..100}; Todo
If [$[$I%3]-eq 0]; Then
Let sum+= $I
Fi
Done
echo "The sum is: $sum"

2. Calculate all odd numbers and all even numbers within 100
Copy Code code as follows:

#!/bin/bash
# echo "Exercise"
Declare-i sum1=0
Declare-i sum2=0
For I in {1..100}; Todo
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. Judge the type of file under/var/log:
If it is a normal document, it is a normal document;
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 Code code as follows:

#!/bin/bash
file1=/var/log/*
For file in $file 1; Todo
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 the default shells for bash on the current system and the default shell is
/sbin/nologin's Users
and statistics The total number of users under various types of shell, showing the results in the form of: Bash,3user,they
Are:root,redhat,gentoo Nologn,2user,they are:bin,ftp
Copy Code code 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; Todo
s= ' grep ' ^ $I: "$file | cut-d:-f7 '
If ["$s" = $bsh]; Then
Let d1= $d 1+1
Muser= $I \, $muser
elif ["$s" = $nobsh]; Then
Let d2= $d 2+1
Suser= $I \, $suser
Fi
Done
Echo BASH, $d 1 users, they are: "
Echo $muser
Echo
echo "Nologin, $d 2 users, they are:"
Echo $suser

5. Write a script:
(1) If it does not exist, create a file/tmp/maintenance if it exists, delete it beforehand
(2) Add the following content to the file/tmp/maintenance:
172.16.0.6
172.16.0.17
172.16.0.20
(3) test the 172.16.0.0/16 network of all the host is online, if the online display its online, if this host
In the/tmp/maintenance file, it shows that it is in a maintenance state, otherwise, it shows that its status is unknown;
Copy Code code 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}; Todo
For host in {1..254}; Todo
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 that completes the following functions:
(1), prompts the user to enter a user name;
(2), display a menu to the user, in the form of:
U|u Show UID
G|g Show GID
S|s Show SHELL
Q|q quit
(3), to remind the user to select an option, and display the content of their choice; If the user gives an option other than the one indicated above, remind the user of the wrong options and ask them to choose again to execute;
The first method:
Copy Code code 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

The second method:
Copy Code code 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; Todo
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 this specified script through vim;
(2) If the user still has an error while saving the exit through VIM, repeat the contents of step 1th, otherwise, close the exit normally.
The first of these methods
Copy Code code as follows:

#!/bin/bash
[!-F $] && echo "wrong path." && Exit 2
Until Bash-n $ &>/dev/null; Todo
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 Code code as follows:

#!/bin/bash
[!-F $] && echo "wrong path." && echo "quit!" && exit 9
Until Bash-n $ &>/dev/null; Todo
Read-p "Grammar wrong Please 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
Copy Code code as follows:

#!/bin/bash
[!-F $] && echo "wrong scripts." && Exit 4
Bash-n $ &>/dev/null
Valu=$?
Until [$valu-eq 0]; Todo
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 if so, notify the current scripting performer "Redhat
is logged on. " Otherwise, test again after 5 seconds of sleep, and exit until it is logged on;
The first of these methods
Copy Code code as follows:

#!/bin/bash
who | grep "^redhat\>" &>/dev/null
Reval=$?
Until [$reval-eq 0];d o
Sleep 5
who | grep "^redhat\>" &>/dev/null
Reval=$?
Done
echo "Redhat is logged on."

The second method:
Copy Code code as follows:

#!/bin/bash
Until who | grep "^redhat\>" &>/dev/null; Todo
Sleep 5
Done
echo "Redhat is logged on"

9 Write a script:
(1), add 20 users to the system, the name is LINUXER1-LINUXER20, password is its username respectively, to use while loop;
(2), requirements: Before adding each user in advance to determine whether the user exists, if already exist, then no longer add this user;
(3), after the completion of the addition, display linuxer1-linuxer20 each user name and corresponding UID number and GID number, such as STU1, uid:1000, gid:1000
Copy Code code as follows:

#!/bin/bash
Declare-i I=1
While [$I-le 20]; Todo
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 comes from the "Knowledge System" blog
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.