shell编程入门
- 格式:doc
- 大小:70.00 KB
- 文档页数:25
Word资料 Shell程序设计
学习内容:
1. 什么是shell
2. 语法:变量、条件判断、程序控制
3. 函数
4. 命令和命令的执行
5. Here文档
6. 调试
7. Grep命令和正则表达式
8. Find命令
一、 什么是shell?
Shell是一个作为用户与Linux系统间接口的程序,它允许用户向操作系统输入需要执行的命令。在一个linux系统中可以安装多个shell,这些shell和其他程序一起环绕在linux内核四周。默认安装的标准是/bin/sh,它是GNU工具集中的bash。使用/bin/bash –version可以查看版本。
Word资料 Shell 历史版本:sh csh,、tcsh、 zsh ksh、pdksh bash
二、 shell脚本编写、运行、调试
1. 脚本均以 #!/bin/bash开头。
2. 脚本中的 ‘#’为注释符。
3. 使用exit为脚本设置返回一个退出码,注意 0 表示的是执行成功。
示例:
#!/bin/bash
# this is my first shell!
echo “helloword!”
exit 0
4. 设置脚本为可执行: chmod u+x filename.sh
5. 执行:./filename.sh
6. sh –x ./filename.sh
三、 shell语法
1、 变量:无需事先声明,直接使用,在访问时要加$在变量前。
例如:
Str=hello
echo $Str
a.这两行语句将在屏幕输出一个“hello”,shell区别大小写,定义变量时应该注意。
b.如果为变量所赋的字符串中含有空格,table或换行符应使用“”标示,例如 “hello word!”
Word资料 c.变量在引号中依然有效如echo “this is $Str!”依然会输出:this is hello!
练习示例:(注:后续的示例中只有关键部分,练习时补全)
echo “input password:”
read pasd
echo “the password is $pasd”
d.如果需要输出字符$Str可以用单引号 和 \来处理:
‘$Str’ 和\$Str 输出的都是 $Str不是变量表示的值,shell中的变量一般都是字符串形式。
e: 环境变量,介绍几种:
$HOME 当前用户的家目录
$PATH 搜索命令的目录列表
$0 shell脚本的名字
$# 传递给脚本的参数个数
$$ 脚本的进程号
f:参数变量:
$1,$2脚本的程序参数
$* 列出所有参数
$@ S*的一种变体,它参数不会挤在一块。
g:变量做整形数字使用:
a=123
let “a +=1”
Word资料 echo “a = $a”
2、 条件
1) 使用 test 或 [ ]
如 if test –f file.c
then
…..
fi
以上代码也可写成如下形式(尽量使用这种形式)
if [ -f file.c ] (注意:if语句和[之间有个空格]
then
….
fi
完整示例:
#!/bin/bash
echo “is it morning? Please answer yes or no”
read timeofday
if [ $timeofday = “yes” ] ;then
echo “good morning”
else
echo “good afternoon”
fi
exit 0
2) elif语句,在if,else语句中增加分支判断。
Word资料 如下示例:
#!/bin/bash
echo “is it morning? Please answer yes or no”
read timeofday
if [ $timeofday = “yes” ] ;then
echo “good morning”
elif [ $timeofday = “no” ]
then
echo “Good afternoon”
else
echo “sorry ,$timeofday not recognized. Enter yes or no”
exit 1
fi
exit 0
3、 一个与变量有关的问题。
如上程序,如果没有输入Yes和no直接按回车键,会出现什么结果呢?程序会有出错信息。原因是。。。避免该问题产生的方法对变量使用双引号“$Str”
4、 for语句
使用for语句可以循环处理一组值,这组值可以是任意字符串的集合。 它的语法形式如下:
for variable in values
Word资料 do
something
done
示例:
#!/bin/bash
For foo in hello myname 129
do
echo $foo
done
exit 0
使用通配符扩展for循环
#!/bin/sh
for file in $(ls *.sh);do
echo $file
done
exit 0
5、while语句
语法结构:
while condition do
Dosometing
done
示例:一个简陋的密码检查程序
Word资料 #!/bin/bash
echo “Enter password”
read paword
while [ “$paword” != “secret” ]
do
echo “sorry, try again ”
read paword
done
exit 0
字符串比较:
string = string
string != string
-n string
-z string
算数比较
num1 –eq num2
num1 –ne num2
6、until语句,与while循环类似,只是把测试条件反过来了。
语法形式如下:
until condition
do
Word资料 Dosomething
done
7、case语句
语法结构:
case variable in
pattern1 [ | pattern] …) dosomething ;;
pattern2 [ | pattern] …) dosomething ;;
esac
该语句 执行 vairable与第一个pattern匹配上的语句。
例如:
#!/bin/bash
echo “is it morning? Please answer yes or no”
read timeofday
case “$timeofday” in
yes ) echo “good moring”;;
no ) echo “good afternoon”;;
y) echo “good morning”;;
n) echo “good afternoon”;;
*)echo “sorry,answer not recognized” ;;
esac
exit 0
匹配部分语句也可改写为:
Word资料 case “$timeofday” in
yes | y | Yes | YES ) echo “good morning”;;
n* | N* ) echo “good afternoon”;;
*) echo “sorry, answer not recognized”;;
esac
也可以将匹配行改为; [yY]|[Yy][Ee][Ss]
8、命令列表
有时需要将好几条命令连接成一个序列。如下
if [ -f this_file ]; then
if [ -f that_file ]; then
if [ -f the_other_file ]; then
echo “all files present, and correct”
fi
fi
fi
9、AND列表
只有在前一条命令执行成功返回true才执行下一条。语法结构:
Statement1 && Statement1 && Statement1 && …
示例:
#!/bin/bash
touch file_one
Word资料 rm –f file_two
if [ -f file_one ] && echo “hello” && [ -f file_two ] && echo “there”
then
echo “in if”
else
echo “in else”
fi
exit 0
10、OR列表 ,直到有一条命令执行成功为止。
语法结构:
Statement1 || Statement2 || Statement3 || …
示例:
if [ -f file_one ] || echo “hello” || echo “there”
then
echo “in if”
else
echo “in else”
fi
11、语句块
如果想在只允许使用单个语句的地方(如 AND,OR)使用多条语句,可以是花括号{}来构造一个块。如下:
Get_confirm && {