您的位置:首页 > 运维架构 > Shell

linux shell编程

2014-07-16 17:22 387 查看
转:/article/1923854.html

1.什么是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.2 if - 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脚本下载




<1>.
什么是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.2 shell中如何使用变量
如果想要得到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,或者是没有定义


4.2 组合判定

逻辑运算符:
! : not
exp1 -a exp2 : a && b
exp1 -o exp2 : exp1 || exp2


4.3 if-else结构:





!/bin/sh#

# see whether
arguments is positive

#

if
[ $# -ne1
]

then

echo"$0
: you must give/supply one integers"

exit1

fi

if
test $1
-gt 0

then

echo"$1
number is postivie"

else

echo"$1
number is negative"

fi









#!/bin/sh

osch=0

echo"1.
unix(sun os)"

echo"2.
linux(red hat)"

echo
-n "select
your os choice [1 or 2] ?"

read osch

if
[ $osch -eq1
]

then

echo"you
pick up unix"

else

#

# nestedif

if
[ $osch -eq2
]

then

echo"you
pick up linux"

else

echo"what
you donot like unix/linux"

fi

fi









#!/bin/sh

#

# test theif..
elif..else

#

if
[ $1
-gt 0 ];then

echo"$1
is positive"

elif [ $1
-lt 0 ];then

echo"$1
is negative"

elif [ $1
-eq 0 ];then

echo"$1
is zero"

else

echo"$1
is not a number"

fi






4.4 for





!/bin/sh
#

# testfor

#

for
i in 12345

do

echo"welcome
$i times"

done









#!/bin/bash

for((
i=0;
i <=5;
i++))

do

echo"welcome
$i times"

done






Tip
1. 注意程序中使用的shell脚本的类型





#!/bin/bash

for((
i =1;
i <=5;
i++))

do

for((
j =1;
j <=5;++j))

do

echo
-n "$i"

done

#print
a new line

echo""

done






4.5 while





#!/bin/bash

#

# test while
loop

#

n=$1

i=0

while [ $i
-le 10 ]

do

echo"$n
* $i = `expr $i \* $n`"

i=`expr$i+1`

done






4.6 case





#!/bin/bash

#

# script to
test case statement

#

action="update"

case $action
in

"update")

echo"update
the db"

;;

"select")

echo"select
from db"

;;

"delete")

echo"delete
from db"

;;

*)

echo"no
action"

;;

esac








<5>.
函数


5.1 函数声明和定义

下面的程序中定义函数demo,向函数传递的所有参数表示为$*,第一个参数$1,第二个参数$2, 依次类推。





#!/bin/bash

function demo()

{

echo"all
function args : $*"

echo"the
first arg : $1"

echo"the
second arg : $2"

echo"the
third arg : $3"

}

#call
the function

demo
-f foo bar






5.2 函数调用(函数参数)

shell中向函数传递参数是通过直接在函数调用后面添加参数完成,在函数中,传递的参数通过$1得到。



<6>.
脚本调试


6.1 万能的echo

shell的脚本调试是比价恶心的,这里仅仅是提供一些常规性的调试方法,最简单的就是使用echo函数打印出变量的值从而达到调试目的。


6.2 两个命令

shell脚本执行可以通过./shell-filename.sh的形式执行,另外的一种形式是通过bash ./shell-filename.sh的形式执行,同时在执行脚本时,可以使用-v或者是-x参数打印程序执行信息。
-v:默认打印shell读取的内容
-x:将命令“展开” 之后打印
例如对于下面的程序:

!/bin/sh
#fordebugshell
script

#

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




<7>.
参考资料及shell代码下载

参考资料:http://www.freeos.com/guides/lsst/index.html

shell脚本下载:/Files/xuqiang/shell.rar

操作系统助教时的ppt:/Files/xuqiang/Shell编程.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: