Shell命令行书写规则
对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程序的步骤
Shell程序是指放在一个文件中的一系列Linux命令和实用程序。在执行的时候,通过Linux操作系统一个接一个地解释和执行每条命令。首先,来编写第一个Shell程序,从中学习Shell程序的编写、修改权限、执行过程。
1、编辑一个内容如下的源程序,保存文件名为date,可将其存放在目录/bin下
[root@localhost bin]#vi date
#! /bin/sh
echo “Mr.$USER,Today is:”
echo &date “+%B%d%A”
echo “Wish you a lucky day !”
#! /bin/sh通知采用Bash解释。如果在echo语句中执行Shell命令date,则需要在date命令前加符号“&”,其中%B%d%A为输入格式控制符。
2、编辑完该文件之后不能立即执行该文件,需给文件设置可执行程序权限。使用如下命令
[root@localhost bin]#chmod +x date
3、执行Shell程序
执行Shell程序有下面三种方法:
方法一:
[root@localhost bin]#./ date
Mr.root,Today is:
二月 06 星期二
Wish you a lucky day !
方法二:
[root@localhost bin]# Bash date
Mr.root,Today is:
二月 06 星期二
Wish you a lucky day !
方法三:
为了在任何目录都可以编译和执行Shell所编写的程序,即把/bin的这个目录添加到整个环境变量中。
[root@localhost root]#export PATH=/bin:$PATH
[root@localhost bin]# date
Mr.root,Today is:
二月 06 星期二
Wish you a lucky day !
Shell程序中使用的参数
如同ls命令可以接受目录等作为它的参数一样,在Shell编程时同样可以使用参数。Shell程序中的参数分为位置参数和内部参数等。
1、位置参数
由系统提供的参数称为位置参数。位置参数的值可以用$N得到,N是一个数字,如果为1,即$1。Linux会把输入的命令字符串分段并给每段进行标号,标号从0开始。第0号为程序名字,从1开始就表示传递给程序的参数。如$0表示程序的名字,$1表示传递给程序的第一个参数,以此类推$1-$9,${10}........。
$1-$9 第1个到第9个位置参量
${10} 第10个位置参量,类似的${11}
2、内部参数
上述过程中的$0是一个内部变量,它是必须的,而$1则可有可无,最常用的内部变量有$0、$#、$?、$*,它们的含义如下。
$0 当前脚本的文件名
$# 位置参量的个数
$* 以单个字符串显示所有位置参量
$@ 未加双引号时和$*含义相同,加双引号时不同
$$ 脚本运行的当前进程号
$! 最后一个后台运行的进程的进程号
$? 显示前面最后一个命令退出状态。0表示没有错误,其他任何值表示有错误。
如果位置参数中有空格 要用双引号
[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 $*”;
执行后的结果如下:
[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 //返回由参数组成的字符串
实例 :利用内部变量和位置参数编写一个名为test2的简单删除程序,如删除的文件名为a,则在终端中输入的命令为:test a
分析:除命令外至少还有一个位置参数,即$#不能为0,删除不能为$1,程序设计过程如下。
(1) 用vi编辑程序
[root@localhost bin]#vi test2
#! /bin/sh
if test $# -eq 0 ; ##判断输入参数是否为0
then
echo “Please specify a file!”
else
gzip $1 ##现对文件进行压缩
mv $1.gz $HOME/dustbin ##移动到回收站
echo “File $1 is deleted !”
fi
(2)
设置权限
[root@localhost bin]#chmod +x test2
(3) 运行
[root@localhost bin]# test2 a (如果a文件在bin目录下存在)
File a is deleted!
Shell程序中的使用变量
1、变量的赋值
在Shell编程中,所有的变量名都由字符串组成,并且不需要对变量进行声明。要赋值给一个变量,其格式如下:
变量名=值
注意:
等号(=)前后没有空格
例如:
x=6
a="How are you"
表示把6赋值给变量x,字符串“How are you ”赋值给变量a。
2、变量的访问
如果要访问变量值,可以在变量前面加一个美元符号“$”,$name 或者 ${name} 或者“${name}”
数组变量:
$ arr = (zhangsan lisi wangwu)
$ echo ${arr[0]} ###zhangsan
$ echo ${arr[*]} ###zhangsan lisi wangwu
$ echo ${#arr[*]} ### 3
$ echo ${arr[0]=beifeng}
3、变量的输入
在Shell程序设计中,变量的值可以作为字符串从键盘读入,其格式为:
read 变量
实例
:编写一个
Shell
程序
test3
,程序执行时从键盘读入一个目录名,然后显示这个目录下所有文件的信息。
[root@localhost bin]#vi test3
#! /bin/sh
echo “please input name of directory”
read DIRECTORY
cd $DIRECTORY
ls –l
(2)设置权限
[root@localhost bin]#chmod +x test3
(3)执行
[root@localhost bin]#./test3
注意:输入路径时需“/”
实例
:运行程序
test4
,从键盘读入
x
、
y
的值,然后做加法运算,最后输出结果。
(1) 用 vi 编辑程序
[root@localhost bin]#vi test4
#! /bin/sh
echo “please input x y”
read x,y
z=`expr $x+$y` ##注意这里是飘号执行
echo “The sum is $z”
(
2
)设置权限
[root@localhost bin]#chmod +x test4
[root@localhost bin]#./test4
45 78
The sum is 123
表达式的比较
在Shell程序中,通常使用表达式比较来完成逻辑任务。表达式所代表的操作符有字符操作符、数字操作符、逻辑操作符、以及文件操作符。其中文件操作符是一种Shell所独特的操作符。因为Shell里的变量都是字符串,为了达到对文件进行操作的目的,于是才提供了文件操作符。
1、字符串操作符
常用的字符串操作符
字符串操作符 | 含义及返回值 |
= | 比较两个字符串是否相同,相同则为“真” |
!= | 比较两个字符串是否不相同,不同则为“真” |
-n | 判定字符串长度是否非0,若非空为“真” |
-z | 判定长度是否为0,若为空则为“真” |
实例 : 判断字符串长度是否大于零
(1)用vi编辑程序
[root@localhost bin]#vi test6
#! /bin/Bash
read ar
[ -n “$ar” ];
echo $? ####保存前一个命令的返回码
(
2
)设置权限
[root@localhost bin]#./test6
####输入空
0 ####结果为0表示前一个命令顺利执行,其实这是一个判断语句,命令都是顺利执行,只是返回是不是真假的问题,这个例子不能体现判断。
2、数字比较符
常用数字比较符
运算符号 | 含 义 |
-eq | 相等 |
-ge | 大于等于 |
-le | 小于等于 |
-ne | 不等于 |
-gt | 大于 |
-lt | 小于 |
(1) 用 vi 编辑程序
[root@localhost bin]#vi test7
#! /bin/Bash
read x,y
if test $x –eq $y ;
then
echo “$x=$y”
else
echo “$x!=$y”
fi
(
2
)设置权限
[root@localhost bin]#chmod +x test7
(
3
)执行
[root@localhost bin]#./test7
50 100
50!=100
[root@localhost bin]#./test7
150 150
150=150
3、逻辑运算符
逻辑运算符
运算符号 | 含 义 |
! | 反:与一个逻辑值相反的逻辑值 |
-a | 与(and):两个逻辑值为“是”返回值为“是”,反之为“否” |
-o | 或(or): 两个逻辑值有一个为“是”,返回值就是“是” |
(1) 用 vi 编辑程序
<pre name="code" class="plain">#! /bin/sh
part1="1111"
part2=""
if [ "$part1" -a "$part2" ]; then
echo "$?"
else
echo "-a false"
fi
if [ "$part1" -o "$part2" ]; then
echo "$?"
else
echo "-o false"
fi
echo "end"
(
2
)设置权限
[root@localhost bin]#chmod +x test
(
3
)执行
[appadmin@dp2 jiangsf]$ ./test<pre name="code" class="plain">-a false
0
end
4、文件操作符
件测试操作表达式通常是为了测试文件的信息,一般由脚本来决定文件是否应该备份、复制或删除。由于test关于文件的操作符有很多,在表中只列举一些常用的操作符。
运算符号 | 含 义 |
-d | 对象存在且为目录返回值为“是” |
-f | 对象存在且为文件返回值为“是” |
-e | 对象存在返回值为“是” |
-r | 对象存在且可读则返回值为“是” |
-w | 对象存在且且可写则返回值为“是” |
-x | 对象存在且且可执行则返回值为“是” |
判断zb目录是否存在于/root下。
[ -d /root/zb ]
循环结构语句
Shell常见的循环语句有for循环、while循环语句和until循环。
1、for循环
语法:
for 变量 in 列表
do
操作
done
列表是在
for
循环的内部要操作的对象,可以是字符串也可以是文件,如果是文件则为文件名。
实例 :在列表中的值: a,b,c,e,I,2,4,6,8 用循环的方式把字符与数字分成两行输出。
(1)用gedit编辑脚本程序test11
[root@localhost bin]#gedit test11
#! /bin/Bash
for i in a,b,c,e,i 2,4,6,8
do
echo $i
done
(
2
)设置权限
[root@localhost bin]#chmod +x test11
(
3
)执行
[root@localhost bin]#./test11
a,b,c,e,i
2,4,6,8
注意:在循环列表中的空格可表示换行。
实例:删除垃圾箱中的所有文件。
(1)用gedit编辑脚本程序test12
[root@localhost bin]#gedit test12
#! /bin/Bash
for i in $HOME/.Trash/*
do
rm $ i
echo “$ i has been deleted!”
done
(
2
)设置权限
[root@localhost bin]#chmod +x test12
(
3
)执行
[root@localhost bin]#./ test12
/root/.Trash/abc~ has been deleted!
/root/.Trash/abc1 has been deleted!
实例
:求从
1
~
100
的和。
[root@localhost bin]#gedit test13
#! /bin/Bash
total =0
for((j=1;j<=100;j++));
do
total=`expr $total + $j`
done
echo “The result is $total”
(
2
)设置权限
[root@localhost bin]#chmod +x test13
(
3
)执行
[root@localhost bin]#./ test13
The result is 5050
注意
:for语句中的双括号不能省,最后的分号可有可无,表达式total=`expr $total + $j`的加号两边的空格不能省,否则会成为字符串的连接。
2、while循环
语法:只要表达式为真,do和done之间的操作就一直会进行。
while 表达式
do
操作
done
实例
12-15
:用
while
循环求
1
~
100
的和。
[root@localhost bin]#gedit test13
total =0
num=0
while((num<=100));
do
total=’expor $total +$ num’
done
echo “The result is $total”
3、until循环
语法:重复do和done之间的操作直到表达式成立为止。与while相反
until 表达式
do
操作
done
实例
:用
until
循环求
1
~
100
的和。
[root@localhost bin]#gedit test15
total =0
num=0
until [$num –gt 100]
do
total=’expor $total +$ num’
num=’expr $num + 1’
done
echo “The result is $total”
条件结构语句
1、if语句
语法:
if 表达式1 ; then
操作
elif表达式2 ; then
操作
elif表达式3 ; then
操作
……
else
操作
fi
[root@localhost bin]#gedit test16
for((j=0;j<=10;j++))
do
if(($j%2==1))
then
echo “$j”
fi
done
2、case语句
语法
case 表达式 in
值1|值2)
操作;;
值3|值4)
操作;;
值5|值6)
操作;;
*)
操作;;
esac
case
的作用就是当字符串与某个值相同是就执行那个值后面的操作。如果同一个操作等于多个值,则使用“|”将各个值分开。在case的每一个操作的最后面都有两个“;;”分号是必需的。
实例:Linux是一个多用户操作系统,编写一程序根据不同的用户登录输出不同的反馈结果。
(1)用vi编辑脚本程序test17
[root@localhost bin]#gedit test17
#!/bin/sh
case $USER in
beechen)
echo “You are beichen!”;;
liangnian)
echo “You are liangnian”; //注意这里只有一个分号
echo “Welcome !”;; //这里才是两个分号
root)
echo “You are root!”;echo “Welcome !”;;
//将两命令写在一行,用一个分号作为分隔符
*)
echo “Who are you?$USER?”;;
easc
(
2
)设置权限
[root@localhost bin]#chmod +x test17
(
3
)执行
[root@localhost bin]#./ test17
You are root
Welcome!
Shell脚本中使用函数
函数格式如下:
函数名( )
{
函数体
}
函数调用方式为
函数名 参数列表
实例
:
编写一函数
add
求两个数的和,这两个数用位置参数传入,最后输出结果。
(1)编辑代码
[root@localhost bin]#gedit test18
#!/bin/sh
add()
{
a=$1
b=$2
z=’expr $a + $b’
echo “The sum is $z”
}
add $1 $2
(
2
)设置权限
[root@localhost bin]#chmod +x test18
(3)
执行
[root@localhost bin]#./ test18 10 20
The sum is 30
注意
:函数定义完成后必须同时写出函数的调用,然后对此文件进行权限设定,在执行此文件。
Shell脚本中调用其他脚本
实例 :在Shell脚本test19中调用test20。
#test19脚本
#!/bin/sh
echo “The main name is $0”
./test20
echo “The first string is $1”
#test20脚本
#!/bin/sh
echo “How are you $USER?”
(
2
)设置权限
[root@localhost bin]#chmod +x test19
[root@localhost bin]#chmod +x test20
(
3
)执行
[root@localhost bin]#./ test19 abc123
The main name is ./test19
How are you root?
the first string is abc123
注意
:
1)在Linux编辑中命令区分大小写字符。
2)在Shell语句中加入必要的注释,以便以后查询和维护,注释以#开头。
3)对Shell变量进行数字运算时,使用乘法符号“*”时,要用转义字符“\”进行转义。
4)由于Shell对命令中多余的空格不进行任何处理,因此程序员可以利用这一特性调整程序缩进,达到增强程序可读性效果。
5)在对函数命名时最好能使用有含义且能容易理解的名字,即使函数名能够比较准确地表达函数所完成的任务。同时建议对于较大的程序要建立函数名和变量命名对照表。
转载地址:http://blog.csdn.net/wwp1026/article/details/6891359#comments
转载的文中有很多错误:这里有的地方修改了,有的还没有修改。一起以实际运行效果为准