linux shell有一套自己的流程控制语句,其中包括条件语句(if),循环语句(for,while,until),选择语句(case/select)。下面我将通过例子介绍下,各个语句使用方法。

1:在shell 中$() 与 ``等效。执行中间包含的命令语句,返回执行结果。
2:从效率来说let==(()) > expr > bc。let和(())运行是内建命令,使用相同的算法。
3:let 和 expr 的运算是整数运算,不包括浮点预算。
4:expr和bc是外部程序,expr的体积几乎等于bc的1/3,执行一次装入内存所消耗的时间就不一样。
5:从运算能力来说,bc排第一位。

shell的循环主要有3种,for,while,until
shell的分支判断主要有2种,if,case

linux shell “(())” 双括号运算符使用

shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select

####循环控制语句 ,break continue exit return
break 命令不执行当前循环体内break下面的语句从当前循环退出.
continue 命令是程序在本循体内忽略下面的语句,从循环头开始执行
break
结束并退出循环
continue
在循环中不执行continue下面的代码,转而进入下一轮循环
exit
退出脚本,
常带一个整数给系统,如 exit 0
return
在函数中将数据返回
或返回一个结果给调用函数的脚本

if条件

#if语句的后面是Shell命令,如果该命令执行成功返回0,则执行then后面的命令。
if command
then
command
command
fi
#用test命令测试其后面expression的结果,如果为真,则执行then后面的命令。
if test expression
then
command
fi
#下面的格式和test expression等同
if [ string/numeric expression ]
then
command
fi
#下面的两种格式也可以用于判断语句的条件表达式,而且它们也是目前比较常用的两种。
if [[ string expression ]] #这种方式支持通配符,上面的那种不支持
then
command
fi if (( numeric expression )) #let表达式
then
command
fi

for循环  http://blog.csdn.net/qiudakun/article/details/7063559

将所有的for写入一个脚本中

for i in f1 f2 f3
for i in {1..5}
for i in $a
for i in $(ls $a)
for i in $(seq 100)
for i in `ls *.sh`
for i in `seq 100`
for ((i=0;i<5;i++))
for i in ${arr[@]}
for i in $*
for f in /proc/sys/net/ipv4/conf/*/arp_accept
默认分隔符是空格,而不管是几个空格。多个空格也当一个来处理。
第一种情况,可以直接写入字符串
[root@-shiyan sh]# cat > for
for f in
do
echo $f
done
[root@-shiyan sh]# bash for 一定要写成两个点号,一个或多于二个都不行
[root@-shiyan sh]# cat > for
for i in {..}
do
echo $i
done
[root@-shiyan sh]# bash for ###第二种情况,可以写变量名
[root@-shiyan sh]# cat > for
IFS=':'
a="root:x:0:0:root:/root:/bin/bash"
for f in $a
do
echo $f
done
[root@-shiyan sh]# bash for
root
x root
/root
/bin/bash ###第三种情况,可以写命令
[root@-shiyan sh]# cat > for
a="/root/sh"
for f in $(ls $a)
do
echo "file-i:$f"
done
[root@-shiyan sh]# bash for
file-i:aa
file-i:ab
file-i:ac
file-i:awk
file-i:ccc
file-i:cfont
file-i:check-root.sh
[root@-shiyan sh]# cat > for
for f in `ls *.sh`
do
name=`echo "$f"|awk -F. '{print $1}'`
echo $name
done
[root@-shiyan sh]# bash for
check-root
eth
for
ser
###查找循环(ls数据量太大的时候也可以用这种方法)
[root@250-shiyan sh]# cat > for
for f in `find . -type f -name "*.sh"`
do
name=`echo "$f"|awk -F/ '{print $2}'`
echo $name
done
[root@250-shiyan sh]# bash for
eth.sh
ser.sh
check-root.sh
for.sh
[root@250-shiyan sh]# cat > for
for f in `seq 100`  或者for f in $(seq 100)
do
if((f%4==0))
then
echo $f
continue
fi
done
[root@250-shiyan sh]# bash for
4
8
12
16
20
24

###第四种情况,for((赋值;条件;运算语句)),c语法的for循环形式
[root@-shiyan sh]# cat > for
for((i=;i<=;i++))
do
echo $(expr $i \* )
done
[root@-shiyan sh]# bash for
[root@250-shiyan sh]# cat > for
for((i=1;i<100;i++))
do
if((i%3==0))
then
echo $i
continue
fi
done
[root@250-shiyan sh]# bash for
3
6
9
12
15
18
[root@250-shiyan sh]# cat > for
arr=("a" "b" "c")
for f in ${arr[@]}
do
echo $f
done
[root@250-shiyan sh]# bash for
a
b
c
[root@250-shiyan sh]# cat > for
for f in $*
do
echo $f
done
[root@250-shiyan sh]# chmod u+x for
[root@250-shiyan sh]# ./for a b c d e f
a
b
c
d
e
f
[root@250-shiyan sh]# cat > for
for f in /proc/sys/net/ipv4/conf/*/arp_accept
do
echo $f
done
[root@250-shiyan sh]#
[root@250-shiyan sh]# bash for
/proc/sys/net/ipv4/conf/all/arp_accept
/proc/sys/net/ipv4/conf/default/arp_accept
/proc/sys/net/ipv4/conf/eth0/arp_accept
/proc/sys/net/ipv4/conf/lo/arp_accept
###直到满足条件,就退出。否则执行echo
[root@-shiyan sh]# cat > until
f=
until [ $f -lt ]
do
echo $f
((f--))
done
[root@-shiyan sh]# bash until 0
[root@84-monitor test]# bash loop
pass 1 in outer_loop
-------------------------
pass 1 in inner_loop
pass 2 in inner_loop
pass 3 in inner_loop
pass 4 in inner_loop
pass 5 in inner_loop pass 2 in outer_loop
-------------------------
pass 1 in inner_loop
pass 2 in inner_loop
pass 3 in inner_loop
pass 4 in inner_loop
pass 5 in inner_loop pass 3 in outer_loop
-------------------------
pass 1 in inner_loop
pass 2 in inner_loop
pass 3 in inner_loop
pass 4 in inner_loop
pass 5 in inner_loop pass 4 in outer_loop
-------------------------
pass 1 in inner_loop
pass 2 in inner_loop
pass 3 in inner_loop
pass 4 in inner_loop
pass 5 in inner_loop pass 5 in outer_loop
-------------------------
pass 1 in inner_loop
pass 2 in inner_loop
pass 3 in inner_loop
pass 4 in inner_loop
pass 5 in inner_loop [root@84-monitor test]# cat loop
outer=1
for a in 1 2 3 4 5
do
echo "pass $outer in outer_loop"
echo "-------------------------"
inner=1
for b in 1 2 3 4 5
do
echo "pass $inner in inner_loop"
let "inner+=1"
done
let "outer+=1"
echo
done
[root@84-monitor test]#
####第一种情况,还做了一个判断才死循环
[root@-shiyan frag]# cat while1.sh
while [ -gt ]
do
sleep
echo used
echo "`free |awk '/Mem/{print $3}'`"
done
[root@-shiyan frag]# bash while1.sh
used used used ####另外一种形式,什么也不判断就一直循环
while :
do
sleep
echo "`df -h`"
done ####第二种情况从标准输入或文件中读取什么,输出什么
while read line
do
echo $line
done
< /etc/passwd ####第三种情况加条件判断才循环
min=
max=
while [ $min -le $max ]
do
echo $min
min=`expr $min + `
done ####第四种形式,双括号形式,内部结构有点像C的语法,注意赋值:i=$(($i+))
i=
while(($i<))
do
if(($i%==))
then
echo $i
fi
i=$(($i+))
done 循环的结束
####计数器控制的while循环
主要用于已经准确知道要输入的数据和字符串的数目。 ####结束标记控制的while循环
主要用于不知道读入数据的个数,但是可以设置一个特殊的数据值来结束循环,该特殊值称为结束标记,通过提示用户输入进行操作。
[root@-shiyan frag]# cat aa.sh
#用脚本演示使用结束标记控制while循环实现猜1~10内的数
#!/bin/sh
echo "Please input the num (1~~10): "
read num
while [[ $num != ]]
do
if [ $num -lt ]
then
echo "Too small ,Try again.."
read num
elif [ $num -gt ]
then
echo "Too big ,Try again.. "
read num
else
exit
fi
done
echo "Yes ,you are right !!"
[root@-shiyan frag]# bash aa.sh
Please input the num (~~): Too small ,Try again.. Yes ,you are right !! ####标致控制的while循环
用户输入标志值来控制循环结束
[root@-shiyan frag]# cat a1.sh
#!/bin/sh
echo "Please input the num:"
read num
sum=
i=
signal=
while [[ $signal != ]]
do
if [ $i -eq $num ]
then
let "signal=1"
let "sum+=i"
echo "1+2、、、+$num=$sum"
else
let "sum=sum+i"
let "i++"
fi
done
[root@-shiyan frag]# bash a1.sh
Please input the num: +、、、+=
[root@-shiyan frag]# bash a1.sh
Please input the num: +、、、+= ####命令行控制的while循环
[root@-shiyan frag]# cat aa.sh
#!/bin/sh
echo "Please input arguements is $# "
echo "What you input : "
while [[ $* != "" ]]
do
echo $
shift
done
[root@-shiyan frag]# ./aa.sh
Please input arguements is
What you input :

case格式
[root@localhost script]# cat >yesno
#!/bin/bash
echo "enter [y/n]:"
read a
case $a in
  y|Y|yes|YES)
  echo "you enter $a"
  ;;
  n|N|no|NO)
  echo "you enter $a"
  ;;
  *)
  echo "error"
  ;;
esac 它能够把变量的内容与多个模板进行匹配,再根据成功匹配的模板去决定应该执行哪部分代码。case语句的匹配是从上往下地匹配顺序。因此,case语句编写的原则是从上往下,模板从特殊到普通。在C语言里,case语句中有default模板,而在shell程序设计中,可能将模板写成*,就可以完成相同的功能。
case语句适用于需要进行多重分支的应用情况。 select表达式是一种bash的扩展应用,动作包括:
()、自动用1,,,4列出菜单 (没有echo指令,自动显示菜单)
()、自动read输入选择 (没有 read指令,自动输入)
()、赋值给变量 (没有赋值指令,自动输入数字后,赋值字符串给变量)
select本身就是一个循环,break是当选择后,就跳出循环
虽然select本身就是循环,但不建议用他的循环 ,因为select虽然循环却不再显示菜单,只循环输入,所以select 语句干脆直接用break,只执行一次,在其上另配while循环,这样以后菜单也跟着循环。select是循环选择,一般与case语句使用。 [root@-shiyan sh]# cat sel1
#!/bin/bash
while echo "display current systeminfo"
do
select vi in "ifconfig" "date" "uptime" "quit"
do
case $vi in      case变量输入值与菜单项是一致的
"ifconfig") ifconfig;;
"date") date;;
"uptime") uptime;;
"quit") exit ;;
*) continue;;
esac
break
done
done

性能比较 awk的流程控制语句与shell的流程控制语句

[chengmo@localhost nginx]# time (awk 'BEGIN{ total=0;for(i=0;i<=10000;i++){total+=i;}print total;}')

real    0m0.003s
user 0m0.003s
sys 0m0.000s
[chengmo@localhost nginx]# time(total=;for i in $(seq );do total=$(($total+i));done;echo $total;) real 0m0.141s
user 0m0.125s
sys 0m0.008s 实现相同功能,可以看到awk实现的性能是shell的50倍!

