for loop, while loop, continue, break, exit parsing, select usage

20.10 for loops

eg:

Find the sum of the numbers from 1 to 100.
[root@localhost sbin]# vim sum.sh
#!/bin/bash
sum=0
for i in seq 1 5
do
sum=$[sum+$i]
done
echo "$sum"

[root@localhost sbin]# sh sum.sh
15
file list loop
[root@localhost sbin]# vim for.sh
#!/bin/bash
dir=/usr/local/sbin/
for a in ls $dir
do
if [ -d $ a ]
then
echo $a
ls $a
fi
done
echo "No directory file!"

[root@localhost sbin]# sh -x for.sh

  • dir=/usr/local/sbin/
    ++ ls /usr/local/sbin/
  • for a in 'ls $dir'
  • '[' -d datefile.sh ']'
  • for a in 'ls $dir'
  • '[' -d filetar.sh ']'
  • for a in 'ls $dir'
  • '[' -d for.sh ']'
  • for a in 'ls $dir'
  • '[' -d if2.sh ']'
  • for a in 'ls $dir'
  • '[' -d sum.sh ']'
  • echo 'No directory file!'
    No directory file!
    for -- separator
    [root@localhost adai]# ll
    total usage 0
    -rw-r--r-- 1 root root 0 September 14 19:25 1
    -rw- r--r-- 1 root root 0 Sep 14 19:25 2
    -rw-r--r-- 1 root root 0 Sep 14 19:25 3 4.txt #Note
    : 3 4.txt is a file (34 with a space in between)

[root@localhost adai]# for i in ls ./; do echo $i ; done
1
2
3
4.txt
The above result shows the description, for defaults to a space or a newline (carriage return) as a separator.

20.11-20.12 while loop

Format: while condition; do...; done

eg:

When the system load is greater than 10, send an email and execute it every 30 seconds.
[root@localhost adai]# vim while.sh
#!/bin/bash
while :
do
load= w|head -1 |awk -F 'load average:' '{print $2}' |cut -d . -f1
if [ $load -gt 10 ]
then
top |mail -s "load is high: $load" [email protected]
fi
sleep 30
done
#while ":" means an infinite loop, it can also be written as while true, which means "true" (Mathematics - true proposition, false proposition)

#Attention: awk -F 'load average: 'Here specifies 'load average: ' as the delimiter, pay attention to the space after the colon #If
this space is not added, the filtered result will have spaces, and the spaces need to be filtered out here

[root@localhost adai]# sh -x while.sh

  • :
    ++ head -1
    ++ awk -F 'load average: ' '{print $2}'
    ++ cut -d. -f1
    ++ w
  • load=0
  • '[' 0 -gt 10 ']'
  • sleep 30
    .
    .
    .
    If you don't stop the script manually, it will be executed in a loop (press Ctrl+c to end), and it is used with screen in the actual environment.

In the interactive mode, the user enters a character and checks whether the character meets the conditions, such as: empty, non-number, number. Make judgments on characters separately, and then make different responses.
[root@localhost sbin]# vim while2.sh
#!/bin/bash
while true
do
read -p "Please input a number:" n
if [ -z "$n" ]
then
echo "You need input some characters!"
continue
fi
n1= echo $n|sed 's/[-0-9]//g'
if [ -n "$n1" ]
then
echo "The character must be a number!"
continue
fi
break
done
echo $n
#continue: restart after interrupting this while loop;
#break: means jump out of this layer loop, that is, the while loop ends

[root@localhost sbin]# sh while2.sh
Please input a number:
You need input a character!
Please input a number:eee333
The character must be a number!
Please input a number:3
3
20.13 break 跳出循环

eg:

[root@localhost sbin]# vim break.sh
#!/bin/bash
for i in seq 1 5
do
echo "$i"
if [ $i -eq 3 ]
then
break
fi
echo "$i"
done
echo "Finished!"

[root@localhost sbin]# sh break.sh
1
1
2
2
3
Finished!
That is, jump out of the while loop and continue to execute commands outside the loop.

20.14 continue to end this loop

eg:

[root@localhost sbin]# vim continue.sh
#!/bin/bash
for i in seq 1 5
do
echo "$i"
if [ $i -eq 3 ]
then
continue
fi
echo "$i"
done
echo "Finished!"

[root@localhost sbin]# sh continue.sh
1
1
2
2
3
4
4
5
5
Finished!
That is, restart the next cycle after ending this cycle.

20.15 exit exits the entire script

eg:

[root@localhost sbin]# vim exit.sh
#!/bin/bash
for i in seq 1 5
do
echo "$i"
if [ $i -eq 3 ]
then
exit
fi
echo "$i"
done

[root@localhost sbin]# sh exit.sh
1
1
2
2
3
Exit the entire script, the following commands will not be executed.

Extended: the usage of select in the shell

select is also a type of loop, which is more suitable for use in the case of user selection.
For example, we have a requirement that after running the script, let the user select a number, select 1, the w command will be run, select 2 to run the top command, select 3 to run the free command, and select 4 to exit. The script does this like this:

#!/bin/bash

echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
echo
select command in w top free quit
do
case $command in
w)
w
;;
top)
top
;;
free)
free
;;
quit)
exit
;;
*)
echo "Please input a number:(1-4)."
;;
esac
done
执行结果如下:

sh select.sh
Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit

1) w
2) top
3) free
4) quit
#? 1
16:06:58 up 109 days, 22:01, 1 user, load average: 0.11, 0.05, 0.01
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 222.128.156.84 16:05 0.00s 0.00s 0.00s w

#? 3
total used free shared buffers cached
Mem: 1020328 943736 76592 0 86840 263624
-/+ buffers/cache: 593272 427056
Swap: 2097144 44196 2052948
#?
We found that select will list the commands corresponding to the serial numbers by default, and each time you enter A number, the corresponding command will be executed, and the script will not exit after the command is executed. It will continue to make us lose the serial number again. We can also modify the prompt in front of the serial number, just use the variable PS3, and modify the script again as follows:

#!/bin/bash
PS3="Please select a number: "
echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
echo

select command in w top free quit
do
case $command in
w)
w
;;
top)
top
;;
free)
free
;;
quit)
exit
;;
*)
echo "Please input a number:(1-4)."
esac
done
If you want the script to exit automatically every time you enter a serial number, you need to change the script again as follows:

#!/bin/bash
PS3="Please select a number: "
echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
echo

select command in w top free quit
do
case $command in
w)
w;exit
;;
top)
top;exit
;;
free)
free;exit
;;
quit)
exit
;;
*)
echo "Please input a number:(1-4).";exit
esac
done

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326173467&siteId=291194637