1, print the system load every 3 seconds
#!/bin/bashwhiletruedo uptime sleep3 Done
2, save the monitoring results to a file, execute in the background, and then monitor the file changes with Tail-f
SH while. sh &[112867
#!/bin/bashwhiletruedo uptime >> log.txt Sleep 3 Done
[Email protected]:~/linux/shell/flow_control$Tail-F Log.txt .: -: +Up -Min1User, load Average:0.33,0.35,0.32 .: -: *Up -Min1User, load Average:0.33,0.35,0.32 .: -: -Up -Min1User, load Average:0.31,0.34,0...
3, process scheduling related commands
FG: The current script or task is placed in the foreground for execution. If you specify a task: FG task number. Task number through Jobs query
BG: Put the task in the background to execute
Jobs: View the currently executing script or task
CTRL + Z: Pause execution of current script
SH while1.sh &: Plus &, representing background execution script
[Email protected]:~/linux/shell/flow_control$ FGSH while.SH^z[1]+ StoppedSH while.SH[email protected]:~/linux/shell/flow_control$ jobs[1]+ StoppedSH while.SH[email protected]:~/linux/shell/flow_control$ bg[1]+SH while.SH&[email protected]:~/linux/shell/flow_control$ jobs[1]+ RunningSH while.SH&[email protected]:~/linux/shell/flow_control$SH while.SH&[2]13411[email protected]:~/linux/shell/flow_control$ jobs[1]-RunningSH while.SH&[2]+ RunningSH while.SH&[email protected]:~/linux/shell/flow_control$ FGSH while.SH^z[2]+ StoppedSH while.SH[email protected]:~/linux/shell/flow_control$ bg[2]+SH while.SH&[email protected]:~/linux/shell/flow_control$ jobs[1]-RunningSH while.SH&[2]+ RunningSH while.SH&
4, print with while loop 0, 1, 2, 3, 4
#!/bin/Bashi=0while5 ]do echo $i ((i + + )) Done
The two brackets can also be
#!/bin/Bashi=0while5 ] "do Echo $i ((i+ + )) Done
You can also use the calculation expression
#!/bin/Bashi=0while5 )The Do echo $i ((i + + )) Done
5, calculate 1 .... 100 of and
[Email protected]:~/linux/shell/flow_control$SH sum.SH 1+2+3.. + -=5050[email protected]:~/linux/shell/flow_control$Cat sum.SH #!/bin/Bashi=1sum=0 while((I <= - )) Do (( sum=sum+i)) ((I++ )) DoneEcho "1+2+3..+100="${sum}
6, Guess the numbers
#!/usr/bin/Bashsum=$ ((random%Wuyi))Echo "the number you need to guess is:"$sumSleep 1Echo "Please enter a number between 1-50 and start guessing!"Count=0functiontype_num () {Read-P"Please enter a number:"NExpr$n +1&>/dev/NULL if[$?-ne0]; Then Echo "Please enter a number"type_numfi}functionguess () {(Count++ )) if[$n-eq $sum]; Then Echo "you guessed it, your number is:"${count}if[$count-lt3]; Then Echo "you're so awesome." elif[$count-ge3-A $count-lt6]; Then Echo "It's still good, come on ." Else Echo "you got a little water." fiExit0 elif[$n-GT $sum]; Then Echo "big guess."type_numElse Echo "small guess."type_numfi}functionMain () {type_num while true DoGuess Done}main
Linux shell script Programming while statement case