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可以实现自动化管理,让系统管理员的工作 ...
随机推荐
- CSDN日报20170328——《你看那个人他像一条狗》
[程序人生]你看那个人他像一条狗 作者:清纯的微笑 今年三十了,到了传说中程序猿最应该迷茫的年龄了,那么我迷茫吗,没的说,依照华为34岁就要劝退的要求,我还有4年的程序生涯. [微信小程序]重磅!个人 ...
- vmwareubuntu18.04网络配置
用vm安装ubuntu的时候要如果使用的是net模式,要确保vm的net服务和dhcp服务开启了,右键我的电脑-管理-服务和应用程序-服务找到对应的vm net服务和dhcp服务启动.
- Python 爬虫(2)多线程
前面说过由于GIL的存在,Python的多线程效率没有希望的那么高,python的多线程适合IO密集型的情况,而爬虫恰好就是一个IO密集的情况,因为爬虫中很大一部分时间,是在等待socket返回数据. ...
- 折叠伸缩工具栏 CollapsingToolbarLayout
PS:这是一个超级超级垃圾的控件,强烈建议放弃使用! demo地址:https://github.com/baiqiantao/CollapsingDemo.git 一个类似的效果的库,有800个星 ...
- Android两次后退键退出
转载请注明出处:http://blog.csdn.net/javacattle/article/details/41964045 仅仅要在 *.Java 文件里加入就可以 private int ba ...
- React 同构思想
作者:yangchunwen React比较吸引我的地方在于其客户端-服务端同构特性,服务端-客户端可复用组件,本文来简单介绍下这一架构思想. 出于篇幅原因,本文不会介绍React基础,所以,如果你还 ...
- Python 高级图像处理
构建图像搜索引擎并不是一件容易的任务.这里有几个概念.工具.想法和技术需要实现.主要的图像处理概念之一是逆图像查询(RIQ).Google.Cloudera.Sumo Logic 和 Birst 等公 ...
- @Autowired注入了dao,为什么还要写getDao(){return userDao}这个方法?有什么作用?
Autowired private UserDao userDao; @Override public BaseDao<User> getDao() { return userDao; } ...
- C# int与string转化
1.int-->string ; string s1 = a.ToString(); string s2 = Convert.ToString(a); 2.string -->int &q ...
- js看起来比较怪异的写法 (综合)
1.$(function() {}中$是什么意思? <script type="text/javascript"> $(function(){ $("#tre ...