一、if
首先看if是一个shell关键字:
[root@node1 shell]# type if
if is a shell keyword
[root@node1 shell]# help if
if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
Execute commands based on conditional.
The `if COMMANDS' list is executed. If its exit status is zero, then the
`then COMMANDS' list is executed. Otherwise, each `elif COMMANDS' list is
executed in turn, and if its exit status is zero, the corresponding
`then COMMANDS' list is executed and the if command completes. Otherwise,
the `else COMMANDS' list is executed, if present. The exit status of the
entire construct is the exit status of the last command executed, or zero
if no condition tested true.
Exit Status:
Returns the status of the last command executed.
通过help命令,我们已经清楚了它的语法:if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]… [ else COMMANDS; ] fi,下面就以例子来说明一下:
[root@node1 shell]# if test 3 -gt 2;then echo "ok";fi
ok
[root@node1 shell]# if test 3 -gt 5;then echo "ok";else echo "error"; fi
error
[root@node1 shell]# if test 3 -eq 4;then echo ok;elif test 3 -eq 3;then echo 3;fi
3
二、while
[root@node1 shell]# type while
while is a shell keyword
[root@node1 shell]# help while
while: while COMMANDS; do COMMANDS; done
Execute commands as long as a test succeeds.
Expand and execute COMMANDS as long as the final command in the
`while' COMMANDS has an exit status of zero.
Exit Status:
Returns the status of the last command executed.
通过help命令,我们可以了解到while的语法:while COMMANDS; do COMMANDS; done
[root@node1 shell]# while ls /share;do echo ok;rm -rf /share;done
ls: 无法访问/share: 没有那个文件或目录
[root@node1 shell]#
输出1~5:
[root@node1 shell]# while test $a -le 5;do echo $a;((a++));done
1
2
3
4
5
三、for
[root@node1 shell]# type for
for is a shell keyword
[root@node1 shell]# help for
for: for NAME [in WORDS ... ] ; do COMMANDS; done
Execute commands for each member in a list.
The `for' loop executes a sequence of commands for each member in a
list of items. If `in WORDS ...;' is not present, then `in "$@"' is
assumed. For each element in WORDS, NAME is set to that element, and
the COMMANDS are executed.
Exit Status:
Returns the status of the last command executed.
for ((: for (( exp1; exp2; exp3 )); do COMMANDS; done
Arithmetic for loop.
Equivalent to
(( EXP1 ))
while (( EXP2 )); do
COMMANDS
(( EXP3 ))
done
EXP1, EXP2, and EXP3 are arithmetic expressions. If any expression is
omitted, it behaves as if it evaluates to 1.
Exit Status:
Returns the status of the last command executed.
通过help,我们了解到for的语法为:1.增强for循环:for NAME [in WORDS … ] ; do COMMANDS; done 2.普通for循环:for ((: for (( exp1; exp2; exp3 )); do COMMANDS; done
输出1~5:
[root@node1 shell]# for((a=1;a<=5;a++));do echo $a;done
1
2
3
4
5
[root@node1 shell]# for i in 1 2 3 4 5;do echo $i;((i++));done
1
2
3
4
5
[root@node1 shell]# for i in `seq 5`;do echo $i;((i++));done
1
2
3
4
5
练习题:
- 用户给定路径
- 输出文件大小最大的文件
- 递归子目录
思路指引:du -a /tmp/ |sort -nr(忘记sort命令的可以点此查看!)
#! /bin/bash
oldIFS=$IFS
IFS=$'\n'
for i in `du -a $1 | sort -nr`;do
echo $i
done
IFS=$oldIFS
接下来我们要判断file并输出:
#! /bin/bash
oldIFS=$IFS
IFS=$'\n'
for i in `du -a $1 | sort -nr`;do
filename=`echo $i | awk '{print $2}'`
if [ -f $filename ];then
echo $filename
break
fi
done
IFS=$oldIFS