Linux 流程控制语句-if-for-while

[god@centos7-1 tmp]$ help if
if: if 命令; then 命令; [ elif 命令; then 命令; ]... [ else 命令; ] fi
    根据条件执行命令。
    
    `if COMMANDS'列表被执行。如果退出状态为零,则执行`then COMMANDS' 
    列表。否则按顺序执行每个 `elif COMMANDS'列表,并且如果它的退出状态为
    零,则执行对应的 `then COMMANDS' 列表并且 if 命令终止。否则如果存在的
    情况下,执行 `else COMMANDS'列表。整个结构的退出状态是最后一个执行
    的命令的状态,或者如果没有条件测试为真的话,为零。
    
    退出状态:
    返回最后一个执行的命令的状态。
[god@centos7-1 tmp]$ if ls -l / ; then echo "hello"; else echo "no ok"; fi
总用量 28
lrwxrwxrwx.   1 root root           7 9月  27 02:39 bin -> usr/bin
dr-xr-xr-x.   5 root root        4096 11月 14 15:58 boot
drwxr-xr-x   20 root root        3320 11月 28 20:24 dev
drwxr-xr-x. 144 root root        8192 12月  5 14:49 etc
drwxr-xr-x.   9 root root          98 12月  3 17:30 home
lrwxrwxrwx.   1 root root           7 9月  27 02:39 lib -> usr/lib
lrwxrwxrwx.   1 root root           9 9月  27 02:39 lib64 -> usr/lib64
drwxr-xr-x.   2 root root           6 4月  11 2018 media
drwxr-xr-x.   2 root root           6 4月  11 2018 mnt
drwxr-xr-x.   4 root root          30 11月 28 18:45 opt
dr-xr-xr-x  212 root root           0 11月 27 11:43 proc
dr-xr-x---.   7 root root        4096 12月  3 17:36 root
drwxr-xr-x   41 root root        1240 12月  5 06:48 run
lrwxrwxrwx.   1 root root           8 9月  27 02:39 sbin -> usr/sbin
drwxr-xr-x.   2 root root           6 4月  11 2018 srv
dr-xr-xr-x   13 root root           0 11月 27 18:57 sys
drwxrwxrwt.  18 root root        4096 12月  5 14:49 tmp
drwxr-xr-x.  14 root root         167 11月 28 19:35 usr
drwxr-xr-x.  22 root root        4096 11月 28 22:19 var
drwx------    2 root sharevickie   24 11月 27 19:22 vickieshare
drwxr-xr-x    3 root root          15 11月 16 12:09 vik
hello
[god@centos7-1 tmp]$ if ls -l /god ; then echo "hello"; else echo "no ok"; fi
ls: 无法访问/god: 没有那个文件或目录
no ok
[god@centos7-1 tmp]$ if ls -l /god &> /dev/null ; then echo "hello"; else echo "no ok"; fi
no ok
[god@centos7-1 tmp]$ help for
for: for 名称 [in 词语 ... ] ; do 命令; done
    为列表中的每个成员执行命令。
    
    `for' 循环为列表中的每个成员执行一系列的命令。如果没有
    `in WORDS ...;'则假定使用 `in "$@"'。对于 WORDS 中的每
     个元素,NAME 被设定为该元素,并且执行 COMMANDS 命令。
    
    退出状态:
    返回最后执行的命令的状态。
