linux shell编程
来源:互联网 发布:淘宝小二是最赚钱的吗 编辑:程序博客网 时间:2024/06/03 17:07
转:http://blog.csdn.net/xsl1990/article/details/8228161
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通过解析用户输入,然后将请求转发给操作系统的内核进行处理。
<2>. 还是hello world程序
首先使用vim编辑器(或者是linux下任意的文本编辑器)编写文件helloshell.sh(没有必要使用.sh后缀名):
#!/bin/bash
保存上面的文件,增加该文件的执行权限:
xuqiang@ubuntu:~/shell$ sudo chmod +x ./helloshell.sh
运行该shell程序:
xuqiang@ubuntu:~/shell$ ./helloshell.sh
通过上面的程序没有什么实际的含义,但是通过第一个shell程序了解shell程序的执行过程。
<3>. shell中的变量
3.1 系统变量
linnux下的shell脚本中的变量分为“系统变量”和“用户自定义变量”,可以通过set命令查看那系统变量。
... 略去内容
3.2 用户定义变量
shell中用户可以自定义变量,shell中的变量是没有数据类型的,shell将根据当前的环境自动进行转化,例如:
msg="hello world"
上面的语句定义变量msg,并设置初始值是为hello world。
Tip 1. 需要注意的是定义变量时,=两边是没有空格的
3.2.1 用户定义变量规则
变量必须是以字母开头,后跟字母或者是下划线,变量的命名是大小写敏感的,并且可以定义一个变量的值为NULL。
3.2.2 shell中如何使用变量
如果想要得到shell变量中存储的值的话,需要在变量名前增加$符号,例如:
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结构:
# 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
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
#
# 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
# testfor
#
for i in 12345
do
echo"welcome $i times"
done
for(( i=0; i <=5; i++))
do
echo"welcome $i times"
done
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
#
# 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
#
# 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, 依次类推。
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:将命令“展开” 之后打印
例如对于下面的程序:
#
tot=`expr$1+$2`
echo$tot
如果使用-v选项,结果如下:
xuqiang@ubuntu:~/shell$ sh -v ./debug.sh 2 5
xuqiang@ubuntu:~/shell$ sh -x ./debug.sh 2 5
<7>. 参考资料及shell代码下载
参考资料:http://www.freeos.com/guides/lsst/index.html
shell脚本下载:/Files/xuqiang/shell.rar
操作系统助教时的ppt:/Files/xuqiang/Shell编程.rar
- Linux Shell编程
- Linux的shell编程
- linux shell编程摘要
- Linux shell编程
- Linux下Shell编程
- Linux的shell编程
- Linux Shell 编程基础
- Linux的shell编程
- Linux Shell编程(基础教程)
- Linux的shell编程
- Linux的shell编程
- linux Shell编程笔记
- Linux的shell编程
- Linux Shell编程入门
- LINUX/UNIX Shell编程
- linux shell编程总结
- linux Shell编程入门
- linux shell编程
- Linux基础学习-管道命令
- 利用WireShark进行DNS协议分析
- E - Speed Limit(2.1.1)
- 使用ajax获取oracle数据库表数据
- 提高效率必须改掉的7种习惯
- linux shell编程
- 关于document,bundle,沙盒的一些看法
- 黑马程序员_IO流学习笔记
- 【POJ】1515 Street Directions 无向图转有向图 边双连通+求桥
- 3g上网卡的驱动程序的自动更新web端架构文档
- ie 滤镜 向量旋转公式:
- Android中的图片的压缩
- Android4.0设置界面修改总结
- [机器学习系列] k-近邻算法(K–nearest neighbors)