shell流控制与循环
来源:互联网 发布:流星网络电视 破解 编辑:程序博客网 时间:2024/06/05 10:56
shell流控制与循环:
1. if-else流控制:
if list1; then
list2
elif list3; then
list4
elif list5; then
list6
else
list7
fi
shell中返回0表示为真,非0为假
例子:
#!/bin/bash
read -p "请输入一个整数A: " A
if test ${A} -lt 1 ; then
echo "A<1"
elif test ${A} -lt 10 ; then
echo "A<10"
elif test ${A} -lt 100 ; then
echo "A<100"
else
echo "A>=100"
fi
等价于:
#!/bin/bash
read -p "请输入一个整数A: " A
if [ ${A} -lt 1 ] ; then
echo "A<1"
elif [ ${A} -lt 10 ]; then
echo "A<10"
elif [ ${A} -lt 100 ]; then
echo "A<100"
else
echo "A>=100"
fi
test expr 等价于 [ expr ]
注意:在[之后和]之前都要有空格,通常[]作为test的缩写
test能够理解三种主要类型的表达式:
文件测试,字符串比较,数字比较
复合表达式:
test expr1 op1 expr2 op1为-a或-o
等价于[ expr1 ] op2 [ expr2 ] op2为&&,||
具体ref:私房菜P380
注意:test测试时的变量,最好用双引号括起来
case语句:
case word in
pattern1)
list1;;
pattern2)
list2;;
...
*)
listn;;
esac
while循环:
while cmd
do
list
done
如:
#!/bin/bash
x=1
while [ ${x} -lt 10 ]
do
echo ${x}
x=`expr $x + 1`
done
输入重定向和while
#!/bin/bash
while read line
do
case ${line} in
*root*) echo ${line};;
esac
done < /etc/passwd
输出:root:x:0:0:root:/root:/bin/bash
---------------------------------------------------------------
#!/bin/bash
#计算文件行数
if [ -f "${1}" ]; then
i=0
while read line
do
i=`expr ${i} + 1`
done < "${1}"
echo $i
fi
-----------------------------------------------------------
#!/bin/bash
if [ $# -ge 1 ]; then
for FILE in ${@}
do
exec 4<&0 0<"${FILE}" #重定向STDIN指针
while read line
do
echo ${line}
done
exec 0<&4 4<&- #恢复STDIN,并关闭描述符4
done
fi
--------------------------------------------------------------
until循环:
#!/bin/bash
x=1
until [ ${x} -ge 10 ]
do
echo ${x}
x=$(($x+1))
done
for循环:
#!/bin/bash
for i in 0 1 2 3 4 5
do
echo ${i}
done
------------------------------------
#!/bin/bash
for FILE in $HOME/.bash*
do
echo ${FILE}
done
--------------------------------------
select循环:
#test.sh:
#!/bin/bash
select DRINK in tea cofee water juice appe all none
do
case $DRINK in
tea|cofee|water|all)
echo "Go to canteen"
;;
juice|appe)
echo "Available at home"
;;
none)
break
;;
*) echo "ERROR: Invalid selection"
;;
esac
done
执行结果:
#bash test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
#? 1
Go to canteen
#? 2
Go to canteen
#? 3
Go to canteen
#? 4
Available at home
#? 5
Available at home
#? 6
Go to canteen
#? 7
您可以更改显示的提示选择循环通过改变变量PS3如下:
$PS3="Please make a selection => " ; export PS3
$./test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
Please make a selection => juice
Available at home
Please make a selection => none
$
----------------------------------------------
无限循环:
#!/bin/bash
x=0
while :
do
echo ${x}
x=`expr ${x} + 1`
done
1. if-else流控制:
if list1; then
list2
elif list3; then
list4
elif list5; then
list6
else
list7
fi
shell中返回0表示为真,非0为假
例子:
#!/bin/bash
read -p "请输入一个整数A: " A
if test ${A} -lt 1 ; then
echo "A<1"
elif test ${A} -lt 10 ; then
echo "A<10"
elif test ${A} -lt 100 ; then
echo "A<100"
else
echo "A>=100"
fi
等价于:
#!/bin/bash
read -p "请输入一个整数A: " A
if [ ${A} -lt 1 ] ; then
echo "A<1"
elif [ ${A} -lt 10 ]; then
echo "A<10"
elif [ ${A} -lt 100 ]; then
echo "A<100"
else
echo "A>=100"
fi
test expr 等价于 [ expr ]
注意:在[之后和]之前都要有空格,通常[]作为test的缩写
test能够理解三种主要类型的表达式:
文件测试,字符串比较,数字比较
复合表达式:
test expr1 op1 expr2 op1为-a或-o
等价于[ expr1 ] op2 [ expr2 ] op2为&&,||
具体ref:私房菜P380
注意:test测试时的变量,最好用双引号括起来
case语句:
case word in
pattern1)
list1;;
pattern2)
list2;;
...
*)
listn;;
esac
while循环:
while cmd
do
list
done
如:
#!/bin/bash
x=1
while [ ${x} -lt 10 ]
do
echo ${x}
x=`expr $x + 1`
done
输入重定向和while
#!/bin/bash
while read line
do
case ${line} in
*root*) echo ${line};;
esac
done < /etc/passwd
输出:root:x:0:0:root:/root:/bin/bash
---------------------------------------------------------------
#!/bin/bash
#计算文件行数
if [ -f "${1}" ]; then
i=0
while read line
do
i=`expr ${i} + 1`
done < "${1}"
echo $i
fi
-----------------------------------------------------------
#!/bin/bash
if [ $# -ge 1 ]; then
for FILE in ${@}
do
exec 4<&0 0<"${FILE}" #重定向STDIN指针
while read line
do
echo ${line}
done
exec 0<&4 4<&- #恢复STDIN,并关闭描述符4
done
fi
--------------------------------------------------------------
until循环:
#!/bin/bash
x=1
until [ ${x} -ge 10 ]
do
echo ${x}
x=$(($x+1))
done
for循环:
#!/bin/bash
for i in 0 1 2 3 4 5
do
echo ${i}
done
------------------------------------
#!/bin/bash
for FILE in $HOME/.bash*
do
echo ${FILE}
done
--------------------------------------
select循环:
#test.sh:
#!/bin/bash
select DRINK in tea cofee water juice appe all none
do
case $DRINK in
tea|cofee|water|all)
echo "Go to canteen"
;;
juice|appe)
echo "Available at home"
;;
none)
break
;;
*) echo "ERROR: Invalid selection"
;;
esac
done
执行结果:
#bash test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
#? 1
Go to canteen
#? 2
Go to canteen
#? 3
Go to canteen
#? 4
Available at home
#? 5
Available at home
#? 6
Go to canteen
#? 7
您可以更改显示的提示选择循环通过改变变量PS3如下:
$PS3="Please make a selection => " ; export PS3
$./test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
Please make a selection => juice
Available at home
Please make a selection => none
$
----------------------------------------------
无限循环:
#!/bin/bash
x=0
while :
do
echo ${x}
x=`expr ${x} + 1`
done
0 0
- shell流控制与循环
- shell编程-流程控制-while循环与until循环
- shell 条件控制,循环
- Shell控制结构--循环控制
- shell判断、循环、控制语句与函数应用
- Shell 循环控制break/continue
- shell计算器while循环控制
- (7) Shell 流程控制-循环
- 【shell】结构控制-循环语句
- shell流程控制--循环语句
- 第3章 shell结构化命令------------(文件数据的循环与控制循环)
- 嵌套循环与循环控制
- shell循环2.while、until、select循环和循环控制
- shell基本知识 - 控制流与函数
- 【Linux Shell】shell命令读书笔记---第六章:使用循环进行流控制
- UNIX Shell循环控制—while
- Shell脚本常识--(循环控制)
- shell编程-流程控制-for循环
- sql语句查出重复数据 并删除
- ubuntu12.04指令
- Linux下批量杀掉 包含某个关键字的 程序进程
- Java/Android DES加密
- 解决JQuery中$符冲突
- shell流控制与循环
- Axis2
- Python的range和xrange
- Performance Optimization of MTP
- 解决安装完MFS找不到mfsmount的问题
- myeclipse在工作中的使用
- 仿饿了么订餐系统源码免费试用
- 破除迷信之:100分的态度!
- CSS实现垂直居中