Bash编程语法

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值