shell学习笔记之控制结构(三)
2013-09-24 18:45
537 查看
1.if语句
例:
2.elif语句
潜在的问题:
如果用户不输入直接按下回车,if [ = "yes" ]这个判断就会出错
所以必须给timeofday变量添加上引号
修改后:
echo -n "hello echo"
-n选项去掉换行符
3.for语句
for variable in values
do
statements
done
例1.
输出:
bar
fud
43
例2.
输出:
bar fud 43
例3.
4.while语句
1.不知道命令序列要执行的次数
2.条件为真时反复执行
while condition do
statements
done
5.until语句
循环反复直到条件为真
until condition
do
statements
done
while:循环至少执行一次
unitl:可能根本不需要执行循环
6.case语句
case variable in
pattern [ | pattern ] ...) statements;;
pattern [ | pattern ] ...) statements;;
...
esac
双分号标记前一个语句的结束和后一个模式的开始
7.命令列表
①AND列表
statement1 && statement2 && statement3 && ...
②OR列表
statement1 || statement2 || statement3 || ...
[ -f file_one ] && command for true || command for false
如果测试成功会执行第一条命令,否则执行第二条命令
③语句块
8.函数
定义:
必须在调用之前定义
function_name(){
statements
}
注意:
1.如果函数里面没有return一个值,函数返回的就是执行最后一条命令的返回码。
2.local关键字在函数中声明一个局部变量,局部变量仅在函数的作用范围内有效。
3.函数可以访问全局作用范围内的其他shell变量。
4.如果一个局部变量和一个全局变量名字相同,前者会覆盖后者,但仅限于函数的作用范围内。
5.让函数返回字符串值的常用的方法:
1>让函数将字符串保存在一个变量中,该变量然后可以在函数结束之后被调用
2>echo一个字符串并捕获其结果
foo(){
echo JAY;
}
...
result="$(foo)"
①参数如何传递
②函数返回值
执行结果
$./my_name Rick Neil
Original parameters are Rick Neil
Is your name Rick?
Enter yes or no:
yes
Hi Rick, nice name
if condition then statements esle statements fi
例:
#!/bin/sh 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语句
#!/bin/sh 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!" exit 1 fi exit 0
潜在的问题:
如果用户不输入直接按下回车,if [ = "yes" ]这个判断就会出错
所以必须给timeofday变量添加上引号
修改后:
#!/bin/sh 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!" exit 1 fi exit 0
echo -n "hello echo"
-n选项去掉换行符
3.for语句
for variable in values
do
statements
done
例1.
#!/bin/sh for foo in bar fud 43 do echo $foo done exit 0
输出:
bar
fud
43
例2.
#!/bin/sh for foo in "bar fud 43"#当做一个字符串 do echo $foo done exit 0
输出:
bar fud 43
例3.
#打印当前目录中所有以字母f开头并且.sh结尾的脚本文件 #!/bin/sh for file in $(ls f*.sh); do lpr $file done exit 0
4.while语句
1.不知道命令序列要执行的次数
2.条件为真时反复执行
while condition do
statements
done
#!/bin/sh echo "Enter password" read trythis while [ "$trythis" != "secret" ]; do echo "sorry, try again" read trythis done exit 0
5.until语句
循环反复直到条件为真
until condition
do
statements
done
while:循环至少执行一次
unitl:可能根本不需要执行循环
#!/bin/bash until who | grep "$1" > /dev/null do sleep 60 done echo -e '\a' #响铃发出警报 echo "***$1 has just logged in****" exit 0
6.case语句
case variable in
pattern [ | pattern ] ...) statements;;
pattern [ | pattern ] ...) statements;;
...
esac
双分号标记前一个语句的结束和后一个模式的开始
#!/bin/bash echo "Is it morning? Please answer yes or no" read timeofday case "$timeofday" in yes) echo "Good Morning";; no ) echo "Good Afternoon";; y ) echo "Good Morning";; n ) echo "Good Afternoon";; * ) echo "sorry";; esac exit 0
#!/bin/bash echo "Is it morning? Please answer yes or no" read timeofday case "$timeofday" in yes | y | Yes | YES ) echo "Good Morning";; n* | N* ) echo "Good Afternoon";; * ) echo "sorry";; esac exit 0
#!/bin/bash echo "Is it morning? Please answer yes or no" read timeofday case "$timeofday" in yes | y | Yes | YES ) echo "Good Morning" echo "Up bright and early this morning" ;; [nN]*) echo "Good Afternoon" ;; * ) echo "sorry" exit 1 ;; #如果最后一个case模式是默认模式,可以省略最后一个双分号;; #[yY] | [Yy] [Ee] [Ss]) esac exit 0
7.命令列表
①AND列表
statement1 && statement2 && statement3 && ...
#!/bin/sh touch file_one 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
②OR列表
statement1 || statement2 || statement3 || ...
#!/bin/sh rm -f file_one if [ -f file_one ] || echo "hello" || echo "there" then echo "in if" else echo "in else" fi exit 0
[ -f file_one ] && command for true || command for false
如果测试成功会执行第一条命令,否则执行第二条命令
③语句块
get_confirm && { grep -v "$cdcatnum" $tracks_file > $temp_file cat $temp_file > $tracks_file echo add_record_tracks }
8.函数
定义:
必须在调用之前定义
function_name(){
statements
}
#!/bin/sh foo() { echo "Function foo is executing" } echo "script starting" foo echo "script ended" exit 0
注意:
1.如果函数里面没有return一个值,函数返回的就是执行最后一条命令的返回码。
2.local关键字在函数中声明一个局部变量,局部变量仅在函数的作用范围内有效。
3.函数可以访问全局作用范围内的其他shell变量。
4.如果一个局部变量和一个全局变量名字相同,前者会覆盖后者,但仅限于函数的作用范围内。
5.让函数返回字符串值的常用的方法:
1>让函数将字符串保存在一个变量中,该变量然后可以在函数结束之后被调用
2>echo一个字符串并捕获其结果
foo(){
echo JAY;
}
...
result="$(foo)"
#!/bin/sh sample_text="global variable" foo() { local sample_text="local variable" echo "Function foo is executing" echo $sample_text } echo "script starting" echo $sample_text foo echo "script ended" echo $sample_text exit 0
①参数如何传递
②函数返回值
#!/bin/sh yes_or_no(){ echo "Is your name $*" while true do echo -n "Enter yes or no:" read x case "$x" in y | yes ) retrun 0;; n | no ) return 1;; *) echo "Answer yes or no";; esac done } echo "Original parameters are $*" if yes_or_no "$1" then echo "Hi $1, nice name" else echo "Never mind" fi exit 0
执行结果
$./my_name Rick Neil
Original parameters are Rick Neil
Is your name Rick?
Enter yes or no:
yes
Hi Rick, nice name
相关文章推荐
- shell学习笔记:控制结构
- Oracle PLSQL 学习笔记(块、控制结构、过程、函数、包)
- php学习笔记(三)――操作符与控制结构
- scala快速学习笔记(二):控制结构,类和对象
- Scala学习笔记-控制结构
- PHP第一季视频教程.李炎恢.学习笔记(五)(第3章 操作符与控制结构(1)(2))
- Scala学习笔记2 - 控制结构和函数
- shell 笔记 控制结构
- LINUX系统学习笔记管道符和作业控制 ,shell变量 , 环境变量配置文件
- shell学习--控制结构
- PHP学习笔记(25)流程分支控制结构 (26)PHP实现简单计算器
- linux shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select 学习笔记
- php学习笔记(三)操作符与控制结构
- 【Linux学习笔记】Shell 基础(3) — Shell流程控制语句
- PHP学习笔记——流程控制概述, PHP分支结构;
- Shell脚本学习笔记(四)--流程控制
- Python学习笔记(3)控制结构
- Java之学习笔记(8)-------------运算符和循环结构(流程控制)
- Shell学习笔记(11)-流程控制之select、until语句
- Lua 学习笔记(四)语句与控制结构