1 变量
1.1 变量规则
1.2 定义与使用变量
练习:
$your_name="abc"
$echo $your_name
abc
练习:
$a="123"
$readonly a
实战1:
$a=10 //定义变量
$echo $a //输出变量
10
$unset a //删除变量
$echo $a
$b=10
$readonly b //把b设置为制度变量
$echo $b
10
$unset b //删除只读变量
-bash: unset: b: cannot unset: readonly variable //只读变量在当前终端不能被删除
重新打开一个终端后再输出b
$echo $b
练习:
$your_name="hogwarts"
$echo $your_name
hogwarts
$greeting="hello,"$your_name""
$echo $greeting
hello,hogwarts
$array_name=(value0 value1 value2 value3)
$valuen=${array_name[n]}
$array_name[0]=value0
实战2:
$my_array=(A B "C" D) //定义数组
$echo ${my_array[0]} //输出数组里的某个值
A
$echo ${my_array[1]}
B
$echo ${my_array[2]}
C
$echo ${my_array[3]}
D
$echo ${my_array[*]} //输出所有
A B C D
$echo ${my_array[@]} //输出所有
A B C D
$b[0]=1 //定义单个数组
$echo ${b[0]} //输出此数组
1
$b[1]=2
$echo ${b[1]}
2
$echo ${b[2]} //未定义的数组,输出为空
2 控制语句
2.1 if
练习:
$if [ 2==2 ];then echo "true";else echo "false";fi
true
$if [[ 2 > 1 ]];then echo "true";else echo "false";fi
true
实战1:
$a=10
$b=9
$if [ $a -eq $b ];then echo "equal";elif [ $a -gt $b ];then echo "big";elif [ $a -lt $b ];then echo "small";fi
big
2.2 for
实战:
$cat test.txt
1
2
3
4
5
]$for i in $(cat test.txt); do echo $i; done
1
2
3
4
5
2.3 while
实战
实战:
[50485581@shell.testing-studio.com ~]$cat test.txt
1
2
3
4
5
[50485581@shell.testing-studio.com ~]$while read i;do echo $i;done < test.txt
1
2
3
4
5