当前位置:网站首页>Shell基本语法--流程控制
Shell基本语法--流程控制
2022-06-29 17:59:00 【__万波__】
if else 语句
基本结构
注意:
if和elif语句后面要跟then才能开始执行语句块then可以换行写, 写在同一行要加分号隔开, 建议写在同一行, 结构会更清晰- 没有 else if 这种语句, 只能写
elif
if [ ]; then
# if 语句块
elif [ ]; then
# 语句块
else
# 语句块
fi
条件运算符
[]与[[]]的不同之处
[[]]运算符只有bash中才有, 其他shell如ash, dash中是没有这个语法的[[]]有[]中没有的功能, 比如比较两个字符串是否相等, 且字符串中有空格的时候, 使用[]时必须加双引号而使用if [ "$str" = "hello man" ]; then[[]]时就不必担心这个问题if [[ $str = "hello man" ]]; then- 逻辑运算. 使用
[]条件运算符时,&&,||,<,>这几个符号都会被当作命令的一部分, 而不是当作逻辑运算符, 使用[[]]条件运算符时就不会有这个问题, 如:# 下面的 > 符号会被当作重定向命令, && 也作为命令的一部分, 会创建200 和 100两个文件, 且语句会报错: [: missing `]' if [ 300 > 200 && 200 > 100 ]; then # 下面的语句就可以正常的执行逻辑运算 if [[ 300 > 200 && 200 > 100 ]]; then- 模糊匹配. 使用
[[]]时可以使用=~运算符及正则语法实现模糊匹配如匹配yes:# 使用 [] 时只能使用如下写法 if [ $str = "y" -o $str = "yes" ];then # 使用 [[]] 时就可以有如下写法 if [[ $str =~ ^y(es)$ ]];then echo ${ BASH_REMATCH[1]} # 匹配到的值存在这个系统变量中 ... # 还可以使用下面的通配符写法 if [[ $str = y* ]];then- 经验, 一般做判断时会使用
[]条件运算符, 但在做字符串比较时, 最好用[[]]会降低出错概率
条件标识符
判断数字
if [ $a -eq $b ]; then # 等于 equal
if [ $a -ne $b ]; then # 不等于 not equal
if [ $a -gt $b ]; then # 大于 greater than
if [ $a -ge $b ]; then # 大于等于 greater equal
if [ $a -lt $b ]; then # 小于 little than
if [ $a -le $b ]; then # 小于等于 little equal
判断字符串
# 使用 [] 运算符
if [ -z "$str" ]; then # 判断为空(字符串变量加引号)
if [ -n "$str" ]; then # 判断不为空(字符串变量加引号)
if [ "$str" = "abc" ]; then # 判断相等(字符串变量加引号)
if [ "$str" == "abc" ]; then # 判断相等(字符串变量加引号)
if [ "$str" != "abc" ]; then # 判断不相等(字符串变量加引号)
if [ "$str1" \> "$str2" ]; then # 判断大于(字符串变量加引号)
if [ "$str1" \< "$str2" ]; then # 判断小于
# 使用 [[]] 运算符
if [[ -z $str ]]; then # 判断为空
if [[ -n $str ]]; then # 判断不为空
if [[ $str = "ab c" ]]; then # 判断相等
if [[ $str == "ab c" ]]; then # 判断相等
if [[ $str != "ab c" ]]; then # 判断不相等
if [[ $str1 > $str2 ]]; then # 判断大于
if [[ $str1 < $str2 ]]; then # 判断小于
文件判断
if [ -e $file ]; then # 判断文件存在
if [ -L $file ]; then # 判断文件存在且是链接文件(要最先判断, 否则会先去判断软链接指向的文件的其他属性)
if [ -s $file ]; then # 判断文件存在且是字符设备
if [ -b $file ]; then # 判断文件存在且是块设备文件
if [ -d $file ]; then # 判断文件存在且是文件夹/目录
if [ -f $file ]; then # 判断文件存在且是普通文件
if [ -S $file ]; then # 判断文件存在且是套接字文件
if [ -p $file ]; then # 判断文件存在且是管道文件
# 对文件权限的判断
if [ -r $file ]; then # 判断文件具有可读权限
if [ -w $file ]; then # 判断文件具有可写权限
if [ -x $file ]; then # 判断文件具有可执行权限
逻辑运算
# 使用 [] 时
if [ $a -o $b ];then # 或 or
if [ $a -a $b ];then # 且 and
if [ ! $a ];then # 非
if [ expression1 ] || [ expression2 ];then # 或
if [ expression1 ] && [ expression2 ];then # 且
if ! [ expression ] # 非
# 使用 [[]]时
if [[ $a && $b ]];then # 或
if [[ $a && $b ]];then # 且
if [[ ! $a ]];then # 非
if [[ expression1 ]] || [[ expression2 ]];then # 或
if [[ expression1 ]] && [[ expression2 ]];then # 且
if ! [[ expression ]] # 非
switch case 选择
case ${var} in
[1-9]) # 范围
# do something
;;
[^a-zA-Z]) # 非此范围
# do something
;;
*)
#do something
;;
esac
select in 选择
select var in ch_a ch_b ch_c ch_d
do
#经常结合case in 语句使用
done
while 循环
while test
do
# 循环体
done
while true # 死循环的写法
do
done
for 循环
for ((i=0; i<=100; i++))
do
# 循环体
done
for var # 不加任何参数会循环所有的位置参数
do
done
for in 循环
for var in a b c d e
do
#
done
for var in {
1..100}
do
#
done
break 与 continue
shell 中的break和continue与普通C系列程序中的同名关键词有着不同的行为
break n: 退出n层循环continue n: 跳过n层循环, 执行
边栏推荐
- VMware安装ESXI
- Sister Juan takes you to learn database -- 5-day sprint Day1
- Mysql database daily backup and scheduled cleanup script
- Yolov6+tensorrt+onnx: deployment based on win10+tensorrt8+yolov6+onnx
- Adobe Premiere基础-炫酷文字快闪(十四)
- Adobe Premiere foundation - sound adjustment (volume correction, noise reduction, telephone tone, pitch shifter, parameter equalizer) (XVIII)
- Serial port experiment based on stm32f103zet6 library function
- Lodash deep copy usage
- Maximum length of palindrome substring (string hash + binary)
- 3H proficient in opencv (VII) - color detection
猜你喜欢

