Shell 脚本
- 脚本声明
#!/bin/bash
- 脚本注释
# example script
- 脚本命令
uptime
free -h
reboot
- 脚本执行
$ cat xiaoxian.sh
#!/bin/bash
# xiaoxian.sh
# this script is test
# 2023-05-09
date
ls -l
free -h
uname -a
$ bash xiaoxian.sh
接收用户输入
$ cat haha.sh
#!/bin/bash
# haha.sh
echo $0
echo $1,$3,$5
echo $#,$*
$ bash haha.sh a b c d e f g
haha.sh
a,c,e
7,a b c d e f g
判断用户输入
- 文件
- 逻辑
- 整数
- 字符串
文件测试语句
- 文件包含一般、目录、设备(块)、链接、管道文件
-d
测试文件是否为目录类型-e
测试文件是否存在-f
判断是否为一般文件-r
测试当前用户是否有权限读取-w
测试当前用户是否有权限写入-x
测试当前用户是否有权限执行
$ [ -e /dev/fstab ]
$ echo $?
1
$ [ -e /etc/fstab ]
$ echo $?
0
$ [ -w /etc/fstab ]
$ echo $?
0
1
: 存在2
: 一般3
: 可以写入
逻辑测试语句
&&
与||
或!
非
[root@linuxprobe ~]# [ -e /etc/fstab ] && echo "ok"
ok
[root@linuxprobe ~]# su - linuxprobe
[linuxprobe@linuxprobe ~]$ [ $USER = root ] && echo "Admin" || echo "User"
User
[linuxprobe@linuxprobe ~]$ exit
logout
[root@linuxprobe ~]#[ $USER = root ]
[root@linuxprobe ~]# echo $?
0
[root@linuxprobe ~]# [ ! $USER = root ]
[root@linuxprobe ~]# echo $?
1
整数测试语句
-eq
是否等于-ne
是否不等于-gt
是否大于-lt
是否小于-le
是否小于等于-ge
是否大于等于
[root@linuxprobe ~]# [ 10 -gt 10 ]
[root@linuxprobe ~]# echo $?
1
[root@linuxprobe ~]# [ 10 -eq 10 ]
[root@linuxprobe ~]# echo $?
0
[root@linuxprobe ~]# [ `free -m | grep Mem | awk '{print $4}'` -lt 1024 ] && echo "mem less than 1024M" || echo "enough mem"
mem less than 1024M
[root@linuxprobe ~]# [ `free -m | grep Mem | awk '{print $4}'` -lt 100 ] && echo "mem less than 100M" || echo "enough mem"
enough mem
字符串测试语句
=
比较字符串内容是否相同!=
比较字符串内容是否不同-z
判断字符串内容是否为空
[root@linuxprobe ~]# [ -z $String ]
[root@linuxprobe ~]# echo $?
0
[root@linuxprobe ~]# echo $String
[root@linuxprobe ~]# [ $LANG != "en.US" ] && echo "Not en.US"
Not en.US
流程控制
if 条件测试语句
# 单分支
[root@linuxprobe ~]# cat haha.sh
#!/bin/bash
# haha.sh
if [ ! -d /dev/hahaha ]
then
mkdir -p /dev/hahaha
fi
[root@linuxprobe ~]# ls -ld /dev/hahaha
ls: cannot access '/dev/hahaha': No such file or directory
[root@linuxprobe ~]# bash haha.sh
[root@linuxprobe ~]# ls -ld /dev/hahaha
drwxr-xr-x. 2 root root 40 May 12 00:54 /dev/hahaha
# 双分支
[root@linuxprobe ~]# cat chkhost.sh
#!/bin/bash
# chkhost.sh
ping -c 3 -i 0.2 -W 3 $1 &> /dev/null
if [ $? -eq 0 ]
then
echo "$1 is online"
else
echo "$1 is offline"
fi
[root@linuxprobe ~]# bash chkhost.sh 127.0.0.1
127.0.0.1 is online
[root@linuxprobe ~]# bash chkhost.sh 192.168.10.10
192.168.10.10 is offline
# 多分支
[root@linuxprobe ~]# cat chkscore.sh
#!/bin/bash
# chkscore.sh
read -p "Enter your score (0-100): " GRADE
if [ $GRADE -gt 100 ] || [ $GRADE -lt 0 ]
then
echo "$GRADE is Error!"
elif [ $GRADE -ge 85 ] && [ $GRADE -le 100 ]
then
echo "$GRADE is Excellent"
elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ]
then
echo "$GRADE is Pass"
else
echo "$GRADE is Fail"
fi
[root@linuxprobe ~]# bash chkscore.sh
Enter your score (0-100): 60
60 is Fail
[root@linuxprobe ~]# bash chkscore.sh
Enter your score (0-100): 70
70 is Pass
[root@linuxprobe ~]# bash chkscore.sh
Enter your score (0-100): 90
90 is Excellent
[root@linuxprobe ~]# bash chkscore.sh
Enter your score (0-100): 101
101 is Error!
[root@linuxprobe ~]# bash chkscore.sh
Enter your score (0-100): -1
-1 is Error!
[root@linuxprobe ~]# bash chkscore.sh
Enter your score (0-100): aaaaa
chkscore.sh: line 4: [: aaaaa: integer expression expected
chkscore.sh: line 4: [: aaaaa: integer expression expected
chkscore.sh: line 7: [: aaaaa: integer expression expected
chkscore.sh: line 10: [: aaaaa: integer expression expected
aaaaa is Fail
for 语句
#!/bin/bash
# haha.sh
read -p "Enter: " PASSWD
for UNAME in `cat user.txt`
do
id $UNAME &> /dev/null
if [ $? -eq 0 ]
then
echo "$UNAME already existed."
else
useradd $UNAME &> /dev/null
echo $PASSWD | passwd --stdin $UNAME &> /dev/null
fi
done
#!/bin/bash
# haha.sh
for IP in `cat ip.txt`
do
ping -c 3 -i 0.2 -W 3 $IP &> /dev/null
if [ $? -eq 0 ]
then
echo "$IP is online"
else
echo "$IP is offline"
fi
done