Judgment statement:
If judging condition
Then
Statement
[Elif]
Statement
...
[Else
Statement
Fi
#!/bin/bash
If [$#-eq 0]
Then
echo "Error"
echo "can use this command like command file_name"
Exit 4
Fi
Type= ' File $ | Cut-d '-f2 '
Echo is a $TYPE
Case statement:
Case Test value in
Option 1)
...
;;
Option 2)
...
;;
...
*)
...
Esac
#!/bin/bash
# This was a simple test in case
#
Case $# in
0)
echo "No option"
;;
1)
echo "You used $# option"
;;
2)
echo "You use $# options"
;;
*)
echo "Options is too ..."
Esac
Looping statements:
for loop:
for I in parameter probability list
do
loop body
done
#!/bin/bash
#
declare-i sum=0 ;
declare-i i;
For I in {1..100}
Do
Let sum+= $I;
Done
echo "SUM = $SUM"
List-to-build:
{M... n}: List of integers m to n
Seq Start number Step length end number: The default step length is 1, the starting number is 1, the starting number, step length can be omitted, the end number cannot be omitted
While loop:
While loop condition
Do
Loop body
Dione
#!/bin/bash
#
Declare-i sum=0;
Declare-i I=1;
While [$I-le 100]
Do
Let sum+= $I
Let i++
Done
echo "SUM = $SUM"
Until cycle:
Until cycle conditions
Do
Loop body
Done
#!/bin/bash
#
echo "Enter Username:"
Read user
Until who | grep "$user" &>/dev/null
Do
Sleep 30
Done
echo "$user is on"
For a while loop, the loop will continue as long as the loop condition is met
For until loops, loops are executed as long as the loop condition does not end successfully
In fact, the until loop is less useful than a while loop, but it can be helpful if you're waiting for an event to happen.
Break: Used to exit a loop
Continue: For early start of the next repeating cycle
Both the break and Continue commands accept optional numeric parameters, which can be used to indicate how many loops are included to be interrupted or resumed.
Shift: When it is used to handle command-line arguments, one (or more bits) is shifted to the left at a time, and when the shift is executed, the original $ $ will disappear, replaced by the
$ $ to replace, and so on, and $ #的值也会逐次减少, shift also accepts an optional parameter, that is, a few moves at a time, the default is 1
Shell selection statement, loop statement