Linux基础--shell脚本(4)

判断语句
    格式五
    if [ con1 ]
    then
        cmd1
    elif [ con2 ]
    then
        cmd2
    elif [ con3 ]
    then
        cmd3
    .....
    else
        cmd_failed
    fi


bc : 超文本计算器
    
    退出:quit
==================================
格式六:
    case $var in
        value1)
            cmd1
            ;;
        value2)
            cmd2
            ;;
        value3)
            cmd3
            ;;
        ....
        *)
            cmd_failed
            ;;
    esac        
    
=========================================
循环语句:
    for while until    

    for格式一
        for varname in value1 value2 ... valuen
        do
            cmd
        done
    
    for varname in $(seq stat_value step end_value)
    do
        cmd
    done


    `` : 反引号
    “” :双引号
    '' : 单引号

    案例1:
        [root@localhost test]# touch a b   无双引号或者单引号
        [root@localhost test]# ll
        总计 0
        -rw-r--r-- 1 root root 0 06-28 20:22 a
        -rw-r--r-- 1 root root 0 06-28 20:22 b
        [root@localhost test]# touch "aa bb"    双引号
        [root@localhost test]# ll
        总计 0
        -rw-r--r-- 1 root root 0 06-28 20:22 a
        -rw-r--r-- 1 root root 0 06-28 20:22 aa bb
        -rw-r--r-- 1 root root 0 06-28 20:22 b
        [root@localhost test]# touch 'aaa bbb'   单引号
        [root@localhost test]# ll
        总计 0
        -rw-r--r-- 1 root root 0 06-28 20:22 a
        -rw-r--r-- 1 root root 0 06-28 20:22 aaa bbb
        -rw-r--r-- 1 root root 0 06-28 20:22 aa bb
        -rw-r--r-- 1 root root 0 06-28 20:22 b

        表示"" 和 '' 都会屏蔽空格
    案例2:    
        [root@localhost test]# echo $USER
        root
        [root@localhost test]# mkdir $USER
        [root@localhost test]# ll
        总计 4
        drwxr-xr-x 2 root root 4096 06-28 20:26 root

        [root@localhost test]# mkdir "$USER"
        [root@localhost test]# ll
        总计 4
        drwxr-xr-x 2 root root 4096 06-28 20:27 root

        [root@localhost test]# mkdir '$USER'
        [root@localhost test]# ll
        总计 4
        drwxr-xr-x 2 root root 4096 06-28 20:27 $USER

        双引号不会屏蔽特殊字符,单引号可以屏蔽特殊字符

        
        date : 表示显示时间命令
            年        :%Y
            月        :%m
            日        :%d
            小时    :%H
            分钟    :%M
            秒        :%S
            星期    :%w
        案例:
            [root@localhost sh]# date +%Y
            2018
            [root@localhost sh]# date +%y
            18
    cal :
        显示日历
        案例
            cal         : 表示显示当前年当前月信息
            cal year : 表示显示当指定年的12个月信息
            cal month year : 表示显示当前年指定月的信息


        案例:
            [root@localhost test]# date
            2018年 06月 28日 星期四 20:31:10 CST

            [root@localhost test]# date            表示执行命令
            2018年 06月 28日 星期四 20:31:10 CST
            [root@localhost test]# mkdir date    创建目录
            [root@localhost test]# ll
            总计 4
            drwxr-xr-x 2 root root 4096 06-28 20:32 date  就是命令名
            [root@localhost test]# rm * -rf
            [root@localhost test]# mkdir `date`        创建目录
            [root@localhost test]# ll
            总计 24
            drwxr-xr-x 2 root root 4096 06-28 20:32 06月
            drwxr-xr-x 2 root root 4096 06-28 20:32 2018年
            drwxr-xr-x 2 root root 4096 06-28 20:32 20:32:52
            drwxr-xr-x 2 root root 4096 06-28 20:32 28日
            drwxr-xr-x 2 root root 4096 06-28 20:32 CST
            drwxr-xr-x 2 root root 4096 06-28 20:32 星期四

        优先执行反引号的命令
    
    for格式二

    for ((init; con; add))
    do
        cmd;
    done


    一个循环是由四部分组成:
        1 初始化部分
        2 条件部分
        3 增量部分
        4 循环体

        #!/bin/bash


        for ((i = 1 初始化; i < 10 条件; i++ 增量))
        do
            echo -n "$i " 循环
        done
        echo

        1 2 3 4 5 6 7 8 9

        1 -> 2 -> 4 -> 3 => 2 -> 4 -> 3 => .... 条件不满足,则退出

        练习:
            实现1 + 2 + 3 + ... + 100 = ?


    for格式三:嵌套
        
        for (())
        do
            for (())
            do
                ....
            done
            .....
        done

        *
        * *
        * * *
        * * *
        * * * * 
        * * * * *
        ......
        * * * * * * * * *

    homework:
        左上角
        *
        * *
        * * *
        * * * *
        * * * * *
        右上角
                *
              * *
            * * *
          * * * *
        * * * * *
        左下角
        * * * * *
        * * * *
        * * *
        * *
        *

        右下角
        * * * * *
          * * * *
            * * *
              * *
                *


    2 实现一个空三角形
      *
      * *
      *   *
      *     *
      *       *
      *         *
      * * * * * * *

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/84671870
今日推荐