Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Loop statement in bash programming

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use circular statements in bash programming. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1.if is a single branch statement in the following format:

If condition; then

Statement

... ..

Fi

2.if... Else is a two-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

The 4.while statement is a loop statement, which loops only when the conditions are met, and exits the loop if it is not satisfied. The format is as follows:

While condition; do

Statement

... ..

Done

The 5.until statement is also a loop statement. If the condition is not satisfied, it will not be looped. The format is as follows:

Until condition; do

Statement

... ..

Done

6.case is also a circular statement, and the format is as follows:

Case $var (variable); in

Value1)

……

Value2)

... ..

*)

..

..

..

Esac

Script activity:

1. Calculate the sum of all positive integers within 100 that are divisible by 3.

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"

two。 Calculate the sum of all odd numbers and all even numbers within 100

The 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 type of file under / var/log:

If it is an ordinary file, it means it is an ordinary file.

If it is a catalog file, it is a catalog file

If it is a symbolic link file, it is a symbolic link file

Otherwise, the file type is not recognized

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 that displays all users on the current system whose default shell is bash and the default shell is

Users of / sbin/nologin

And count the total number of users under all kinds of shell, and display the results such as: bash,3user,they

Are:root,redhat,gentoo nologn,2user,they are:bin,ftp

The code is as follows:

#! / bin/bash

File=/etc/passwd

Bsh='/bin/bash'

Nobsh='/sbin/nologin'

Use= `cat $file | cut-d:-f1`

Declare-I d1x 0

Declare-I d2o0

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 a file / tmp/maintenance;. If it exists, delete it beforehand.

(2) add the following 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.0amp 16 network are online, and show that they are online if they are online, if this host

In the / tmp/maintenance file, it is shown that it is in a state of maintenance; otherwise, its status is 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 / 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 perform the following functions:

(1) prompt the user for a user name

(2) display a menu to the user, 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 selected content; if the user gives an option other than the one indicated above, remind the user that the option given is wrong, and ask him to re-select and execute

The first method:

The code is as follows:

#! / bin/bash

Read-p "Enter a username:" username

! Id $username & > / dev/null & & echo "Come on, the user you input unexit" & & exit 9

Cat / dev/null & & echo "Come on, you input user notexit" & & exit 9

Cat / dev/null; do

Read-p "Q | q to quit .vendor to edit:" opt

Case $opt in

Q | Q)

Echo "quit..."

Exit 3

*)

Vim $1

Esac

Done

The second method:

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 .vendor 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

The third method

The 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 is logged in to the system, and if so, notify the current script executor "redhat"

Is logged on. "; otherwise, test again after 5 seconds of sleep; log out until it logs in.

The first method

The 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."

The second method:

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, whose name is linuxer1-linuxer20, password is their username, and use while loop

(2) requirements: determine whether a user exists before adding each user, and if it already exists, do not add this user

(3) after the addition is completed, each user name of linuxer1-linuxer20 and the corresponding UID number and GID number are displayed, such as stu1, UID: 1000, GID: 1000

The code is as follows:

#! / bin/bash

Declare-I Ignition 1

While [$I-le 20]; do

L=linuxer$I

Let iTunes +

! Id $l & > / dev/null & & useradd $l & > / dev/null & & echo "the user:$l" | passwd-- stdin $l & > / dev/null & & echo "add 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 is the end of this article on "how to use circular sentences in bash programming". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report