shell流程控制语句的更多相关文章

  1. Linux Shell 流程控制语句

    * 本文主要介绍一些Linux Shell 常用的流程控制语句* 1. if 条件语句:if-then/if-elif-fi/if- else-fi if [条件判断逻辑1];then command ...

  2. 5.Shell 流程控制语句

    1.流程控制语句 通过if.for.while.case这4种流程控制语句来学习编写难度更大.功能更强的Shell脚本 4.3.1 if条件测试语句: if条件测试语句可以让脚本根据实际情况自动执行相 ...

  3. Linux | Shell流程控制语句

    流程控制语句 简单的Shell 脚本还不能满足我们日常工作的需要要,因为他不能批量的帮我们完成工作,所以Shell引入了 if.for.while.case 4种流程控制语句来帮助我们完成工作. if ...

  4. shell 流程控制语句

    case语句 case $变量名 in "值1")   如果变量的值等于值1,则执行程序1 ;;  "值2")   如果变量的值等于值2,则执行程序2 ;;   ...

  5. Shell流程控制语句for

    for语法格式: for 变量 in 参数列表 do 命令 done 或者 for 变量 in 参数列表 ; do 命令 done for语句流程控制图: 实例: [root@youxi1 ~]# v ...

  6. Shell流程控制语句while

    while语法格式: while 判断条件 do 命令 done while语句流程控制图: 实例: [root@youxi1 ~]# vim a.sh #!/bin/bash i=0 while [ ...

  7. Shell流程控制语句case

    case语法格式: case 变量或表达式 in 变量或表达式1) 命令1 ;; 变量或表达式2) 命令2 ;; ...... *) 默认命令 esac case语句流程控制图:  实例: [root ...

  8. Shell流程控制语句if

    (1).if语句 语法格式: if 判断条件 ; then 命令 fi 或 if 判断条件 then 命令 fi if语句流程图: 实例:判断命令是否执行成功,成功则输出语句This is ok. [ ...

  9. linux shell awk 流程控制语句(if,for,while,do)详细介绍

    在linux awk的 while.do-while和for语句中允许使用break,continue语句来控制流程走向,也允许使用exit这样的语句来退出.break中断当前正在执行的循环并跳到循环 ...

随机推荐

  1. vijos 1780 开车旅行

    细节巨多. 倍增即可. #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  2. mantis增加密码修改

    解决方式就是考虑修改Mantisbt PHP程序,增加一个密码修改框,这样管理员就可以直接修改用户密码了.     操作步骤如下:    1) 修改文件 manage_user_edit_page.p ...

  3. 10年山东省赛-E-最短路

    题目连接:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2155&cid=1430 题意:输入一个n个节点,m条边的图,然后k条记录,纪录 ...

  4. 第二个Sprint冲刺第七天

    讨论地点:宿舍 讨论成员:邵家文.李新.朱浩龙.陈俊金 讨论:整理已完成的功能

  5. POM的配置文件

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. dos快速通道

    要在文件夹的右键菜单中添加“命令提示符”选项.在注册表HKEY_CLASSES_ROOT\Directory\shell分支下新建一项“CommandPrompt”,修改右侧窗格中的“默认”键值为“命 ...

  7. ML2 – Address Population

    Why do we need it, whatever it is? VM unicast, multicast and broadcast traffic flow is detailed in m ...

  8. 使用BitTorrent-Sync实现双机文件双向同步

    BitTorrent-Sync是一款基于P2P的分布式文件同步工具,简称btsync,非开源软件但免费使用.本文使用btsync实现两台服务器上的软件双向同步. 安装 直接从官网下载相应的安装包,为了 ...

  9. Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.

    svn更新代码后,打开xcode工程文件,会出现  xxx..xcodeproj  cannot be opened because the project file cannot be parsed ...

  10. CentOS7上安装和使用Docker

    导读 Docker 是一个开源工具,它可以让创建和管理 Linux 容器变得简单,容器就像是轻量级的虚拟机,并且可以以毫秒级的速度来启动或停止.在本篇文章中我们将教你如何在 CentOS 7.x 中安 ...