一起来学Shell脚本编程(五)

简介: 一起来学Shell脚本编程(五)

前言

目前正在出一个Shell脚本编程系列教程, 篇幅会较多, 喜欢的话,给个关注❤️ ~

前面我们学习了Linux的一些基本命令,需要我们在终端手动去执行,但在处理一些复杂的操作就显得有点乏力,需要不停的执行,还很容易出错。有没有一种办法,可以帮助我们执行这一些列操作,方法当然有,那就是通过脚本去解决。

作为服务端开发,shell脚本编程还是要掌握一下的,可以做做基础性的运维,提高工作效率。

本系列主要以Linux Bash为主 ,好了, 废话不多说直接开整吧~

if

条件判断, 语法:

if [ expression ]
then
   Statement(s) to be executed if expression is true
fi

之前给大家提到过,同时它也支持else if,但是在shell语法中它是这么写的:

if ... elif ... else ... fi 

下面一起看个例子:

a="1"
if [ $a -eq "2" ]
then
    echo "2222"
elif [ $a -eq "1" ]
then
    echo "1111"
else
    echo ""
fi        

结果:

[root@iZ2ze5vrnucj8nu52fq932Z shell]# ./if.sh
1111
[root@iZ2ze5vrnucj8nu52fq932Z shell]# 

if .... else 也可以写成一行,之前给大家介绍过了

case

分条件控制,它跟我们熟知的switch case差不多,下面我们直接看一个例子:

a="1"
case $a in
    "1")
         echo "1111";;
    *) 
        echo "";;
esac

输出:

[root@iZ2ze5vrnucj8nu52fq932Z shell]# ./switch.sh
1111
[root@iZ2ze5vrnucj8nu52fq932Z shell]# 

这里的;;类似break

getopts

case经常会和getopts结合使用,下面我们通过一个例子给大家展示一下通过命令行参数去执行不同的命令:

if [ -z $1 ];then
    echo "command options can not null"
fi
while getopts 'a:b:c' OPT; do
    case $OPT in
        a)
            echo "option is a";;
        b)
            echo "option is b";;   
        c)
            echo "option is c";;
        *)
            echo "option is $OPT";;
    esac               
done

输出:

[root@iZ2ze5vrnucj8nu52fq932Z shell]# ./switch.sh
command options can not null
[root@iZ2ze5vrnucj8nu52fq932Z shell]# ./switch.sh -a 1 -b 2 -c 3
option is a
option is b
option is c
[root@iZ2ze5vrnucj8nu52fq932Z shell]# ./switch.sh -f 111
./switch.sh: illegal option -- f
option is ?
[root@iZ2ze5vrnucj8nu52fq932Z shell]# 

for

循环控制,语法:

for var in list
do
    ...
done

下面一起看个例子:

for v in 1 2 3 4 5 6 7
do
    echo "value is $v"
done    

输出:

[root@iZ2ze5vrnucj8nu52fq932Z shell]# ./for.sh
value is 1
value is 2
value is 3
value is 4
value is 5
value is 6
[root@iZ2ze5vrnucj8nu52fq932Z shell]# 

遍历目录:

for FILE in *
do
    echo $FILE
done    

输出:

[root@iZ2ze5vrnucj8nu52fq932Z shell]# ./for.sh
arr.sh
a.txt
b.txt
exp.sh
for.sh
hello.txt
if.sh
read.sh
str.sh
switch.sh
test1.sh
test.sh
var.sh
[root@iZ2ze5vrnucj8nu52fq932Z shell]# 

while

同样也是循环控制,语法:

while 条件
do
   ...
done

下面一起看个例子:

a=0
while [ $a -lt 5 ]
do 
  echo "a is $a"
  a=`expr $a + 1`
done

输出:

[root@iZ2ze5vrnucj8nu52fq932Z shell]# ./while.sh
a is 0
a is 1
a is 2
a is 3
a is 4
[root@iZ2ze5vrnucj8nu52fq932Z shell]# 

break

跳出循环

a=0
while [ $a -lt 5 ]
do
  if [ $a -eq 3 ];
  then
    a=`expr $a + 1`
    break
  fi
  echo "a is $a"
  a=`expr $a + 1`
done

结果输出:

[root@iZ2ze5vrnucj8nu52fq932Z shell]# ./while.sh
a is 0
a is 1
a is 2
[root@iZ2ze5vrnucj8nu52fq932Z shell]# 

可以看到当a=3的时候循环终止,这个跟其它语言的break功能一致

continue

break怎么能少得了continue呢,这个跟我们熟知的continue一样

a=0
while [ $a -lt 5 ]
do
  if [ $a -eq 3 ];
  then
    a=`expr $a + 1`
    continue
  fi
  echo "a is $a"
  a=`expr $a + 1`
done

输出结果:

[root@iZ2ze5vrnucj8nu52fq932Z shell]# ./while.sh
a is 0
a is 1
a is 2
a is 4
[root@iZ2ze5vrnucj8nu52fq932Z shell]# 

a=3的时候跳过了echo

结束语

大家平时在工作的时候就可以灵活运用起来了,多用用就熟悉了,大部分项目中都会用到脚本帮助我们去做一些自动化的工作,从而提高效率,还不容易出错~

本着把自己知道的都告诉大家,如果本文对有所帮助,点赞+关注鼓励一下呗~

相关文章
|
6天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
6天前
|
Shell Linux
Linux shell编程学习笔记30:打造彩色的选项菜单
Linux shell编程学习笔记30:打造彩色的选项菜单
|
4天前
|
Shell Linux 开发工具
|
4天前
|
监控 Unix Shell
shell脚本编程学习
shell脚本编程
22 12
|
6天前
|
Shell Linux
Linux shell编程学习笔记82:w命令——一览无余
Linux shell编程学习笔记82:w命令——一览无余
|
7天前
|
Shell
shell脚本变量 $name ${name}啥区别
shell脚本变量 $name ${name}啥区别
|
10天前
|
人工智能 监控 Shell
常用的 55 个 Linux Shell 脚本(包括基础案例、文件操作、实用工具、图形化、sed、gawk)
这篇文章提供了55个常用的Linux Shell脚本实例,涵盖基础案例、文件操作、实用工具、图形化界面及sed、gawk的使用。
26 2
|
1月前
|
Shell
Shell脚本有哪些基本语法?
【9月更文挑战第4天】
43 17
|
1月前
|
存储 Unix Shell
shell脚本编程基础
【9月更文挑战第4天】
36 12
|
29天前
|
网络协议 关系型数据库 MySQL
Shell 脚本案例
Shell 脚本案例
36 8