shell学习笔记之控制结构(三)
1.if语句
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
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
fi exit
潜在的问题:
如果用户不输入直接按下回车,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
fi exit
echo -n "hello echo"
-n选项去掉换行符
3.for语句
for variable in values
do
statements
done
例1.
#!/bin/sh for foo in bar fud
do
echo $foo
done exit
输出:
bar
fud
43
例2.
#!/bin/sh
for foo in "bar fud 43"#当做一个字符串
do
echo $foo
done
exit
输出:
bar fud 43
例3.
#打印当前目录中所有以字母f开头并且.sh结尾的脚本文件
#!/bin/sh
for file in $(ls f*.sh); do
lpr $file
done
exit
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
5.until语句
循环反复直到条件为真
until condition
do
statements
done
while:循环至少执行一次
unitl:可能根本不需要执行循环
#!/bin/bash until who | grep "$1" > /dev/null
do
sleep
done echo -e '\a' #响铃发出警报
echo "***$1 has just logged in****"
exit
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
#!/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
#!/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
;; #如果最后一个case模式是默认模式,可以省略最后一个双分号;;
#[yY] | [Yy] [Ee] [Ss])
esac exit
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
②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
[ -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
注意:
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
①参数如何传递
②函数返回值
#!/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 ;;
n | no ) return ;;
*) 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
执行结果
$./my_name Rick Neil
Original parameters are Rick Neil
Is your name Rick?
Enter yes or no:
yes
Hi Rick, nice name
shell学习笔记之控制结构(三)的更多相关文章
- SHELL学习笔记三
SHELL学习笔记一 SHELL学习笔记二 SHELL学习笔记三 for 命令 读取列表中的复杂值 从变量读取列表 从命令读取值 更改字段分隔符 用通配符读取目录 which 使用多个测试命令 unt ...
- [转帖][Bash Shell] Shell学习笔记
[Bash Shell] Shell学习笔记 http://www.cnblogs.com/maybe2030/p/5022595.html 阅读目录 编译型语言 解释型语言 5.1 作为可执行程序 ...
- shell学习笔记
shell学习笔记 .查看/etc/shells,看看有几个可用的Shell . 曾经用过的命令存在.bash_history中,但是~/.bash_history记录的是前一次登录前记录的所有指令, ...
- shell学习笔记汇总
1.shell脚本中函数使用 函数定义在前,调用在后,顺序反了就没有效果了.函数调用为:函数名 参数列表 函数内部通过以下变量访问函数的参数:shell脚本函数中: $0: 这个脚本的名字 $n: 这 ...
- shell 学习笔记2-shell-test
一.字符串测试表达式 前面一篇介绍:什么是shell,shell变量请参考: shell 学习笔记1-什么是shell,shell变量 1.字符串测试表达式参数 字符串需要用""引 ...
- 【Unity Shaders】学习笔记——SurfaceShader(三)BasicDiffuse和HalfLambert
[Unity Shaders]学习笔记——SurfaceShader(三)BasicDiffuse和HalfLambert 转载请注明出处:http://www.cnblogs.com/-867259 ...
- SHELL学习笔记----IF条件判断,判断条件
SHELL学习笔记----IF条件判断,判断条件 前言: 无论什么编程语言都离不开条件判断.SHELL也不例外. if list then do something here ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第三章:变换
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第三章:变换 学习目标 理解如何用矩阵表示线性变换和仿射变换: 学习在 ...
- 【转】shell学习笔记(一)——学习目的性、特殊字符、运算符等
1 学习shell的目的性 写之前我们先来搞清楚为什么要学shell,学习要有目的性 shell简单.灵活.高效,特别适合处理一些系统管理方面的小问题 shell可以实现自动化管理,让系统管理员的工作 ...
随机推荐
- Cookie && Session之验证实例
为了防止各种自动登录,以及反作弊和破坏,往往会要求登录时让用户输入随机产生的验证码(这组验证码是一组数字和字母),这样可以起到一定的防止他人利用程序让机器自动反复登录的情况.在PHP下要实现这种功能是 ...
- IOS中WebView的使用
UIWebView是iOS sdk中一个最常用的控件.是内置的浏览器控件,我们可以用它来浏览网页.打开文档等等,UIWebView能够加载html/htm.pdf.docx.txt等格式的文件 系统 ...
- 使用百度地图API实现轨迹回放
调用百度地图API实现路线的轨迹回放功能其实很简单,只要搞懂以下几点即可: 1.需要用Polyline方法先绘制好路线图 2.用Marker添加标注点 3.关键一步,通过结合定时器,使用Marker创 ...
- EasyUI-window包含一个iframe,在iframe中如何关闭window
我试过类似$('#win').window('close');报$.data...options无效的错误,我已经引入了js文件,路径没问题,而且在同一个页面,不用iframe是可以关闭的 在ifra ...
- 无需编码开发快速设计互动式UI - uilang
uilang是一个非常小巧的ui类库,可以帮助不熟悉前端代码的web设计人员快速的开发互动式UI.你只需要使用“语义式”的说明来控制元素的动态效果. 开发中你只需要在<code>标签内部输 ...
- struts2学习笔记(三)—— 在用户注冊程序中使用验证框架
实现目标: 1.使用验证框架对用户注冊信息进行验证 2.验证username.password.邮箱不能为空 3.验证username.password长度 ...
- linux性能监控命令
vmstat 可以用来监控虚拟内存.可对操作系统的虚拟内存.IO.CPU等多个指标的整体情况进行监视. Linux系统的内存分为物理内存和虚拟内存两种.物理内存是真实的,也就是物理内存条上的内存.而虚 ...
- 算法笔记_019:背包问题(Java)
目录 1 问题描述 2 解决方案 2.1 蛮力法 2.2 减治法 2.2.1 递归求解 2.2.2 非递归求解(运用异或运算) 2.3 动态规划法 1 问题描述 给定n个重量为w1,w2,w3,... ...
- 前端project与性能优化(长文)
原文链接:http://fex.baidu.com/blog/2014/03/fis-optimize/ 每一个參与过开发企业级 web 应用的前端project师也许都曾思考过前端性能优化方面的问题 ...
- js 动态创建变量
js 动态创建变量 CreationTime--2018年7月2日15点04分 Author:Marydon 1.实现方式 通过eval()实现 2.代码实现 /** * 声明一个函数 * @ex ...