RHCE033--Shell Scripts
时间:2010-09-28 来源:twenty_four
- Shell Scripts是一个包含shell指令的文本文件。
- 我们最常用的shell脚本:
- 我们可以使用各种编辑工具来编辑shell script ,比如vim
- 在脚本中,#开头表示该行被注释。
- 脚本中可以采用shell下能使用的所用命令、变量、正则表达式
- ^a 以开头的a字符
- a$ 以a结尾的字符
- \.$ 以点结尾的字符 cat /etc/hosts |grep '\.$'
- * = {0,} 出现0次及0次以上。
- + = {1,} 出现1次及1次以上
- . 任意一个字符(广意)
- {m,n} 出现至少m到n次。
- sh /路径/脚本名
- . /路径/脚本名
- ksh /路径/脚本名
- bash /路径/脚本名
- 实例一:计算1+2+3...100的值
- 实例二:判断当前目录是否为文件,并统计文件的个数。
一、Shell 脚本
Shell脚本支持变量与简单的语法来完成批量工作。
Shell脚本支持交互的方式,在执行时从标准输入读取数据。
有大量的系统工作都是通过Shell脚本的方式来完成,例如系统开机,服务控制。
/etc/rc.sysinit: 系统初始化脚本
/etc/rc.local : 很像Dos下的autoexec.bat的系统用户自定义启动脚本
/etc/profile : bash shell 的登录脚本之一
二、编辑shell脚本
一个shell脚本的第一行,可以做特别定义:
第一个字符非#,表示这是一个bash脚本。
第一个字符是#,但第二个字符不是!,表示这是一个c shell脚本。
第一个字符是#,且第二个字符是!,表示调用其后指定的shell来执行这个脚本。
当我们定义了脚本解释器,但这个脚本解释器又不存在,那么这个脚本就不能正常运行!
比如,定义一个数据大小比较的脚本
[root@51cto ~]# vim 51cto.sh
[root@51cto ~]# cat 51cto.sh
#!/bin/bash //bash是一个存在的脚本编辑器,所以能执行脚本
#blog-link:http://twentyfour.blog.51cto.com/
echo "input num1:"
read num1
echo "input num2:"
read num2
if [ $num1 -eq $num2 ];then
echo "$num1 = $num2"
elif [ $num1 -gt $num2 ];then
echo "$num1 > $num2"
elif [ $num1 -lt $num2 ];then
echo "$num1 < $num2"
fi
[root@51cto ~]# chmod +x 51cto.sh //赋予可执行权限
[root@51cto ~]# sh 51cto.sh //执行脚本
input num1:
51
input num2:
15
51 > 15 //脚本测试结果
正则表达式:元字符的组合,元字符是所有能表示的最小单元。如下:
[root@51cto ~]# ls |grep '^5' //列出以5开头的文件
51cto.sh
[root@51cto ~]# ls /etc/|grep 'conf$' //列出以conf结尾的文件
autofs_ldap_auth.conf
cdrecord.conf
conman.conf
dhcp6c.conf
dnsmasq.conf
esd.conf
…….
[root@51cto ~]# touch 51cto. //创建一个以 . 结尾的51cto. 文件
[root@51cto ~]# ls |grep '\.$' //列出以 . 结尾的文件
51cto.
(注意:这里 ls |grep '\.$' 一定要加上\ ,不然系统会把 . 当成任意字符)
[root@51cto ~]# ls |grep '.$' //这个命令等同于ls了
51cto.
51cto.sh
aabb
anaconda-ks.cfg
cisco
Desktop
file1
file2
install.log
install.log.syslog
linux
……
[root@51cto ~]# vim cto
[root@51cto ~]# cat cto //拿cto这个文件验证
aabb bbaa
ab
aaa
bbb
[root@51cto ~]# grep 'a*' cto //查看出现0次及0次以上a的内容,发现都显示
aabb bbaa
ab
aaa
bbb
[root@51cto ~]# grep -E 'a+' cto //发现bbb和空白都不显示了
aabb bbaa
ab
aaa
[root@51cto ~]# grep -E '(ab)+' cto //包含ab的才显示
aabb bbaa
ab
[root@51cto ~]# grep -E 'a.' cto
aabb bbaa
ab
aaa
[root@51cto ~]# grep -E 'a{2,3}' cto //显示出现2到3个a的内容
aabb bbaa
aaa
三、运行shell脚本
[root@51cto ~]# vim cto.sh
[root@51cto ~]# cat cto.sh
#!/bin/bash
var=date
echo `$var`
[root@51cto ~]# chmod +x cto.sh
[root@51cto ~]# sh cto.sh
Tue Sep 28 01:09:08 PDT 2010
[root@51cto ~]# . cto.sh
Tue Sep 28 01:09:24 PDT 2010
[root@51cto ~]# ksh cto.sh
Tue Sep 28 01:09:29 PDT 2010
[root@51cto ~]# bash cto.sh
Tue Sep 28 01:09:34 PDT 2010
四、if
条件判断控制语句:
if 条件; then
动作
elif 条件; then
动作
else 条件; then
动作
fi
if,当条件为真时,执行then后的动作。
elif在if判断为假时才做判断,else在if与elif都为假时执行。
例子:
[root@51cto ~]# vim 24.sh
[root@51cto ~]# cat 24.sh
#!/bin/bash
echo "input num1:"
read num1
echo "input num2:"
read num2
if [ $num1 -eq $num2 ];then //判断是否等于
echo "$num1 = $num2"
elif [ $num1 -gt $num2 ];then //判断是否大于
echo "$num1 > $num2"
elif [ $num1 -lt $num2 ];then //判断是否小于
echo "$num1 < $num2"
fi
[root@51cto ~]# chmod +x 24.sh
[root@51cto ~]# sh 24.sh
input num1:
24
input num2:
42
24 < 42 //输出正确结果
五、for
循环控制语句
for 变量 in 数组
do
动作
done
[root@51cto ~]# vim for.sh
[root@51cto ~]# cat for.sh
#!/bin/bash
sum=0
for ((i=1;i<=100;i++))
do
let sum=sum+i
done
echo $sum
[root@51cto ~]# chmod +x for.sh
[root@51cto ~]# sh for.sh
5050
[root@51cto ~]# vim for24.sh
[root@51cto ~]# chmod +x for24.sh
[root@51cto ~]# cat for24.sh
#!/bin/bash
sum=0
for i in `ls`
do
if [ -f $i ];then
let sum=sum+1
fi
done
echo file total: $sum
[root@51cto ~]# sh for24.sh
file total: 19
[root@51cto ~]# ll |grep '^-'
-rwxr-xr-x 1 root root 227 Sep 28 03:16 24.sh
-rwxr-xr-x 1 root root 320 Sep 28 03:21 42.sh
-rw-r--r-- 1 root root 0 Sep 28 00:31 51cto.
-rwxr-xr-x 1 root root 272 Sep 28 00:14 51cto.sh
-rw------- 1 root root 943 Sep 23 17:56 anaconda-ks.cfg
-rw-r--r-- 1 root root 30 Sep 26 19:42 cisco
-rw-r--r-- 1 root root 26 Sep 28 00:40 cto
-rwxr-xr-x 1 root root 33 Sep 28 01:08 cto.sh
-rw-r--r-- 1 root root 6 Sep 27 02:18 file1
-rw-r--r-- 1 root root 6 Sep 27 02:19 file2
-rwxr-xr-x 1 root root 102 Sep 28 03:30 for24.sh
-rwxr-xr-x 1 root root 74 Sep 28 03:28 for.sh
-rw-r--r-- 1 root root 28206 Sep 23 17:56 install.log
-rw-r--r-- 1 root root 3498 Sep 23 17:55 install.log.syslog
-rw-r--r-- 1 root root 14 Sep 26 19:39 linux
-rw-r--r-- 1 root root 16 Sep 26 19:41 linuxer
-rw-r--r-- 1 root root 100 Sep 26 19:21 ok.txt
-rw-r--r-- 1 root root 0 Sep 25 21:41 twenty_four
-rw-r--r-- 1 root root 14 Sep 26 19:40 unix //总19个文件
六、until
循环控制语句
until 条件
do
动作
Done
until,当条件为真时,结束其后的循环。否则,一直循环下去。
实例:
[root@51cto ~]# vim until.sh
[root@51cto ~]# cat until.sh
#!/bin/bash
until
echo "1-display dir"
echo "2-cd /home"
echo "3-vim /root/1.sh"
echo "4-rm /root/1.txt"
echo "5-quit"
read yn
test $yn = 5
do
case "$yn" in
1)
ls -l /root
;;
2)
cd /home
;;
3)
vim /root/1.sh
;;
4)
rm /root/1.txt -rf
;;
5|q|Q)
echo "quit"
break
;;
*)
echo "input 1~5";
;;
esac
done
[root@51cto ~]# sh until.sh
1-display dir
2-cd /home
3-vim /root/1.sh
4-rm /root/1.txt
5-quit
5
[root@51cto ~]#
七、判断ssh、ftp、http、https、pop3、smtp服务是否运行
[root@51cto ~]# service sshd status //服务启动ing
openssh-daemon (pid 2846) is running...
[root@51cto ~]# service vsftpd status //服务启动ing