Chapter 1th whileLooping Statements
in a programming language , a while loop is a statement of a control flow that uses an expression that returns a Boolean value that, when the return value of the expression is true , repeats the condition of the loop If the return value of the expression is False , the condition of the loop is not executed
1.1 WhileGrammar:
while conditions
Do
Command
Done
1.2 WhileUsage Scenarios: 1.2.1more for creating daemons
[email protected] tmp]# cat test.sh
#!/bin/bash
##############################################################
# File Name:test.sh
# version:v1.0
# Author:da Ya
# Organization: [Email protected]
# Created time:2018-03-24 19:48:06
# Description:
##############################################################
While True
Do
echo "OK" | NC-L 81
Done
[Email protected] tmp]# sh test.sh
get/http/1.1
user-agent:curl/7.29.0
host:10.0.0.31:81
Accept: */*
get/http/1.1
user-agent:curl/7.29.0
host:10.0.0.31:81
Accept: */*
1.2.2You can create a timed task,scheduled tasks with less than one minute execution time
[email protected] tmp]# cat test.sh
#!/bin/bash
While true causes the return value to always be true , so it always loops
Do
Uptime
Sleep 1
Done
[Email protected] tmp]# sh test.sh
19:48:45 up 2 days, 4:12, 4 users, load average:0.08, 0.04, 0.05
19:48:46 up 2 days, 4:12, 4 users, load average:0.08, 0.04, 0.05
19:48:47 up 2 days, 4:12, 4 users, load average:0.08, 0.04, 0.05
19:48:48 up 2 days, 4:12, 4 users, load average:0.07, 0.04, 0.05
^c
1.3 Whilefunction:
Replenish Scheduled Tasks to perform tasks that are less than one second
Loop through certain actions , such as fruit menu
1.4traverse each line in a file: 1.4.1Method One:
While read I
Do
echo "$i"
Done </etc/hosts
[Email protected] scripts]# sh test.sh
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
:: 1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.16.200 mirrors.aliyun.com
1.4.2Method Two:
Cat/etc/hosts|while Read I
Do
echo "$i"
Done
1.4.3Method Three:
EXEC </etc/hosts
While read I
Do
echo "$i"
Done
Get each word in a row
While read I
Do
echo "$i"
For W in $i
Do
echo "$w"
Done
Done </etc/hosts
1th Chapterloop Control Scripts1.1Example: Break
For i in {1..5}
Do
If ["$i"-eq 2];then
Break
Fi
echo "$i"
Done
Echo OK
[Email protected] scripts]# sh test.sh
1
Ok
1.2Example: Continue
For i in {1..5}
Do
If ["$i"-eq 2];then
Continue
Fi
echo "$i"
Done
Echo OK
[Email protected] scripts]# sh test.sh
1
3
4
5
Ok
1.3Example: Exit
For i in {1..5}
Do
If ["$i"-eq 2];then
Exit
Fi
echo "$i"
Done
Echo OK
[Email protected] scripts]# sh test.sh
1
2nd Chapter ShellArray
The simple answer is: An array is a combination of multiple variables
[Email protected] scripts]# name= (Jiang Bo Yang)
[[email protected] scripts]# echo ${name[1]}
Bo
[[email protected] scripts]# echo ${name[2]}
Yang
[Email protected] scripts]# unset name[1]
[[email protected] scripts]# echo ${name[1]}
[[email protected] scripts]# echo ${name[*]}
Jiang Yang
[[email protected] scripts]# echo ${name[@]}
Jiang Yang
[Email protected] scripts]# Name[1]=bo
[[email protected] scripts]# echo ${name[@]}
Jiang Bo Yang
1th Chapter Shellfunction1.1Defining Functions:
Jiang () {
Echo Nihao
}
Jiang means calling the function defined above
[Email protected] scripts]# sh test.sh
Nihao
1.2extended usage of functions:Add a command
[Email protected] scripts]# source test.sh
Nihao
[email protected] scripts]# Jiang
Nihao
1.3parameters of a function
Jiang () {
Echo Nihao $
}
Daya () {
Echo Buhao $
}
Jiang $
Daya $
[[Email protected] scripts]# sh test.sh 1
Nihao 1
Buhao 1
1.4 basenameUse of Commands
[Email protected] scripts]# basename/server/scripts/test.sh
test.sh
[Email protected] scripts]# basename/server/scripts/test.sh. sh
Test
2nd Chapter ShellDebugging Tips: 2.1Power -down commissioning:
Jiang () {
Echo Nihao $
}
Daya () {
Echo Buhao $
}
Jiang
Exit Add exit where there may be a problem , not continue to execute script down , narrow down the scope of the investigation
Daya
2.2Use-XParameters:
[Email protected] scripts]# sh-x test.sh
+ Jiang
+ Echo Nihao
Nihao
+ Daya
+ Echo Buhao
Buhao
2.3specifying the debug scope:
[Email protected] scripts]# sh test.sh
Nihao
+ Daya
+ Echo Buhao
Buhao
+ Set +x
The third of the shell