Bash Shell结构
Korn和Bash shells非常相似,但是还是有一些不同之处。Bash的结构如下所示。
Bash Shell语法结构
Shbang行
|
"shbang" 是脚本起始行,告诉kernel那个shell解析. #!位于行头。例如#!/bin/bash
|
注释
|
行注释用#符号.例如:# This is a comment
|
通配符
|
例如*, ?, 和 [ ] 用于文件名扩展。<, >, 2>, >>, 和 | 符号用于IO和重定向和管道。为了保证这些符号不被解析,这个字符要被引起来。 例如:
rm *; ls ??; cat file[1-3];
echo "How are you?"
|
输出显示
|
使用echo命令。使用`或者一对“”通配符。例如:
echo "How are you?"
|
局部变量
|
局部变量作用于当前shell,shell结束时局部变量失效.例如
variable_name=value
declare variable_name=value
name="John Doe"
x=5
|
全局变量
|
全局变量也称为环境变量. 例如:内建的带-x选项的声明函数也可以设置为环境变量。可以用export使用。例如:
export VARIABLE_NAME=value
declare -x VARIABLE_NAME=value
export PATH=/bin:/usr/bin:.
|
从变量中提取值
|
使用$.例如:
echo $variable_name
echo $name
echo $PATH
|
读取用户输入
|
使用read读入一行。例如:
EXAMPLE
echo "What is your name?"
read name
read name1 name2 ...
|
参数
|
可以从命令行传入参数。位置参数用于从脚本中接收值。例如:
At the command line:
$ scriptname arg1 arg2 arg3 ...
在脚本中:
|
echo $1 $2 $3
|
位置参数
|
echo $*
|
所有位置参数
|
echo $#
|
位置参数号
|
数组
|
Bourne shell使用位置参数创建单词列表。除了位置参数外, Bash shell支持数组语法,起始索引是0。 Bash shell数组使用declare -a 命令创建。例如:
set apples pears peaches (positional parameters)
echo $1 $2 $3
declare -a array_name=(word1 word2 word3 ...)
declare -a fruit=( apples pears plums )
echo ${fruit[0]}
|
算术
|
像C/TC shells和Bourne shell, UNIX/Linux 命令的输出可以指定到一个变量。Bash shell提供新的语法. 使用前端加$,例如:
variable_name=`command`
variable_name=$( command )
echo $variable_name
echo "Today is `date`"
echo "Today is $(date)"
|
算术
|
Bash shells支持整数算术。declare -i 命名用于声明一个整型变量。Korn shell的typeset命令也可以用于向后兼容。
|
例如
|
declare -i variable_name
|
used for bash
|
typeset -i variable_name
|
can be used to be compatible with ksh
|
(( n=5 + 5 ))
echo $n
|
操作符
|
Bash shell 使用内建命令,类似于C语言。
|
例如
|
相等性:
|
逻辑性:
|
==
|
equal to
|
&&
|
and
|
!=
|
not equal to
|
||
|
or
|
|
|
!
|
not
|
关系型:
|
>
|
greater than
|
>=
|
greater than, equal to
|
<
|
less than
|
<=
|
less than, equal to
|
条件语句
|
If类似于C语言。if 用endif结束。 [[ ]] 用于模式匹配条件表达式。 [ ] 用于向后兼容Bourne shell。例如:
|
The if construct is:
if command
then
block of statements
fi
if [[ expression ]]
then
block of statements
fi
if (( numeric expression ))
then
block of statements
else
block of statements
fi
The if/else construct is:
if command
then
block of statements
else
block of statements
fi
if [[ expression ]]
then
block of statements
else
block of statements
fi
if (( numeric expression ))
then
block of statements
else
block of statements
fi
The case construct is:
case variable_name in
pattern1)
statements
;;
pattern2)
statements
;;
pattern3)
;;
esac
case "$color" in
blue)
echo $color is blue
;;
green)
echo $color is green
;;
red|orange)
echo $color is red or orange
;;
*) echo "Not a matach"
;;
esac
|
The if/else/else if construct is:
if command
then
block of statements
elif command
then
block of statements
else if command
then
block of statements
else
block of statements
fi
-------------------------
if [[ expression ]]
then
block of statements
elif [[ expression ]]
then
block of statements
else if [[ expression ]]
then
block of statements
else
block of statements
fi
--------------------------
if (( numeric expression ))
then
block of statements
elif (( numeric expression ))
then
block of statements
else if ((numeric expression))
then
block of statements
else
block of statements
fi
|
循环
|
四种循环while, until, for, 和select.
while循环后跟随[],do关键字,代码段,结束于done关键字。[[ ]]是新的测试操作符,老的[ ]仍旧向后兼容Bourne shell.
until循环类似于while循环。
for循环用于遍历一个字列表。For循环后跟随变量名,in关键字,字列表,代码块,结束于done关键字。
select循环用于提示菜单选择。
循环控制命令是break和continue。
|
例如
while command until
command
do do
block of statements
block of statements
done done
-------------------------
---------------------------
while [[ string expression ]] until
[[ string expression ]]
do do
block of statements block
of statements
done done
------------------------- ----------------------------
while (( numeric expression )) until
(( numeric expression ))
do do
block of statements
block of statements
done done
for variable in word_list
select variable in word_list
do do
block of statements block
of statements
done done
--------------------------
----------------------------
for color in red green b
PS3="Select an item from the menu"
do do
item in blue red green
echo $color echo
$item
done done
|
|
Shows menu:
- blue
- red
- green
|
函数
|
函数有两种格式.一种格式来自于Bourne shell, Bash版本使用function关键字:例如
function_name() {
block of code
}
function function_name {
block of code
}
------------------------
function lister {
echo Your present working directory is `pwd`
echo Your files are:
ls
}
|
|
|
|
|
|
|
Bash Shell脚本例子:
1 #!/bin/bash
# GNU bash versions 2.x
2 # The Party Program––Invitations to friends from the "guest" file
3 guestfile=~/shell/guests
4 if [[ ! –e "$guestfile" ]]
then
5 printf "${guestfile##*/} non–existent"
exit 1
fi
6 export PLACE="Sarotini's"
7 (( Time=$(date +%H) + 1 ))
8 declare -a foods=(cheese crackers shrimp drinks `"hot dogs"` sandwiches)
9 declare -i n=0
10 for person in $(cat $guestfile)
do
11 if [[ $person == root ]]
then
continue
else
# Start of here document
12 mail –v –s "Party" $person <<- FINIS
Hi $person! Please join me at $PLACE for a party!
Meet me at $Time o'clock.
I'll bring the ice cream. Would you please bring
${foods[$n] and anything else you would like to eat?
Let me know if you can make it.
Hope to see you soon.
Your pal,
ellie@$(hostname)
FINIS
13 n=n+1
14 if (( ${#foods[*]} == $n ))
then
15 declare -a foods=(cheese crackers shrimp drinks `"hot dogs"`
sandwiches)
16 n=0
fi
fi
17 done
printf "Bye..."
解释
- 让kernel知道在运行Bash shell脚本.
- 注释行
- 变量guestfile被设置为文件的全路径名,叫做guests.
- 行读入
- 内建函数printf显示文件名
- 全局环境变量
- 数字表达式
- Bash数组foods定义
- 整数n定于初始值为0
- For循环
- If语句
- 发送mail消息
- 整数n加1
- If语句
- 数组foods重新分配值
- 变量n重新设置回0
- 循环结束
posted on 2008-07-08 09:38
一叶笑天 阅读(344)
评论(0) 编辑 收藏 所属分类:
Shell技术