1、编写及执行Shell程序
Shell命令行的书写规则:
在执行Shell命令时多个命令可以在一个命令行上运行,但此时要使用分号(;)分隔命令,例如:
[root@localhost root]# ls a* -l;free;df
长Shell命令行可以使用反斜线字符(\)在命令行上扩充,例如:
[root@localhost root]# echo “this is \
>long command”
This is long command
我们先打开一个文件编写Shell程序:
[root@localhost bin]#vi date
接下来输入以下程序:
#!/bin/sh
echo "Mr.$USER,Today is:"
echo &date "+%B%d%A"
echo "Wish you a lucky day!"
然后给文件设置可执行权限:
[root@localhost bin]#chmod +x date
之后就可以执行了,执行方法一般有三种:
方法一:
[root@localhost bin]#./ date
方法二:
[root@localhost bin]# Bash date
方法三:
[root@localhost root]#export PATH=/bin:$PATH
[root@localhost bin]# date
实例1:编写一个Shell程序mkf,此程序的功能是:显示root下的文件信息,然后建立一个kk的文件夹,在此文件夹下建立一个文件aa,修改此文件的权限为可执行。
程序如下:
[root@localhost root]#vi mkf
ls –l
mkdir kk
cd kk
vi aa
chmod +x aa
cd /root
执行后的结果如下:
[root@localhost bin]# test1 this is a test program //传递5个参数
Program name is /bin/test1 //给出程序的完整路径和名字
There are totally 5 parameters passed to this program //参数的总数
The last is 0 //程序执行效果
The parameters are this is a test program //返回由参数组成的字符串
2、参数
实例 2:编写一个Shell程序,用于描述Shell程序中的位置参数为:$ 0 , 0, 0,#、 ? 、 ?、 ?、*,程序名为test1,代码如下:
[root@localhost bin]#vi test1
#! /bin/sh
echo “Program name is $0”;
echo “There are totally $# parameters passed to this program”;
echo “The last is $?”;
echo “The parameter are $*”
实例3:利用内部变量和位置参数编写一个名为test2的简单删除程序,如删除的文件名为a,则在终端中输入的命令为:test a
编辑程序:
[root@localhost bin]#vi test2
#! /bin/sh
if test $# -eq 0
then
echo “Please specify a file!”
else
gzip $1 //现对文件进行压缩
mv $1.gz $HOME/dustbin //移动到回收站
echo “File $1 is deleted !”
fi
执行结果:
[root@localhost bin]#chmod +x test2
[root@localhost bin]# test2 a //如果a文件在bin目录下存在
File a is deleted!
3、变量
实例 4:编写一个Shell程序test3,程序执行时从键盘读入一个目录名,然后显示这个目录下所有文件的信息。
编辑程序:
[root@localhost bin]#vi test3
#! /bin/sh
echo “please input name of directory”
read DIRECTORY
cd $DIRECTORY
ls –l
执行:
[root@localhost bin]#chmod +x test3
[root@localhost bin]#./test3
实例5:运行程序test4,从键盘读入x、y的值,然后做加法运算,最后输出结果。
编辑程序:
[root@localhost bin]#vi test4
#! /bin/sh
echo “please input x y”
read x,y
z=`expr $x+$y`
echo “The sum is $z”
执行结果:
[root@localhost bin]#chmod +x test4
[root@localhost bin]#./ test4
45 78
The sum is 123
4、表达式的比较
实例 6:从键盘输入两个字符串,判断这两个字符串是否相等。
程序如下:
[root@localhost bin]#vi test5
#! /bin/Bash
read ar1
read ar2
[ “$ar1” = “$ar2” ]
echo $? #?保存前一个命令的返回码
执行结果:
[root@localhost bin]#chmod +x test5
[root@localhost root]#./ test5
aaa
bbb
1
实例 7: 比较字符串长度是否大于零。
程序如下:
[root@localhost bin]#vi test6
#! /bin/Bash
read ar
[ -n “$ar” ]
echo $? //保存前一个命令的返回码
执行结果:
[root@localhost bin]#chmod +x test6
[root@localhost bin]#./ test6
0
实例 8:比较两个数字是否相等。
程序如下:
[root@localhost bin]#vi test7
#! /bin/Bash
read x,y
if test $x –eq $y
then
echo “$x=$y”
else
echo “$x!=$y”
fi
执行结果:
[root@localhost bin]#chmod +x test7
[root@localhost bin]#./ test7
50 100
50!=100
[root@localhost bin]#./ test7
150 150
150=150
实例9:分别给两个字符变量赋值,一个变量赋予一定的值,另一个变量为空,求两者的与、或操作。
程序如下:
[root@localhost bin]#vi test8
#! /bin/Bash
part1 =”1111”
part2 =” ” #part2为空
[ “$ part1” –a “$ part2”]
echo $? #保存前一个命令的返回码
[ “$ part1” –o “$ part2”]
echo $?
执行结果:
[root@localhost bin]#chmod +x test8
(3)执行
[root@localhost bin]#./ test8
1
0
实例 10:判断zb目录是否存在于/root下。
程序如下:
[root@localhost bin]#vi test9
#! /bin/Bash
[ -d /root/zb ]
echo $? #保存前一个命令的返回码
执行结果:
[root@localhost bin]#chmod +x test9
[root@localhost bint]#./ test9
1
[root@localhost bin]#mkdir zb
[root@localhost bin]#./test9
0
实例 11:编写一个Shell程序test10,输入一个字符串,如果是目录,则显示目录下的信息,如为文件显示文件的内容.
程序如下:
[root@localhost bin]#vi test10
#! /bin/Bash
echo “Please enter the directory name or file name”
read DORF
if [ -d $DORF ]
then
ls $DORF
elif [ -f $DORF ]
then
cat $DORF
else
echo “input error! ”
fi
执行:
root@localhost bin]#chmod +x test10
[root@localhost bin]#./ test10