今天再来学习shell脚本,下面对自己已经掌握的一些shell语法及命令进行总结!
1.read命令提示用户输入字符串
设置好PATH变量,这个比较好的是我们写的脚本在执行时可以直接使用一些外部命令,而不必加上绝对路径。
[zoulei@CentOS test]$ vim test.sh
1 #!/bin/bash
2 PATH=$PATH
3 export PATH
4 read -p "Your first name:" firstname
5 read -p "Your last name:" lastname
6 echo -e "Your full name:$firstname $lastname"
7 exit 0
[zoulei@CentOS test]$ sh test.shYour first name:zouYour last name:leiYour full name:zou lei[zoulei@CentOS test]$ echo $?0
exit 0的作用是离开script且执行完脚本的同时回传一个0给系统,然后在shell命令行输入echo $?则可得到0的值。
2.test命令的测试功能
[zoulei@CentOS test]$ vim test.sh
1 #!/bin/bash
2 PATH=$PATH
3 export PATH
4 echo -e "Input name of the file that you want to check"
5 read -p "Filename:" filename
6 test -z $filename && echo "You must input a filename" && exit 0
7 test ! -e $filename && echo "The file '$filename' DO NOT exist" && exit 0
8 exit 0
[zoulei@CentOS test]$ sh test.shtest -z string的作用是判定输入的字符串是否为零,若string为空字符串,则为true!
Input name of the file that you want to check
Filename:test.sh
[zoulei@CentOS test]$ sh test.sh
Input name of the file that you want to check
Filename:hh.sh
The file 'hh.sh' DO NOT exist
[zoulei@CentOS test]$
test -e filename表示filename文件是否存在,
test ! -x filename表示当filename不具有X时,回传ture!这里的x指e,f,d等测试标志,x为e用看来测试文件是否存在!
3.条件表达式
(1)if...elif..else...fi结构
[zoulei@CentOS test]$ vim test.sh
1 #!/bin/bash
2 PATH=$PATH
3 export PATH
4 read -p "Please input [Y/N]" choice
5 if [ "$choice" == "Y" ] || [ "$choice" == "y" ];then
6 echo "OK!continue"
7 exit 0
8
9 elif [ "$choice" == "N" ] || [ "$choice" == "n" ];then
10 echo "oh!interupt"
11 exit 0
12 else
13 echo "Input [Y/N]"
14 fi
15 exit 0
16
[zoulei@CentOS test]$ vim test.sh(2).case...esac结构
[zoulei@CentOS test]$ sh test.sh
Please input [Y/N]y
OK!continue
[zoulei@CentOS test]$ sh test.sh
Please input [Y/N]n
oh!interupt
[zoulei@CentOS test]$
[zoulei@CentOS test]$ vim test.sh
[zoulei@CentOS test]$ sh test.sh
Please input [Y/N]y
OK!continue
[zoulei@CentOS test]$ sh test.sh
Please input [Y/N]n
oh!interupt
[zoulei@CentOS test]$ sh test.sh
Please input [Y/N]k
Input [Y/N]
[zoulei@CentOS test]$
[zoulei@CentOS test]$ vim test.sh
1 #!/bin/bash
2 PATH=$PATH
3 export PATH
4 read -p "Please input your choice:" choice
5 case $choice in
6 "one")
7 echo "Your choice is ONE"
8 ;;
9 "two")
10 echo "Your choice is TWO"
11 ;;
12 "three")
13 echo "Your choice is THREE"
14 ;;
15 *)
16 echo "Usage $0{one|two|three}"
17 ;;
18 esac
19 exit 0
*************************************************************************************************************************************************************************************
注意:
case结构语法如下:case $变量名称 in <==关键字为case,变量前有$符号
"第一个变量名称")
;; <==每个类型结尾使用两个连续的分号来处理!
"第二个变量名称")
;;
*) <==最好一个变量的内容都会用*来代表其他值
esac <==最终的case结尾!“case反过来写表示结尾”
**************************************************************************************************************************************************************************************
[zoulei@CentOS test]$ sh test.sh
Please input your choice:one
Your choice is ONE
[zoulei@CentOS test]$ sh test.sh
Please input your choice:kk
Usage test.sh{one|two|three}
[zoulei@CentOS test]
**************************************************************************************************************************************************************************************
注意:
($0,$1...)为shell script 的默认变量,$0表示执行的脚本文件名,$1表示脚本文件名后接的第一个参数,$2 表示脚本文件名后接的第二个参数。$#代表脚本文件名后接的参数个数,$@代表"$1","$2","$3"之意,每个变量是独立的。
**************************************************************************************************************************************************************************************
4.循环
(1)while...do...done结构
[zoulei@CentOS test]$ vim test.sh
1 #!/bin/bash
2 PATH=$PATH
3 export PATH
4
5 while [ "$choice" != "yes" ]
6 do
7 read -p "Give your choice [yes/no]:" choice
8 done
9 echo "OK! you input the correct answer"
10 exit 0
[zoulei@CentOS test]$ sh test.sh
Give your choice [yes/no]:no
Give your choice [yes/no]:yes
OK! you input the correct answer
[zoulei@CentOS test]$
(2)for...do...done结构
[zoulei@CentOS test]$ vim test.sh
1 #!/bin/bash
2 PATH=$PATH
3 export PATH
4
5 for animal in cat dog elephant
6 do
7 echo "There are ${animal}s..."
8 done
9 #echo "OK! you input the correct answer"
10 exit 0
[zoulei@CentOS test]$ sh test.shThere are cats...There are dogs...There are elephants...*************************************************************************************************************************************************************************************
注意:
语法为:
for var in con1 con2 con3 ...
do
程序段
done
这个$var的变量内容在循环工作时:1.第一次循环时,$var的内容为con1;2.第二次循环时,$var的内容为con2;3.第三次循环时,$var的内容为con3;......
****************************************************************************************************************************************************************************************************
再看个例子:
[zoulei@CentOS test]$ vim test.sh
1 #!/bin/bash
2 PATH=$PATH
3 export PATH
4 users=$(cut -d ':' -f1 /etc/passwd)
5
6 for username in $users
7 do
8 id $username
9
10 done
11
12 exit 0
[zoulei@CentOS test]$ sh test.shuid=0(root) gid=0(root) 组=0(root)uid=1(bin) gid=1(bin) 组=1(bin),2(daemon),3(sys)uid=2(daemon) gid=2(daemon) 组=2(daemon),1(bin),4(adm),7(lp)uid=3(adm) gid=4(adm) 组=4(adm),3(sys)uid=4(lp) gid=7(lp) 组=7(lp)uid=5(sync) gid=0(root) 组=0(root)uid=6(shutdown) gid=0(root) 组=0(root)uid=7(halt) gid=0(root) 组=0(root)uid=8(mail) gid=12(mail) 组=12(mail)uid=10(uucp) gid=14(uucp) 组=14(uucp)uid=11(operator) gid=0(root) 组=0(root)uid=12(games) gid=100(users) 组=100(users)uid=13(gopher) gid=30(gopher) 组=30(gopher)uid=14(ftp) gid=50(ftp) 组=50(ftp)uid=99(nobody) gid=99(nobody) 组=99(nobody)uid=81(dbus) gid=81(dbus) 组=81(dbus)uid=113(usbmuxd) gid=113(usbmuxd) 组=113(usbmuxd)uid=69(vcsa) gid=69(vcsa) 组=69(vcsa)uid=499(rtkit) gid=497(rtkit) 组=497(rtkit)uid=170(avahi-autoipd) gid=170(avahi-autoipd) 组=170(avahi-autoipd)uid=498(pulse) gid=496(pulse) 组=496(pulse)uid=68(haldaemon) gid=68(haldaemon) 组=68(haldaemon)uid=38(ntp) gid=38(ntp) 组=38(ntp)uid=48(apache) gid=48(apache) 组=48(apache)uid=497(saslauth) gid=76(saslauth) 组=76(saslauth)uid=89(postfix) gid=89(postfix) 组=89(postfix),12(mail)uid=173(abrt) gid=173(abrt) 组=173(abrt)uid=42(gdm) gid=42(gdm) 组=42(gdm)uid=74(sshd) gid=74(sshd) 组=74(sshd)uid=72(tcpdump) gid=72(tcpdump) 组=72(tcpdump)uid=500(lingyun) gid=500(lingyun) 组=500(lingyun)uid=32(rpc) gid=32(rpc) 组=32(rpc)uid=29(rpcuser) gid=29(rpcuser) 组=29(rpcuser)uid=65534(nfsnobody) gid=65534(nfsnobody) 组=65534(nfsnobody)uid=501(zoulei) gid=501(zoulei) 组=501(zoulei)uid=502(leilei) gid=502(leilei) 组=502(leilei)这是通过管道命令的cut找出单纯的账号名称,以id检查用户的标识符!
5.函数function
[zoulei@CentOS test]$ vim test.sh
1 #!/bin/bash
2 PATH=$PATH
3 export PATH
4 function print(){
5 echo "Your choice is $1"
6 }
7 echo "This program will print your choice! "
8
9 case $1 in
10 "one")
11 print 1
12 ;;
13 "two")
14 print 2
15 ;;
16 "three")
17 print 3
18 ;;
19 *)
20 echo "Usage $0 {one|two|three}"
21 ;;
22 esac
23 exit 0
**************************************************************************************************************************************************************************************************
注意:
function语法:
function fname() {
程序段
}
那个fname是我们自定义的执行命令的名称,程序段就是我们要执行它的内容。
***************************************************************************************************************************************************************************************************
[zoulei@CentOS test]$ sh test.sh two其实shell脚本中的函数与C语言中的函数用法是一样的,都是定义之后,直接调用就可以,可以简化程序代码,shell脚本与C定义函数不同的是shell脚本是将一些命令封装到自定义的函数里面,当然两者定义函数时,都可以嵌套的!
This program will print your choice!
Your choice is 2
相信学习了shell script的这些语法,看懂别人写的脚本应该是没有什么问题了,只要很熟悉shell命令,那么我相信也能够写出自己想要的脚本,不过一切的要自己多去写才行!
好了,shell 脚本先总结到这里了!以后自己要多多练习。