Linux之Shell编程
2013-09-11 14:13 hduhans 阅读(201) 评论(0) 收藏 举报Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核。Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行。
一、Shell基础知识
1、变量,分临时变量和永久变量。
1) 变量分类:
① 临时变量:是Shell程序内部定义的变量,使用范围仅限于定义它的程序,对其他程序不可见。
② 永久变量:是环境变量,值不随Shell脚本的执行结束而消失。
2) 变量赋值
使用字符串给变量命名时,单引号和双引号的区别如下:(假设 $NAME="hans")
① WELCOME="Hello,$NAME" --$WELCOME="Hello,hans"
② WELCOME='Hello,$NAME' --$WELCOME='Hello,$NAME'
3) 查看系统变量,set命令。格式:set。
4) 删除变量,unset命令。格式:unset 变量名,如 unset NAME,此处变量名不带$。
5) 一些特殊变量:
① $* --这个程序的所有参数
② $# --这个程序的参数个数
③ $$ --这个程序的PID
④ $! --执行上一个后台命令的PID
⑤ $? --执行上一个命令的返回值,值=0说明执行成功
⑥ $0 --位置变量,代表当前sh程序名
⑦ $1 --位置变量,代表第一个参数名
示例:
① 编写shell文件special.var,内容如下:
#!/bin/sh
# test special variable
# Useage:sh special.var file0 file1
echo '$# is:' $#
echo '$* is:' $*
echo '$$ is:' $$
echo '$0 is:' $0
echo '$1 is:' $1
echo '$2 is:' $2
echo '$? is': $?
② 执行程序命令:sh special.var file0 file1,执行结果如下所示:
$# is: 2 --两个参数
$* is: file0 file1 --具体参数
$$ is: 11486 --执行程序PID
$0 is: special.var --位置变量,$0表示程序本身
$1 is: file0 --位置变量,$1表示第一个参数
$2 is: file1 --位置变量,$2表示第二个参数
$? is: 0 --执行程序返回值为0,说明程序执行成功
注:① 变量名只能用字母表中的字符开头,不能使用数字,建议使用大写字母命名
② 变量赋值,等号两边不能有空格,如 NUM=10
③ 访问变量,使用 $+变量名
2、执行shell程序,使用shell对应的命令解释器,#!/bin/sh使用sh命令
1) sh 程序名 --执行脚本
sh -x 程序名 --执行脚本,显示详细提示
2) xxx.sh --直接执行脚本 需要将脚本权限设置为可执行(chmod u+x xxx.sh)
3、$()与``
都是用来替换命令执行结果,建议使用$()。
二、Shell命令
1、从键盘读入数据,read命令(使shell具有互动性):
语法:read 变量名 --读取键盘数据并复制给变量
示例:
① 编写脚本文件read.sh,内容如下:
#!/bin/sh
read first second third
echo "first parameter is $first"
echo "second parmeter is $second"
echo "third parmeter is $third"
② 执行脚本:sh read,输入:param1 param2 param3,执行结果如下所示:
first paramter is param1
second parmeter is param2
third parmeter is param3
注:① read读取变量时,多个变量使用空格间隔,不输入数据或数据个数不符合变量数,后几个变量赋值为空
② 输入变量个数大于程序要求的变量,程序会将多余的输入值赋给最后一个变量,如此例输入:100 200 300 400,则$first=100,$second=200,$third=300 400
2、算数运算,对整数型变量进行算数运算,expr命令:
▶ expr 3 + 8 --结果为11,注意3 + 8中必须有空格
▶ expr $var / 33 --$var变量除以33,结果为整数,如100 / 33=3
▶ expr 5 \* 8 --5*8,乘号用转义符\
▶ expr `expr 5 + 7` / 2 --使用优先级,先运算5+7,然后除2
▶ var3 = `expr $var1 / $var2` --$var1 / $var2赋值给$var3
三、变量测试语句
1、字符串测试
test str1=str2 --测试字符串是否相等
test str1!=str2 --测试字符串是否不相等
test str1 --测试字符串是否不为空
test -n str1 --测试字符串是否不为空
test -z str1 --测试字符串是否为空
2、整数测试
test int1 -eq int2 --测试整数是否相等
test int1 -ge int2 --测试int1是否>=int2
test int1 -gt int2 --测试int1是否>int2
test int1 -le int2 --测试int1是否<=int2
test int1 -lt int2 --测试int1是否<int2
test int1 -ne int2 --测试int1是否等于int2
3、文件测试
test -d file --测试指定文件是否目录
test -f file --测试指定文件是否常规文件
test -x file --测试指定文件是否可执行
test -r file --测试文件是否可读
test -w file --测试文件是否可写
test -a file --测试文件是否存在
test -s file --测试文件大小是否非0
4、说明
变量测试语句一般不单独使用,一般做为if语句的测试条件,如:
if test -d $1 then
...
fi
可简化如下:test -d $1 => [ -d $1 ]
5、一个应用实例。该实例可每隔固定时间检测apache服务器是否处于运行状态,若不处于运行状态,则启动服务。
1) 编写shell脚本detect.apache
#!/bin/sh #"if...else" useage #Using this program to show your system's services echo "start delect apache service" echo web=`/usr/bin/pgrep httpd` if [ "$web" != "" ] #注意中间有空格 then echo "Apache service is running" else echo "Apche service is not running" fi
2) 计划命令执行此脚本
四、流程控制语句
1、判断,if语句:
if 条件1 then
命令1
elif 条件2 then
命令2
else
命令3
fi
2、退出,exit语句:
exit 0 --正常退出
exit 非0 --非正常退出
3、循环,for语句:
格式:
for 变量 in 名字表
do
命令列表
done
范例:1) for name in AA BB CC DD
do
echo '$name=' $name
done
2) killuser.sh 一个根据用户名删除用户进程,剔除用户的脚本,使用命令:sh killuser.sh hans
#!/bin/sh #The script to kill login user username="$1" /bin/ps -aux | /bin/grep $username |/bin/awk '{ print $2 }' > /test/temp.pid pid_list=`/bin/cat /test/temp.pid` for pid in $pid_list do /bin/kill -9 $pid 2> /test/error done
注:名字表可用命令执行结果作为参数,如`ls`
4、select语句,select把关键字中的每一项做成类似表单,以交互的方式执行do和done之间的命令
格式:
select 变量 in 关键字
do
command 1
.....
command 2
done
范例: --select.sh 选择一个选项的标号,执行相应的命令
#!/bin/sh #"select" useage select var in "A" "B" "C" do break done echo "you choose" $var
5、case...esac语句(类似swich..case)
格式:
case 变量 in
字符串1) 命令列表1
;;
...
字符串n) 命令列表n
;;
esac
范例:--读取操作符,判断并执行相应处理
#!/bin/sh #"case..esac" useage read op case $op in C) echo "Copy" ;; D|E) --$op=D或E均执行此分支 echo "Delete" ;; *) --值不等于上述任何一个,进入这个分支 echo "Invalid" esac
6、while语句
格式:
while 条件
do
命令
done
范例:--while.sh 显示输出1到10的平方
#!/bin/sh #"while" useage i=1 while [ $i -le 10 ] do SUM=`expr $i \* $i` echo "$SUM" i=`expr $i + 1` done
7、until语句
格式:
until 条件 --当条件成立时退出循环,否则一直执行do和done之间的命令
do
命令
done
范例: --until.sh 直到输入Y或y才会结束结束执行循环
#!/bin/sh #"until" useage echo "Press Y/y to stop..." read input until [ "$input" = "Y" ] || [ "$input" = "y" ] do echo "error,try again..." read input done echo "Stop here!"
8、跳出循环,break和continue可使用
9、移除参数,shift命令。
在命令:sh shift 100 200 300 中,参数$*=100 200 300,参数个数$#=3,执行shift后,$*=200 300,参数个数$#=200 300
范例: --shift.sh 计算所有参数的和并输出
#!/bin/sh #"shift" useage if [ $# -le 0 ] then echo "parameters is not enough" exit 0 fi sum=0 while [ $# -gt 0 ] do sum=`expr $sum + $1` shift done echo $sum
10、定义函数
格式:函数名(){ ..//命令 }
调用:不带()
范例:
#!/bin/sh #function useage pap(){ echo "do fun(),a=$1,b=$2" } pap 1 2
说明:1) 函数中变量均为全局变量,没有局部变量
2) 调用函数时可以传递参数,函数体内使用$1,$2...来引用参数
3) 可以在脚本中通过语句:source xxx.sh引用其他通用函数脚本,然后使用函数
五、脚本调试
1、sh -x script --执行脚本并显示所有变量的值
2、sh -n script --不执行脚本,只检查语法模式,返回所有语法错误信息
浙公网安备 33010602011771号