zoukankan      html  css  js  c++  java
  • Shell编程 之 if语句2

    4. 多分支 if 语句

      4.1 语句结构

        

      4.2 简易计算器

    #!/bin/bash
    
    read -t 30 -p "input number1: " num1
    read -t 30 -p "input number2: " num2
    read -t 30 -p "input an operator: " ope
    
    if [ -n "$num1" -a -n "$num2" -a -n "$ope" ]
            then
                    test1=$(echo $num1 | sed 's/[0-9]//g')
                    test2=$(echo $num2 | sed 's/[0-9]//g')
    
            if [ -z "$test1" -a -z "$test2" ]
                    then
                            if [ "$ope" == '+' ]
                                    then
                                            res=$(( $num1 + $num2))
                            elif [ "$ope" == '-' ]
                                    then
                                            res=$(( $num1 - $num2))
                            elif [ "$ope" == '*' ]
                                    then
                                            res=$(( $num1 * $num2))
                            elif [ "$ope" == '/' ]
                                    then
                                            res=$(( $num1 / $num2))
                            else
                                    echo "invalid operator"
                                    exit 10
                            fi
            else
                    echo "invalid number"
                    exit 11
            fi
    else
            echo "no number inputed"
            exit 12
    fi
    
    echo "$num1 $ope $num2 = $res"
    

      4.5 判断用户输入的是什么文件

        tips:运行中输错内容,按 ctrl + backspace 进行删除

    #!/bin/bash
    
    read -t 30 -p "input a filename: " file
    
    if [ -z "$file" ]
            then
                    echo "no inputs"
                    exit 11
    elif [ ! -e "$file" ]
            then
                    echo "$file is not a file"
                    exit 22
    elif [ -f "$file" ]
            then
                    echo "$file is a regular file"
    elif [ -d "$file" ]
            then
                    echo "$file is a directory"
    else
            echo "$file is a specific file"
    fi
    ~                                                                                     
    ~                                                                                     
    ~                                                                                                                                                                         
    "if6.sh" 21L, 331C
    

      

  • 相关阅读:
    VS自带的诊断工具
    Electron学习
    PC跨*台
    .NET调试学习
    Mac使用
    SSL/TLS
    UKey学习
    授权机制OAuth、JWT
    代理服务器
    .NET相关源码查找
  • 原文地址:https://www.cnblogs.com/wnzhong/p/6390960.html
Copyright © 2011-2022 走看看