read命令键盘读取变量的值
从键盘读取变量的值,通常用在shell脚本中与用户进行交互的场合。该命令可以一次读取多个变量的值,变量和输入的值都需要使用空格隔开。在read命令后面,如果没有指定变量名,读取的数据将被自动赋值给特定的变量REPLY
read从键盘读入数据,赋给变量
[root@localhost ~]# read a b
hell word
[root@localhost ~]# echo $a $b
hell word
read常用见用法及参数
从标准输入读取一行并赋值给变量passwd
[root@localhost ~]# read passwd
读取多个值,从标准输入读取一行,直至遇到第一个空白符或换行符。把用户键入的第一个词存到变量first中,把该行的剩余部分保存到变量last中
[root@localhost ~]# read firt last
read -s passwd 将你输入的东西隐藏起来,值赋给passwd。这个用户隐藏密码信息
[root@localhost ~]# read -s passwd
[root@localhost ~]# echo $passwd
输入的时间限制
[root@localhost ~]# read -t 2 time #超过两秒没有输入,直接退出
输入的长度限制
[root@localhost ~]# read -n 2 test #最多只接受2个字符
使用-r参数输,允许让输入中的内容包括:空格、/、\、 ?等特殊字符串。
[root@localhost ~]# read -r line
sdf sdf / sdfs /n
[root@localhost ~]# echo $line
sdf sdf / sdfs /n
-p 用于给出提示符,在前面的例子中我们使用了echo –n “…“来给出提示符
1
[root@localhost ~]# read -p "please input: " pass
please input: fff
[root@localhost ~]# echo $pass
fff
2
[root@localhost ~]# echo -n "please input: " ; read pass
please input: 123456
[root@localhost ~]# echo $pass
123456
read 综合实例
[root@localhost ~]# vim test.sh #写入下面的内容
#!/bin/bash
read -p "请输入姓名:" NAME
read -p "请输入年龄:" AGE
read -p "请输入性别:" SEX
cat<<eof
*********************
你的基本信息如下:
姓名: $NAME
年龄:$AGE
性别:$SEX
********************
eof
[root@localhost ~]# ./test.sh
请输入姓名:erio
请输入年龄:24
请输入性别:女
*********************
你的基本信息如下:
姓名: erio
年龄:24
性别:女
********************
流程控制语句if
语法格式:
if 条件
then
commands
fi
if语句流程图:
注:根据我们的命令退出码来进行判断(echo $? =0),如果是0,那么就会执行then后面的命令
[root@localhost ~]# vim if.sh
#!/bin/bash
if ls /mnt
then
echo "it's ok"
fi
[root@localhost ~]# chmod +x if.sh
[root@localhost ~]# ./if.sh
full pass_bacl passwd_bacp sdb1 test_back1 test_back2
it's ok
双分支if语句
语法格式:
if command ; then
commands
else
commands
fi
[root@localhost ~]# vim if.sh
#!/bin/bash
if grep root /etc/passwd ; then
echo "it's ok"
else
"it's err"
fi
[root@localhost ~]# ./if.sh
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
It‘s OK
[root@localhost ~]# cat if.sh
#!/bin/bash
if grep rio /etc/passwd;then
echo "It‘s OK"
else
echo "It's error"
fi
[root@localhost ~]# ./if.sh
It's error
多分支if语句
语法结构:
if条件测试操作1 ; then
commands
elif 条件测试操作2 ; then
commands
elif 条件测试操作3 ; then
commands
…
else
commands
fi
判断用户在系统中是否存在,是否有家目录
[root@localhost ~]# cat if.sh
#!/bin/bash
read -p "input a user:" tu
if grep $tu /etc/passwd ; then
echo "the user $tu exists on this system"
elif ls -d /home/$tu ; then
echo "the user $tu not exists on this system"
echo "$tu has a home directory"
else
echo "the user $tu not exists on this system"
echo "$tu not has a direcotry"
fi
[root@localhost ~]# ./if.sh
input a user:root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
the user root exists on this system
[root@localhost ~]# ./if.sh
input a user:rio
ls: 无法访问/home/rio: 没有那个文件或目录
the user rio not exists on this system
rio not has a direcotry
test测试命令
Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试
格式:test 测试条件
如果结果是对的,也叫结果为真,用$?=0表示,反之为假,用非0表示
数值比较
[root@localhost ~]# cat test.sh
#!/bin/bash
if test 2 -eq 1 ; then
echo ok
else
echo err
fi
if [ 2 -eq 2 ] ; then
echo ok
else
echo err
fi
[root@localhost ~]# ./test.sh
err
ok
比较整数大小
[root@localhost ~]# cat test.sh
#!/bin/bash
read -p "input var1 var2:" var1 var2
if [ $var1 -gt $var2 ] ; then
echo "$var1 > $var2"
elif [ $var1 -lt $var2 ] ; then
echo "$var1 < $var2"
else
echo "$var1 = $var2"
fi
[root@localhost ~]# ./test.sh
input var1 var2:88 44
88 > 44
[root@localhost ~]# ./test.sh
input var1 var2:7 0
7 > 0
[root@localhost ~]# ./test.sh
input var1 var2:1 6
1 < 6
注意:在做数值比较时,只能用整数
字符串比较
根据用户名判断是否是超级管理员
[root@localhost ~]# cat test.sh
#!/bin/bash
#!/bin/bash
read -p "input your name: " name
if [ $name == "root" ] ; then
echo "you are super administrator"
else
echo "You are a general user"
fi
[root@localhost ~]# ./test.sh
input your name: alon
You are a general user
[root@localhost ~]# ./test.sh
input your name: root
you are super administrator
在做字符串大小比较的时候,注意字符串的顺序
大于号和小于号必须转义,要不然SHELL会把它当成重定向符号
大于和小于它们的顺序和sort排序是不一样的
在test比较测试中,它使用的是ASCII顺序,大写字母是小于小写字母的;sort刚好相反
扩展: ASCII(American Standard Code for Information Interchange,美国信息交换标准代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言。它是现今最通用的单字节编码系统,并等同于国际标准ISO/IEC 646。
[root@localhost ~]# cat test.sh
#!/bin/bash
var1=test
var2=Test
if [ $var1 \> $var2 ] ; then
echo "$var1 > $var2"
else
echo "$var1 < $var2"
fi
[root@localhost ~]# ./test.sh
test > Test
文件比较
[root@localhost ~]# cat test.sh
#!/bin/bash
if [ -e /etc/passwd ] ; then
echo ok
else
echo err
fi
[root@localhost ~]# ./test.sh
ok
[root@localhost ~]# test -e /etc/aaa.txt && echo ok || echo err
err
[root@localhost ~]# test -e /etc/passwd && echo ok || echo err
ok
[root@localhost ~]# test -e /etc && echo ok || echo err
ok
清空日志目录
[root@localhost ~]# vim clear-log.sh
#!/bin/bash
#clear /var/log/messages
#确定当前是root用户
if [ $USER !="root" ];then
echo "你必须是root用户才能执行脚本"
exit 10
fi
#判断文件是否存在
if [ ! -f /var/log/messages ];then
echo "文件存在"
exit 12
fi
#保留最近100行的日志内容
tail -100 /var/log/messages > /var/log/mesg.tmp
#清理日志
>/var/log/messages
# cat /dev/null > /var/log/messages
mv /var/log/mesg.tmp /var/log/messages
echo "Logs clean up"
[root@localhost ~]# ./clear-log.sh
Logs clean up
注:退出码 exit ,取值范围是0-255
流程控制过程中复杂条件和通配符
判断第一种:两个条件都为真或有一个为真就执行
if [ 条件判断一 ] && (||) [ 条件判断二 ]; then
命令一
elif [ 条件判断三 ] && (||) [ 条件判断四 ]; then
命令二
else
执行其它
fi
判断第二种
if [条件判断一 -a (-o) 条件判断二 -a (-o) 条件判断三]; then
elif [条件判断三 -a (-o) 条件判断四 ]; then
else
执行其它
fi
判断第三种
if [[条件判断一 && (||) 条件判断二 ]]; then
elif [[ 条件判断三 && (||) 条件判断四 ]]; then
else
执行其它
fi
设置umask
[root@localhost ~]# cat /etc/profile
[root@localhost ~]# vim umask.sh
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
echo "umask 002"
else
echo "i am root :umask 022"
fi
[root@localhost ~]# bash umask.sh
i am root :umask 022
[[ 。。。 ]]和[ 。。。]的区别
[[… ]] 运算符是[… ]运算符的扩充;[[… ]]能够支持 *,< 、>等符号且不需要转义符
[root@localhost ~]# if [[ $USER == r* ]] ; then echo "hello,$USER" ; else echo $USER not ; fi
hello,root
注: $USER == r对比时, r 表示以r开头的任意长度字符串,这样就包括root
当只有一个[] 方括号时:
[root@localhost ~]# if [ $USER == r* ] ; then echo "hello,$USER" ; else echo $USER not ; fi
root not
#对比时r* ,就表示两个字符串 r*
[root@localhost ~]#if [[ $USER == [a-z]oot ]] ; then echo "hello,$USER" ; else echo $USER not ; fi
[[ 。。。 ]]和[ 。。。]的区别汇总:
1、所有的字符与逻辑运算符直接用“空格”分开,不能连到一起。
2、在[… ]表达式中,常见的> 、<需要加转义符\,大小比较
3、进行逻辑运算符&& 、||比较时;如果用的[ ]符号,则用在外面,如[… ] && [… ] || [ …]如果在[…]里面进行逻辑与或的比较,则用-a、-o进行表示,如[ x = y –a x < z –o x > m ]
4、[[… ]] 运算符只是[… ]运算符的扩充;能够支持< 、>符号运算不需要转义符;它还是以字符串比较大小。里面支持逻辑运算符 || 、 && , 不再使用-a 、-o
5、[[…]] 用 && 而不是 -a 表示逻辑“与”;用 || 而不是 -o表示逻辑“或”
6、[[… ]]可以进行算术扩展,而[ … ]不可以
7、[[…]]能用正则,而[…]不行
8、双括号(( ))用于数学表达式
9、双方括号号[[ ]]用于高级字符串处理,比如“模糊匹配”
shell中的通配符
[root@localhost ~]# ls /etc/*.conf
[root@localhost ~]# ls /etc/???.conf
/etc/yum.conf
[root@localhost ~]# touch /etc/a{1,2,3}.txt
[root@localhost ~]# ls /etc/*.txt
/etc/a1.txt /etc/a2.txt /etc/a3.txt
[root@localhost ~]# ls /etc/a[1,2,3].txt
/etc/a1.txt /etc/a2.txt /etc/a3.txt
3个shell脚本实战
编写脚本检查服务器运行状态
[root@localhost ~]# vim status.sh
#!/bin/bash
if [ $# -ge 1 ] ; then
systemctl status $1 > /dev/null
if [ $? -eq 0 ] ; then
echo "$1 服务正在运行"
else
systemctl start $1
fi
else
echo "执行脚本的格式"
echo "sh $0 服务名"
fi
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# bash status.sh httpd
httpd 服务正在运行
根据学生的成绩判断 学生的优劣
[root@localhost ~]# vim check.sh
#!/bin/bash
read -p "请输入你的成绩 " cj
if [ $cj -ge 0 ] && [ $cj -le 59 ] ;then
echo "补考"
elif [ $cj -ge 60 ] && [ $cj -le 70 ] ;then
echo "良好"
elif [ $cj -ge 71 ] && [ $cj -le 85 ] ;then
echo "好"
elif [ $cj -ge 86 ] && [ $cj -le 100 ] ;then
echo "优秀"
else
echo "成绩的有效范围是0-100之间"
fi
[root@localhost ~]# ./check.sh
请输入你的成绩 4
补考
[root@localhost ~]# vim check.sh
[root@localhost ~]# ./check.sh
请输入你的成绩 44
补考
[root@localhost ~]# ./check.sh
请输入你的成绩 78
好
[root@localhost ~]# ./check.sh
请输入你的成绩 90
优秀
每周一晚上3:00 ,备份数据库服务器上webdb库的所有数据到系统的/mysqlbak目录里,使用系统日期做备份文件名。
#!/bin/bash
baknamefile=date +%Y-%m-%d
#备份名
bakdir=/mysqlbak #备份目录
user=root #备份用户
password=123 #用户密码
dbname=webdb #数据名
[ -e $bakdir ] || mkdir
b
a
k
d
i
r
m
y
s
q
l
d
u
m
p
−
u
bakdir mysqldump -u
bakdirmysqldump−uuser -p$password --flush-log $dbname >
b
a
k
d
i
r
/
bakdir/
bakdir/{baknmefile}-webdb.sql
[root@localhost ~]# cat mysqldump.sh
#!/bin/bash
baknamefile=`date +%Y-%m-%d`
bakdir=/etcbak
srcdir=/etc
[ -e $bakdir ] || mkdir $bakdir
tar zcvf ${bakdir}/${baknamefile}-etc.tar.gz /etc/
echo "========================"
ls -lh ${bakdir}/${baknamefile}-etc.tar.gz
echo "back etc is ok!"
[root@localhost ~]# crontab -e
0 3 * * * /root/mysqldump.sh 2>&1 > /dev/null