How to get started with Shell programming: how to get started with shell programming
Unlike Java, PHP, and other languages, the sh process control cannot be blank, for example:
Public class Test {public static void main (String [] args) {int a = 10; if (a> 5) {System. out. println ("a greater than 5");} else {// do not do anything }}}
This cannot be written in bash. If the else branch does not have statement execution, do not write this else.
If
Statement syntax format:
a=10b=20if [ $a == $b ]then echo "a is same to b."fi
If else
Statement syntax format:
a=10b=20if [ $a == $b ]then echo "a is same to b."else echo "a is not same to b."fi
If else-if else
Statement syntax format:
A = 10b = 20if [$ a = $ B] then echo "a equals B" elif [$ a-gt $ B] then echo "a greater than B" elif [$ -lt $ B] then echo "a less than B" else echo "no matching condition" fi
The output result is: a is smaller than B.
If else statements are often used in combination with the test Command, as shown below:
Num1 = $ [2*3] num2 = $[1 + 5] if test $ [num1]-eq $ [num2] then echo 'two numbers are equal! 'Else' two numbers are not equal! 'Fi
The output result is: two numbers are not equal.
For Loop
Output the numbers in the current list sequentially:
for loop in 1 2 3 4 5do echo "The value is : $loop"done
While statement
num=1while(( $num<=5 ))do echo $num let "num++"done
Bash uses the let command to execute one or more expressions. $ Variable is not required in variable calculation.
The while loop can be used to read the keyboard information. In the following example, the input information is set as the variable input, and the cycle is ended by pressing Ctrl + D.
Echo "press ctrl + D to exit" echo ". Enter the following information:" while read inputdo echo "your input is: $ input" done
Until Loop
Until executes a series of commands cyclically until the condition is true.
The Processing Method of the until loop is the opposite to that of the while loop.
num=5until(( $num>10 ))do echo $num let "num++"done
The result is output in sequence: 5, 6, 8, 9, and 10.
Case
You can use the case statement to match a value with a pattern. If the match succeeds, execute the matched command.
Echo 'Enter the information 'read aNumcase $ aNum in 1) echo 'you selected 1'; 2) echo' you selected 2'; 3) echo 'you selected 3'; "wsz") echo 'you selected wsz'; *) echo 'You did not enter a number between 1 and 4'; esac
The case method is shown in the preceding figure. The value must be followed by the word in, and each pattern must end with parentheses. The value can be a variable or constant. After matching finds that the value matches a certain mode, all commands are executed until; indicates break. The value will detect each matching mode. Once the mode matches, the matching mode command is executed and the other modes are not continued. If no matching mode exists, use asterisk * to capture the value and then execute the following command. Esac syntax is very different from C family syntax. It requires an esac (that is, the case in turn) as the end mark, and each case Branch uses; to represent break. Bounce cycle break
In the following example, the script enters an endless loop until the number entered by the user is not one of 1 2 3 4 5. To jump out of this loop, use the break command.
while :do read aNum case $aNum in 1|2|3|4|5) echo "your input is $aNum" ;; *) echo "game over" break ;; esacdone
Continue
The continue command is similar to the break command. There is only one difference. It does not jump out of all loops and only jumps out of the current loop.
for (( i=0 ; i<10 ; i++ ))do if [ $i == 3 ] then continue else echo $i fidone
The above results will be output: 0 1 2 4 5 6 7 8 9, not 3.