for ((: for (( 表达式1; 表达式2; 表达式3 )); do 命令; done
    算术 for 循环。
    
    等价于
        (( EXP1 ))
        while (( EXP2 )); do
            COMMANDS
            (( EXP3 ))
        done
    EXP1、EXP2 和 EXP3都是算术表达式。如果省略任何表达式,
    则等同于使用了估值为1的表达式。
    
    退出状态:
    返回最后执行的命令的状态。
[god@centos7-1 tmp]$ for((i=0; i<10; i++));do echo $i; done
0
1
2
3
4
5
6
7
8
9
[god@centos7-1 tmp]$ help for
for: for 名称 [in 词语 ... ] ; do 命令; done
    为列表中的每个成员执行命令。
    
    `for' 循环为列表中的每个成员执行一系列的命令。如果没有
    `in WORDS ...;'则假定使用 `in "$@"'。对于 WORDS 中的每
     个元素,NAME 被设定为该元素,并且执行 COMMANDS 命令。
    
    退出状态:
    返回最后执行的命令的状态。
for ((: for (( 表达式1; 表达式2; 表达式3 )); do 命令; done
    算术 for 循环。
    
    等价于
        (( EXP1 ))
        while (( EXP2 )); do
            COMMANDS
            (( EXP3 ))
        done
    EXP1、EXP2 和 EXP3都是算术表达式。如果省略任何表达式,
    则等同于使用了估值为1的表达式。
    
    退出状态:
    返回最后执行的命令的状态。
[god@centos7-1 tmp]$ for i in alksdjflaksd flkasdf laksdf askdjf;do echo $i; done
alksdjflaksd
flkasdf
laksdf
askdjf
[god@centos7-1 tmp]$ seq 8
1
2
3
4
5
6
7
8
[god@centos7-1 tmp]$ for i in seq 10; do echo $i; done
seq
10
[god@centos7-1 tmp]$ for i in `seq 10`; do echo $i; done
1
2
3
4
5
6
7
8
9
10
[god@centos7-1 tmp]$ help while
while: while 命令; do 命令; done
    只要测试成功即执行命令。
    
    只要在 `while' COMMANDS 中的最终命令返回结果为0,则
    展开并执行 COMMANDS 命令。
    
    退出状态:
    返回最后一个执行的命令的状态。
[god@centos7-1 tmp]$ cd
[god@centos7-1 ~]$ mkdir /god
mkdir: 无法创建目录"/god": 权限不够
[god@centos7-1 ~]$ su root
密码:
[root@centos7-1 god]# mkdir /god
[root@centos7-1 god]# ll /god
总用量 0
[root@centos7-1 god]# cd
[root@centos7-1 ~]# mkdir /god
mkdir: 无法创建目录"/god": 文件已存在
[root@centos7-1 ~]# ll /
总用量 28
lrwxrwxrwx.   1 root root           7 9月  27 02:39 bin -> usr/bin
dr-xr-xr-x.   5 root root        4096 11月 14 15:58 boot
drwxr-xr-x   20 root root        3320 11月 28 20:24 dev
drwxr-xr-x. 144 root root        8192 12月  5 14:49 etc
drwxr-xr-x    2 root root           6 12月  5 15:06 god
drwxr-xr-x.   9 root root          98 12月  3 17:30 home
lrwxrwxrwx.   1 root root           7 9月  27 02:39 lib -> usr/lib
lrwxrwxrwx.   1 root root           9 9月  27 02:39 lib64 -> usr/lib64
drwxr-xr-x.   2 root root           6 4月  11 2018 media
drwxr-xr-x.   2 root root           6 4月  11 2018 mnt
drwxr-xr-x.   4 root root          30 11月 28 18:45 opt
dr-xr-xr-x  214 root root           0 11月 27 11:43 proc
dr-xr-x---.   7 root root        4096 12月  3 17:36 root
drwxr-xr-x   41 root root        1240 12月  5 06:48 run
lrwxrwxrwx.   1 root root           8 9月  27 02:39 sbin -> usr/sbin
drwxr-xr-x.   2 root root           6 4月  11 2018 srv
dr-xr-xr-x   13 root root           0 11月 27 18:57 sys
drwxrwxrwt.  18 root root        4096 12月  5 15:06 tmp
drwxr-xr-x.  14 root root         167 11月 28 19:35 usr
drwxr-xr-x.  22 root root        4096 11月 28 22:19 var
drwx------    2 root sharevickie   24 11月 27 19:22 vickieshare
drwxr-xr-x    3 root root          15 11月 16 12:09 vik
[root@centos7-1 ~]# while ls -l /god ;do echo "ok"; rm -rf /god; done
总用量 0
ok
ls: 无法访问/god: 没有那个文件或目录
[root@centos7-1 ~]# ll
总用量 286452
-rw-r--r--  1 root  root          0 11月 22 13:54 a
-rw-r--r--  1 root  root          0 11月 22 13:54 ab
-rw-r--r--  1 root  root          0 11月 22 13:54 abc
-rwxr-xr-x  1 root  root        251 12月  3 17:36 addlinuxuser.sh
-rw-r--r--  1 root  root        135 11月 27 17:34 awk.txt
-rw-r--r--  1 root  root        111 11月 22 15:50 grep.txt
-rw-r--r--  1 root  root        511 11月 24 00:11 inittab
-rw-r--r--  1 root  root  138090286 11月 28 18:02 jdk-7u80-linux-x64.rpm
-rw-r--r--  1 root  root  153530841 11月 28 18:02 jdk-7u80-linux-x64.tar.gz
-rw-r--r--  1 root  root       2347 11月 27 16:49 passwd
-rwxr-xr-x  1 root  root       1833 11月 22 12:28 profile
-rw-r--r--  1 root  root         53 12月  3 09:23 sh01.sh
-rw-r--r--  1 root  root         26 12月  3 14:17 sh02.sh
-rw-r--r--  1 root  root        106 12月  3 14:21 sh03.sh
drwxr-xr-x  2 root  root        170 12月  2 22:40 shell
-rw-r--r--  1 root  root         38 11月 24 00:09 sort.txt
drwxr-xr-x 12 50469 users      4096 11月 28 18:38 tengine-2.1.0
-rw-r--r--  1 root  root    1653240 11月 28 18:02 tengine-2.1.0.tar.gz
[root@centos7-1 ~]# du -a 
[root@centos7-1 ~]# du -a | sort -n
[root@centos7-1 ~]# du -a | sort -nr

[root@centos7-1 ~]# vi findMaxFile.sh
[root@centos7-1 ~]# cat findMaxFile.sh 
#!/bin/bash
for i in `du -a $1| sort -rn`; do
echo $i;
done
[root@centos7-1 ~]# chmod +x findMaxFile.sh 
[root@centos7-1 ~]# ./findMaxFile.sh ./
[root@centos7-1 ~]# vi findMaxFile.sh
[root@centos7-1 ~]# cat findMaxFile.sh 
#!/bin/bash
oldifs=$IFS
IFS=$'\n'
for i in `du -a $1| sort -rn`; do
echo $i;
done
IFS=$oldIFS
[root@centos7-1 ~]# help test
test: test [表达式]
    Evaluate conditional expression.
    
    Exits with a status of 0 (true) or 1 (false) depending on
    the evaluation of EXPR.  Expressions may be unary or binary.  Unary
    expressions are often used to examine the status of a file.  There
    are string operators and numeric comparison operators as well.
    
    The behavior of test depends on the number of arguments.  Read the
    bash manual page for the complete specification.
    
    File operators:
    
      -a FILE        True if file exists.
      -b FILE        True if file is block special.
      -c FILE        True if file is character special.
      -d FILE        True if file is a directory.
      -e FILE        True if file exists.
      -f FILE        True if file exists and is a regular file.
      -g FILE        True if file is set-group-id.
      -h FILE        True if file is a symbolic link.
      -L FILE        True if file is a symbolic link.
      -k FILE        True if file has its `sticky' bit set.
      -p FILE        True if file is a named pipe.
      -r FILE        True if file is readable by you.
      -s FILE        True if file exists and is not empty.
      -S FILE        True if file is a socket.
      -t FD          True if FD is opened on a terminal.
      -u FILE        True if the file is set-user-id.
      -w FILE        True if the file is writable by you.
      -x FILE        True if the file is executable by you.
      -O FILE        True if the file is effectively owned by you.
      -G FILE        True if the file is effectively owned by your group.
      -N FILE        True if the file has been modified since it was last read.
    
      FILE1 -nt FILE2  True if file1 is newer than file2 (according to
                       modification date).
    
      FILE1 -ot FILE2  True if file1 is older than file2.
    
      FILE1 -ef FILE2  True if file1 is a hard link to file2.
    
    String operators:
    
      -z STRING      True if string is empty.
    
      -n STRING
         STRING      True if string is not empty.
    
      STRING1 = STRING2
                     True if the strings are equal.
      STRING1 != STRING2
                     True if the strings are not equal.
      STRING1 < STRING2
                     True if STRING1 sorts before STRING2 lexicographically.
      STRING1 > STRING2
                     True if STRING1 sorts after STRING2 lexicographically.
    
    Other operators:
    
      -o OPTION      True if the shell option OPTION is enabled.
      -v VAR     True if the shell variable VAR is set
      ! EXPR         True if expr is false.
      EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
      EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.
    
      arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne,
                     -lt, -le, -gt, or -ge.
    
    Arithmetic binary operators return true if ARG1 is equal, not-equal,
    less-than, less-than-or-equal, greater-than, or greater-than-or-equal
    than ARG2.
    
    Exit Status:
    Returns success if EXPR evaluates to true; fails if EXPR evaluates to
    false or an invalid argument is given.
[root@centos7-1 ~]# vi findMaxFile.sh
[root@centos7-1 ~]# cat findMaxFile.sh 
#!/bin/bash
oldifs=$IFS
IFS=$'\n'
for i in `du -a $1| sort -rn`; do
    echo $i;
    fileName=`echo $i | awk '{print $2 }'`
    if[ -f $fileName]; then
        echo $fileName;
        exit 0
    fi
done
IFS=$oldIFS
[root@centos7-1 ~]# cat findMaxFile.sh 
#!/bin/bash
oldifs=$IFS
IFS=$'\n'
for i in `du -a $1| sort -rn`; do
    echo $i
    fileName=`echo $i | awk '{print $2 }'`
    if [ -f $fileName ] ; then
        echo $fileName;
        exit 0
    fi
done
IFS=$oldIFS
[root@centos7-1 ~]# ./findMaxFile.sh ./
318796    ./
149936    ./jdk-7u80-linux-x64.tar.gz
./jdk-7u80-linux-x64.tar.gz

[root@centos7-1 ~]# . findMaxFile.sh ./
318796    ./
149936    ./jdk-7u80-linux-x64.tar.gz
./jdk-7u80-linux-x64.tar.gz

Connection closed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值