1. Position variable
Script:
[Email protected] sh]# cat/opt/sh/test1.sh
#/bin/bash
echo "The script name and path you executed are:" $
The first parameter of the echo "script is:" $
The second parameter of the echo "script is:"
The third parameter of the echo "script is:" $
The fourth parameter of the echo "script is:" $4
The fifth parameter of the echo "script is:" $
echo "Script has a number of parameters:" $#
echo "Script five parameters are:" [email protected]
Execution results
[[email protected] sh]#/opt/sh/test1.sh 1 2 3 4 5 6 7 8
The script name and path you executed are:/opt/sh/test1.sh
The first parameter of a script is: 1
The second parameter of the script is: 2
The third parameter of the script is: 3
The fourth parameter of the script is: 4
The fifth parameter of the script is: 5
The number of arguments to the script is: 8
The five parameters of the script are: 1 2 3 4 5 6 7 8
2.If statement
Script:
[email protected] sh]# cat test2.sh
#!/bin/bash
A=$1
If [$a-eq 0];then
echo "A is 0"
elif [$a-eq 1];then
echo "A is 1"
elif [$a-eq 2];then
Echo "A is 2"
Else
echo "A is not B"
Fi
Execution results
[Email protected] sh]#/test2.sh 0
A is 0
[Email protected] sh]#
[Email protected] sh]#/test2.sh 1
A is 1
[Email protected] sh]#/test2.sh 2
A is 2
[Email protected] sh]#/test2.sh 3
A is not B
3. While loop
Script
[email protected] sh]# cat test3.sh
#!/bin/bash
I=0
While [$i-ne 10]
Do
i=$ (($i + 1))
Echo $i
Done
Execution results
[Email protected] sh]#./test3.sh
1
2
3
4
5
6
7
8
9
10
4. For loop
Usage One:
#!/bin/bash
For ((i=1;i<=10;i++));
Do
echo $ (Expr $i \* 3 + 1);
Done
Usage Two:
#!/bin/bash
For I in $ (SEQ 1 10)
Do
echo $ (Expr $i \* 3 + 1);
Done
Shell Learning (iii)