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 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