Adobe Premiere基础-声音调整(音量矫正,降噪,电话音,音高换挡器,参数均衡器)(十八)

自动化软件测试 - 利用短信转发器结合Selenium读取短信验证码

Issue 42: is it necessary for MySQL to have multiple column partitions

Proxmox VE Install 7.2

Adobe Premiere基础-编辑素材文件常规操作(脱机文件,替换素材,素材标签和编组,素材启用,便捷调节不透明度,项目打包)(十七)

Bloom filter:

Have you grasped the most frequently asked question in the interview about massive data processing?

ABC253 D FizzBuzz Sum Hard(容斥定理)

Premature end of script headers 或 End of script output before headers

【网络是怎么连接的】第三章 探索集线器,交换机和路由器
随机推荐
PostgreSQL database system table
源码安装MAVROS
最长异或路径(dfs+01trie)
When easycvr deploys a server cluster, what is the reason why one is online and the other is offline?
3h精通OpenCV(五)-透视变换
Yolov6+tensorrt+onnx: deployment based on win10+tensorrt8+yolov6+onnx
Force deduction daily question 06.29 add two numbers
WBF:检测任务NMS后虑框新方式?
行程卡“摘星”热搜第一!刺激旅游产品搜索量齐上涨
Adobe Premiere基础-素材嵌套(制作抖音结尾头像动画)(九)
/usr/bin/ld: warning: **libmysqlclient. so. 20**, needed by //usr/
Adobe Premiere基础-时间重映射(十)
Adobe Premiere基础-不透明度(混合模式)(十二)
You can do sideline work
给定一个数在序列中求最大异或值(01字典)
Request header field xxxx is not allowed by Access-Control-Allow-Headers in preflight response问题
Relationship among controller, service and Dao
[tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction
让 Google 搜索到自己的博客
Software testing - you may not understand the basic theoretical knowledge