一、判断用户的参数(两种方式)
配合参数**$?**返回结果:正确返回0 错误返回非0
1、test命令(脚本中不用)
实例:
[root@localhost ~]# test -d test.sh
[root@localhost ~]# echo $?
1
2、[ ](脚本中常用)
[root@localhost ~]# [ -f test.sh ]
[root@localhost ~]# echo $?
0
3、逻辑运算
- && : 与,当两边同时成立,则返回0;只有前面正确,才会继续判断后边的;
[root@localhost ~]# [ -f test.sh ] && [ -d /etc ]
[root@localhost ~]# echo $?
0
- ||: 或,当两边有一个成立,则返回0;前面错误,仍然可以继续判断后面的内容;
[root@localhost ~]# [ -f test.sh ] || [ -d /etc ]
[root@localhost ~]# echo $?
0
- !:非,如果判断是错的,返回是0
[root@localhost ~]# [ ! -f test.sh ]
[root@localhost ~]# echo $?
1
综合实例:
[root@localhost ~]# USER=root
[root@localhost ~]# [ $USER != root ] && echo "user" || echo "root"
root
4、运算符
注意:用于区分之前的重定向,如:<,>等符号,只针对数字,正确为空:0 ;错误为不空:非0。
[root@localhost ~]# [ 5 -lt 6 ]
[root@localhost ~]# echo $?
0
[root@localhost ~]# A=12345
[root@localhost ~]# B=2345
[root@localhost ~]# [ A = B ]
[root@localhost ~]# echo $?
1
[root@localhost ~]# [ -z A ]
[root@localhost ~]# echo $?
1
二、流程控制语句
1、判断
if判断: 分为单分支(一次判断),双分支(两次),多分支(更加精细)
单分支(如果 ,那么):
实例:
#!/bin/bash
DIR=$1
if [ ! -e $DIR ]
then mkdir -p $DIR
fi
[root@localhost ~]# ./test.sh /thinkmo //thinkmo为$1参数
双分支(如果,那么,要不然):
实例:
#!/bin/bash
DIR=$1
if [ ! -e $DIR ]
then mkdir -p $DIR
else
echo "$DIR existed"
fi
[root@localhost ~]# ./test.sh /thinkmo
/thinkmo existed
多分支:
实例:
#!/bin/bash
read -p "Input a number:" NUM
if [ $NUM -ge 90 ] && [ $NUM -le 100 ];
then echo "excellent!"
elif [ $NUM -ge 80 ] && [ $NUM -le 89 ];
then echo "Good!"
elif [ $NUM -ge 60 ] && [ $NUM -le 79 ];
then echo "pass!"
else
echo "fail!"
fi
备注:read用于从键盘读取一个你输入的值,-p 打印输出后面的提示语句
2、循环
- for循环
实例:(批量创建用户和密码)
#!/bin/bash
#2019年3月19日16:15:51
#Autor by zt
#create users
read -p "Input password:" PASSWD
for USER_NAME in `cat users.txt` //反引号``:反引号里的内容 如果有命令,则当作命令去执行
do
useradd $USER_NAME
echo "$PASSWD" | passwd --stdin $USER_NAME
done
- while循环(默认当你的条件不满足时才会退出)
实例(猜数字):
#!/bin/bash
#2019年3月19日17:29:49
PRICE=$(expr $RANDOM % 1000)
while true
do
read -p "input a num:" NUM
if [ $NUM -gt $PRICE ];
then echo "Big"
elif [ $NUM -lt $PRICE ];
then echo "Sma"
else echo "Right"
exit 0 //返回正确结果并退出
fi
done
- case判断语句(脚本执行的时候使用)
实例:
#!/bin/bash
#2019年3月20日09:17:21
#Autor by zt
case $1 in
[a-z] | [A-Z])
echo "字母"
;;
[0-9])
echo "数字"
;;
*)
echo "特殊字符"
;;
esac
[root@localhost ~]# ./case.sh 1
数字
[root@localhost ~]# ./case.sh a
字母
[root@localhost ~]# ./case.sh A
字母
[root@localhost ~]# ./case.sh
特殊字符
三、计划任务(定时任务)crcrond
注意:命令要写绝对路径 ,从/开始写 命令在哪个路径下which定位
表示形式:
- */5 每五分钟(小时、天、月、周几)
- 15-20 每个小时的第15-20分钟
- 日期和星期是不能同时使用的,避免冲突,而且分钟字段必须有数值空的位置用 * 表示
实例:
[root@localhost ~]# crontab -l -u thinkmo //创建用户thinkmo的定时任务
5 10 * * * /usr/bin/rm -rf /opt/* //每天的10点5分 删除/opt下的所有文件
[root@localhost ~]# crontab -l -u thinkmo //查看thinkmo用户所执行的定时任务
5 10 * * * /usr/bin/rm -rf /opt/*
课后实例:
1、自动删除test.txt文件脚本,命名脚本名称为auto_cp.sh,脚本的功能实现从/root/目录cp拷贝test.txt到/tmp目录,并且在/tmp目录创建一个目录abc,并且删除原/root/test.txt。
执行前
[root@localhost ~]# mkdir test.txt
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1458 Mar 2 09:58 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Mar 22 08:51 test.txt
[root@localhost ~]# ll /tmp/
total 0
drwx------. 3 root root 17 Mar 22 08:26 systemd-private-2f754dbf50ac465290b6c68f9d1c9e41-vmtoolsd.service-NEd8Qt
drwx------. 3 root root 17 Mar 22 08:31 systemd-private-6fd456c132594a61b847f194ee698c2b-vmtoolsd.service-mU1ZpX
drwx------. 3 root root 17 Mar 21 10:22 systemd-private-effb7a2e2db14b79a46bfc6e6fa6ed99-vmtoolsd.service-T4xcSs
[root@localhost ~]#
编辑脚本
[root@localhost ~]# vim auto_cp.sh
#!/bin/bash
#从/root/目录cp拷贝test.txt到/tmp目录,并且在/tmp目录创建一个目录abc,并且删除原/root/下test.txt
#2019年3月22日21:23:24
if [ ! -e /tmp/test.txt ]
then cp -a test.txt /tmp/
echo "复制test.txt文件"
fi
if [ ! -e /tmp/abc ]
then mkdir -p /tmp/abc
echo "在tmp目录创建abc文件"
fi
if [ -e /root/test.txt ]
then rm -rf test.txt
echo "删除test.txt文件"
fi
[root@localhost ~]# chmod +x auto_cp.sh
执行后
[root@localhost ~]# ./auto_cp.sh
复制test.txt文件
在tmp目录创建abc文件
删除test.txt文件
[root@localhost ~]# ll
total 8
-rw-------. 1 root root 1458 Mar 2 09:58 anaconda-ks.cfg
-rwxr-xr-x. 1 root root 440 Mar 22 09:33 auto_cp.sh
[root@localhost ~]# ll /tmp/
total 0
drwxr-xr-x. 2 root root 6 Mar 22 09:33 abc
drwx------. 3 root root 17 Mar 22 08:26 systemd-private-2f754dbf50ac465290b6c68f9d1c9e41-vmtoolsd.service-NEd8Qt
drwx------. 3 root root 17 Mar 22 08:31 systemd-private-6fd456c132594a61b847f194ee698c2b-vmtoolsd.service-mU1ZpX
drwx------. 3 root root 17 Mar 21 10:22 systemd-private-effb7a2e2db14b79a46bfc6e6fa6ed99-vmtoolsd.service-T4xcSs
drwxr-xr-x. 2 root root 6 Mar 22 08:51 test.txt
2、批量创建10个系统帐号thinkmo01-thinkmo10并设置密码(密码为随机数,要求字符和数字等混合)。
#!/bin/bash
#批量创建用户名和随机密码
#2019年3月22日23:02:19
for i in `seq 10`
do
useradd thinkmo$i
password=`cat /dev/urandom | head -n 5 | md5sum | head -c 8`
echo $password|passwd --stdin thinkmo$i
echo "thinkmo$i的密码是:$password"
done
[root@localhost ~]# ./a.sh
Changing password for user thinkmo1.
passwd: all authentication tokens updated successfully.
thinkmo1的密码是:7db68d1f
Changing password for user thinkmo2.
passwd: all authentication tokens updated successfully.
thinkmo2的密码是:23b0544a
Changing password for user thinkmo3.
passwd: all authentication tokens updated successfully.
thinkmo3的密码是:c7d19003
Changing password for user thinkmo4.
passwd: all authentication tokens updated successfully.
thinkmo4的密码是:662ef349
Changing password for user thinkmo5.
passwd: all authentication tokens updated successfully.
thinkmo5的密码是:349e7e3a
Changing password for user thinkmo6.
passwd: all authentication tokens updated successfully.
thinkmo6的密码是:46e4315b
Changing password for user thinkmo7.
passwd: all authentication tokens updated successfully.
thinkmo7的密码是:ce36275c
Changing password for user thinkmo8.
passwd: all authentication tokens updated successfully.
thinkmo8的密码是:f05f298d
Changing password for user thinkmo9.
passwd: all authentication tokens updated successfully.
thinkmo9的密码是:6dc473a0
Changing password for user thinkmo10.
passwd: all authentication tokens updated successfully.
thinkmo10的密码是:7de03d3f
3、猜数字游戏
脚本生成一个 100 以内的随机数,提示用户猜数字,根据用户的输入,提示用户猜对了, 猜小了或猜大了,直至用户猜对脚本结束。RANDOM 为系统自带的系统变量,值为 0‐32767的随机数,使用取余算法将随机数变为 1‐100 的随机数
#!/bin/bash
#猜大小正确结束退出
#2019年3月22日23:09:58
PRICE=$(expr $RANDOM % 100)
while true
do
read -p "input a num:" NUM
if [ $NUM -gt $PRICE ];
then echo "Big"
elif [ $NUM -lt $PRICE ];
then echo "Sma"
else echo "Right"
exit 0
fi
done
[root@localhost ~]# ./b.sh
input a num:23
Sma
input a num:88
Big
input a num:56
Sma
input a num:60
Big
input a num:57
Sma
input a num:58
Right
[root@localhost ~]#