想要成为一名优秀的运维开发工程师,务必要掌握shell脚本的编写。这里整理一下shell脚本的常用语法,方便自己使用。
基本结构
我们从一个hello脚本了解一个shell脚本的基本结构
#!/bin/bash
# show hello world to you
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "Hello World! \a \n"
exit 0
- 第一行是用来说明该脚本运行的环境
- 第二行一般是用来说明该脚本的主要内容
- 主要环境变量的声明
- export命令使得环境变量在子shell中可用
前4行一般是一个shell脚本中的固定内容,当然了,实际都可以省略,但是一个规范的shell脚本中最好都写上。
接下来的echo那一行,就是我们这个脚本的主程序。
最后一行是执行结果,正常结束返回0
脚本运行
有三种方式可以运行脚本
运行命令 | 区分 | 备注 |
---|---|---|
source hello.sh 或者. hello.sh | 在当前shell中运行 | 只有用source运行的脚本,环境变量可以影响到当前shell |
sh hello.sh | 通过shell解释器运行 | |
./hello.sh | 作为可执行文件运行 | 需要用chmod授予运行权限 |
输入输出
shell通过read -p 获取输入
read -p "请输入你的姓:" lastname # 提示输入
read -p "请输入你的名:" firstname
echo -e "\n你的完整名字是: ${lastname} ${firstname}" # 打印输出
以下例子可以处理无输入的情况
read -p "请输入文件名称" userinput # 提示用户输入
filename=${userinput:-"filename"} # 如果无输入,使用默认值filename
拼接字符串
在shell中,可以直接拼接字符串
part1="Hello"
part2="World"
result="${part1} ${part2}"
echo "$result"
数值运算
bash shell仅支持整数运算,注意下面例子有两个括号
numa=1
numb=2
echo $(($numa+$numb))
判断文件是否存在
filename="example.txt"
if [ -e "$filename" ]; then
echo "文件 $filename 存在。"
else
echo "文件 $filename 不存在。"
fi
其他常用参数
-e 路径是否存在
-f 路径是否是普通文件(不是文件夹
-d 路径是否是文件夹
判断文件执行权限
-r 是否具有读权限
-w 写权限
-x 执行权限
shell脚本中的参数
如果执行脚本时,需要传入参数,可以像下图这样写
/文件/具体/路径.sh opt1 opt2 opt3 opt4
$0 $1 $2 $3 $4
默认的参数就是脚本名称本身,
echo "本shell脚本的名字是 ==> ${0}"
echo "参数的总个数是 ==> $#"
[ "$#" -lt 2 ] && echo "参数总数小于两个" && exit 0
echo "全部参数是 ==> ‘$@’"
echo "第一个参数是 == > ${1}"
echo "第二个参数是 == > ${2}"
逻辑判断语句if
if判断语句的结构是如下这样,当然了,如无需要,elif和else可以没有
if [ 判断条件1 ]; then
条件1成立时,执行的语句
elif [ 判断条件2 ]; then
条件2成立时,执行的语句
else
以上条件都不成立时执行
fi
举例,如下语句判断本机开放的端口。如果无法使用netstat命令,可以用yum install net-tools来安装
echo "即将进行端口检测..."
testfile=/root/bin/net_check.txt
netstat -tuln > ${testfile}
testing=$(grep ":80 " ${testfile})
if [ "${testing}" != "" ]; then
echo "80端口是开放的。"
fi
testing=$(grep ":22 " ${testfile})
if [ "${testing}" != "" ]; then
echo "22端口是开放的。"
fi
testing=$(grep ":25 " ${testfile})
if [ "${testing}" != "" ]; then
echo "25端口是开放的。"
fi
case…esac语句
case语句在python里没有,在其他语言也挺常见的,shell中的case语句基本语法如下:
case $变量名称 in
"变量1")
执行的内容
;;
"变量2")
执行的内容
;;
esac <== 表示结束case语句
函数function
shell中的函数基本语法如下:
function funcname() {
......
}
例:
function printit() {
echo -n "你的选择是:"
}
echo "本程序将会打印你的输入"
case ${1} in
"one")
printit; echo ${1} | tr 'a-z' 'A-Z'
;;
"two")
printit; echo ${1} | tr 'a-z' 'A-Z'
;;
*)
echo "仅支持如下参数: ${0} {one|two}"
;;
esac
while do done 和 until do done
当你不确定循环要做多少次的时候,一般使用while循环,语法如下
while [循环条件]
do
执行语句
done
shell中还有一种until循环,意思是 当满足制定条件时,终止循环;否则一直执行。
until [循环终止条件]
do
执行语句
done
for…do…done循环
已知要进行几次循环的情况下,一般使用for循环,基本语法如下
for var in con1 con2 con3 ...
do
执行程序
done
例子,检测本地占用的ip地址
network="192.168.32"
for ip in $(seq 1 20)
do
ping -c 1 -w 1 ${network}.${ip} &> /dev/null && result=0 || result=1
if [ "${result}" == 0 ]; then
echo "IP ${network}.${ip} 在使用中"
else
echo "IP ${network}.${ip} 未占用"
fi
done
for…done的另一种写法
其实就类似其他语言的 for(i=0, i<10,i++)
for (( 初始值; 限制值; 赋值运算))
do
程序
done
例如累加运算
read -p "请输入一个整数,我将计算1+2+..到n的值:" num
s=0
for (( i=1; i<=${num}; i=i+1 ))
do
s=$((${s}+${i}))
done
echo "1+2+...+${num}的计算结果是:${s}"