什么是shell
简单地说,就是命令解析器,将用户输入的指令转换为相应的机器能够运行的程序。
- Bourne shell (sh)
- Korn shell (ksh)
- Bourne Again shell (bash)
- C shell (包括 csh and tcsh)
- TENEX/TOPS C shell (tcsh)
shell脚本:
一个包含一系列命令序列的文本文件。当运行这个脚本文件时,文件中包含的命令序列将得到执行。(展开、运行hello.sh)
shell脚本的第一行必须是如下格式:
#!/bin/sh
符号#!用来指定该脚本文件的解析程序。在上面例子中使用/bin/sh来解析该脚本。当编辑好脚本后,如果要执行该脚本,还必须使其具有可执行属性。
chmod +x filename
在进行shell编程时,以#开头的句子表示注释,直到这一行的结束。如果使用了注释,即使相当长的时间内没有使用该脚本,也能在很短的时间内明白该脚本的作用及工作原理。
在shell编程中,所有的变量都由字符组成,并且不需要预先对变量进行声明,
例:
#!/bin/sh
a="hello world"
echo "A is:"
echo $a
运行结果:
A is:
hello world
有时候变量名很容易与其他文字混淆。
例:
#!/bin/sh
num=2
echo "this is the $numnd"
运行结果:
[regry@localhost myshell]$ ./s2
this is the
我们可以看到打印的结果和我们的意愿并不相同,那怎么来改呢?
我做了下面的修改
#!/bin/sh
num=2
echo "this is the ${num}nd"
运行结果:
[regry@localhost myshell]$ ./s3
this is the 2nd
默认变量:
$#:传入脚本的命令行参数个数
$*:所有命令行参数值,在各个参数值之间留有空格
$0:命令本身(shell文件名)
$1:第一个命令行参数
...
例:
#!/bin/sh
echo "number of vars:" $#
echo "values of vars:"$*
echo "file of shell:"$0
echo "value of var1:"$1
echo "value of var2:"$2
echo "value of var3:"$3
echo "value of var4:"$4
运行结果:
[regry@localhost myshell]$ ./s4 2 4 5 6
number of vars: 4
values of vars:2 4 5 6
file of shell:./s4
value of var1:2
value of var2:4
value of var3:5
value of var4:6
局部变量:
在变量首次被赋值时加上local关键字可以声明一个局部变量,
例:
#!/bin/sh
hello="var1"
echo $hello
function func1
{
}
func1
echo $hello
运行结果:
[regry@localhost myshell]$ ./s5
var1
var2
var1
上面的程序如果改为:
#!/bin/sh
hello="var1"
echo $hello
function func1
{
}
func1
echo $hello
运行结果:
[regry@localhost myshell]$ ./s5
var1
var2
var2
在编辑shell脚本时,有几个点,我们特别要注意:
1、变量赋值时,“=”左右两边都不能有空格
2、BASH中的语句结尾不需要分号
if语句:
**********
if [ expression ]
then
fi
*******
if [ expression ]
then
else
fi
***********
if [ expresssion ]
then
else if [ expression ]
fi
比较操作
大于或等于
小于或等于
不为空
例:
比较整数a和b是否相等:if [ $a = $b ] (也可以用eq)
判断整数a是否大于整数b:if [ $a -gt $b ]
比较字符串a和b是否相等:if [ $a = $b ]
判断字符串a是否为空:if [ -z $a ]
判断整数变量a是否大于b:if [ $a -gt $b ]
注意:
1、在“[”和“]”符号的左右都留有空格
2、“=”左右都有空格
-e 文件已经存在
-f 文件时普通文件
-s 文件的大小不为零
-d 文件时一个目录
-r 文件对当前用户可以读取
-w 文件对当前用户可以写入
-x 文件对当前用户可以执行
例子:
[regry@localhost myshell]$ vi s7
#!/bin/sh
folder=/home
[ -r "$folder" ] && echo "can read
$folder"
[ -f "$folder" ] || echo " this is not file"
运行结果:
[regry@localhost myshell]$ ./s7
can read /home
this is not file
for循环
在bash中for循环的基本结构是:
for var in [list]
do
done
其中$var是循环控制变量,[list]是var需要遍历的一个集合,do/done对包含了循环体,相当于c语言中的一对大括号。另外如果do和for被写在同一行,必须在do前面加上“:”。如:for
$var in [list]: do
例:
#!/bin/bash
for day in sun mon tue wed thu fri sat
do
done
如果列表被包含在一对双引号中,则被认为是一个元素,例如:
#!/bin/bash
for day in "sun mon tue wed thu fri sat"
done
while循环:
while循环的基本结构是:
while [ condition ]
do
done
until 循环的基本结构是:
until [ condition ]
do
done
while 和 until的区别在于while是为真时执行,until是为假时执行
case语句
bash中的case结构与c语言中的switch语句的功能比较类似,可以用于进行多项分支控制。
case "$var" in
例子:
[regry@localhost myshell]$ vi s8
#!/bin/bash
echo "hit a key, then hit return."
read Keypress
case "$Keypress" in
esac
运行结果:
[regry@localhost myshell]$ ./s8
hit a key, then hit return.
a
lowercase letter
[regry@localhost myshell]$ ./s8
hit a key, then hit return.
A
uppercase letter
[regry@localhost myshell]$ ./s8
hit a key, then hit return.
1
digit