shell脚本编程简介
<1>. 什么是shell1.什么是shell
2. 还是hello world程序
3. shell中的变量
3.1 系统变量
3.2 用户定义变量
3.2.1 用户定义变量规则
3.2.3 shell如何使用变量
3.2.3 全局变量 vs 局部变量
4. shell编程中的控制结构
4.1 条件判定
4.1.1 简单条件判定
4.1.2 组合判定
4.2if - else
4.3 for
4.4 while
4.5 case
5. shell中的函数
5.1 函数声明和定义
5.2 函数调用
6. shell脚本调试
6.1 万能的echo
6.2两个命令
7. 参考资料及shell脚本下载
shell扮演者操作系统内核和用户的中间人的角色,用户通过键入shell command,然后shell通过解析用户输入,然后将请求转发给操作系统的内核进行处理。
1. 一个系统可以存在多个shell,可以通过cat /etc/shells命令查看系统中安装的shell,不同的shell可能支持的命令语法是不相同的。
2. 可以通过echo $SHELL查看当前使用的shell
<2>. 还是hello world程序首先使用vim编辑器(或者是linux下任意的文本编辑器)编写文件helloshell.sh(没有必要使用.sh后缀名):
#!/bin/bash
echo "hello shell";
保存上面的文件,增加该文件的执行权限:
xuqiang@ubuntu:~/shell$ sudo chmod +x ./helloshell.sh
运行该shell程序:
xuqiang@ubuntu:~/shell$ ./helloshell.sh
hello shell
通过上面的程序没有什么实际的含义,但是通过第一个shell程序了解shell程序的执行过程。
<3>. shell中的变量3.1 系统变量linnux下的shell脚本中的变量分为“系统变量”和“用户自定义变量”,可以通过set命令查看那系统变量。
xuqiang@ubuntu:~/shell$ set
... 略去内容
xuqiang@ubuntu:~/shell$ echo $HOME
/home/xuqiang
3.2 用户定义变量shell中用户可以自定义变量,shell中的变量是没有数据类型的,shell将根据当前的环境自动进行转化,例如:
msg="hello world"
上面的语句定义变量msg,并设置初始值是为hello world。
Tip 1. 需要注意的是定义变量时,=两边是没有空格的
3.2.1用户定义变量规则
变量必须是以字母开头,后跟字母或者是下划线,变量的命名是大小写敏感的,并且可以定义一个变量的值为NULL。
xuqiang@ubuntu:~/shell$ vech=
xuqiang@ubuntu:~/shell$ echo $vec
3.2.2shell中如何使用变量
如果想要得到shell变量中存储的值的话,需要在变量名前增加$符号,例如:
xuqiang@ubuntu:~/shell$ vech="value"
xuqiang@ubuntu:~/shell$ echo $vech # this will print the value of vech
value
xuqiang@ubuntu:~/shell$ echo vech # this will print the string "vech"
vech
3.2.3 全局变量 vs 局部变量
默认在shell中编写的变量全部是局部变量,如果重新打开console的话,那么这些变量将全部丢失,全局的变量可以写在文件~/.bashrc文件。
<4>. 控制结构4.1 条件判定在shell中条件判断是通过test命令或者是[ ]实现, 判断条件如下:
数学运算:
a -eq b :a == b
a -ne b : a != b
a -lt b : a < b
a -le b : a <= b
a -gt b : a > b
a -ge b : a >= b
string比较:
string1 = string2
string1 != string2
string1 : string1 不是NULL,或者是没有定义
逻辑运算符:
! : not
exp1 -a exp2 : a && b
exp1 -o exp2 : exp1 || exp2
4.3 if-else结构:!/bin/sh#
#seewhetherargumentsispositive
#
if[$#-ne1]
then
echo"$0:youmustgive/supplyoneintegers"
exit1
fi
iftest$1-gt0
then
echo"$1numberispostivie"
else
echo"$1numberisnegative"
fi
#!/bin/sh
osch=0
echo"1.unix(sunos)"
echo"2.linux(redhat)"
echo-n"selectyouroschoice[1or2]?"
readosch
if[$osch-eq1]
then
echo"youpickupunix"
else
#
#nestedif
if[$osch-eq2]
then
echo"youpickuplinux"
else
echo"whatyoudonotlikeunix/linux"
fi
fi
#!/bin/sh
#
#testtheif..elif..else
#
if[$1-gt0];then
echo"$1ispositive"
elif[$1-lt0];then
echo"$1isnegative"
elif[$1-eq0];then
echo"$1iszero"
else
echo"$1isnotanumber"
fi
!/bin/sh
#
#testfor
#
foriin12345
do
echo"welcome$itimes"
done
#!/bin/bash
for((i=0;i<=5;i++))
do
echo"welcome$itimes"
done
Tip 1. 注意程序中使用的shell脚本的类型
#!/bin/bash
for((i=1;i<=5;i++))
do
for((j=1;j<=5;++j))
do
echo-n"$i"
done
#printanewline
echo""
done
#!/bin/bash
#
#testwhileloop
#
n=$1
i=0
while[$i-le10]
do
echo"$n*$i=`expr$i\*$n`"
i=`expr$i+1`
done
4.6 case#!/bin/bash
#
#scripttotestcasestatement
#
action="update"
case$actionin
"update")
echo"updatethedb"
;;
"select")
echo"selectfromdb"
;;
"delete")
echo"deletefromdb"
;;
*)
echo"noaction"
;;
esac
<5>. 函数5.1 函数声明和定义下面的程序中定义函数demo,向函数传递的所有参数表示为$*,第一个参数$1,第二个参数$2, 依次类推。
#!/bin/bash
functiondemo()
{
echo"allfunctionargs:$*"
echo"thefirstarg:$1"
echo"thesecondarg:$2"
echo"thethirdarg:$3"
}
#callthefunction
demo-ffoobar
5.2 函数调用(函数参数)shell中向函数传递参数是通过直接在函数调用后面添加参数完成,在函数中,传递的参数通过$1得到。
<6>. 脚本调试6.1 万能的echoshell的脚本调试是比价恶心的,这里仅仅是提供一些常规性的调试方法,最简单的就是使用echo函数打印出变量的值从而达到调试目的。
6.2 两个命令shell脚本执行可以通过./shell-filename.sh的形式执行,另外的一种形式是通过bash ./shell-filename.sh的形式执行,同时在执行脚本时,可以使用-v或者是-x参数打印程序执行信息。
-v:默认打印shell读取的内容
-x:将命令“展开” 之后打印
例如对于下面的程序:
!/bin/sh
#fordebugshellscript
#
tot=`expr$1+$2`
echo$tot
如果使用-v选项,结果如下:
xuqiang@ubuntu:~/shell$ sh -v ./debug.sh 2 5
#!/bin/sh
#
# for debug shell script
#
tot=`expr $1 + $2`
echo $tot
7
如果是使用-x选项的话,结果:
xuqiang@ubuntu:~/shell$ sh -x ./debug.sh 2 5
+ expr 2 + 5
+ tot=7
+ echo 7
7
转载地址:http://www.cnblogs.com/xuqiang/archive/2011/04/27/2031034.html
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。