shell 简单四则运算

shell 简单四则运算
#!/bin/bash

echo 0 >/dev/null 2>&1  # the opretor  is right so return 0

function input_num
{
    if [ $? -ne 0 ]
    then 
    read -p "the input not a number,Please input a number: " number1
    else
    read -p "Please input a number: " number1
    fi 

    expr $number1 + 1 >/dev/null 2>&1
    if [ $? -ne 0 ] 
    then 
    expr $number1 + 1 >/dev/null 2>&1
    input_num
    else
    echo  $number1  
    #return 
    fi  
}

echo 0 >/dev/null 2>&1  # the opretor  is right so return 0

function input_chara
{

    if [ $? -ne 0 ]
    then 
    read -p "the input not a character,Please input a character: " cha
    else
    read -p "Please input a character: " cha
    fi 

    if [ $cha != "+" ] && [ $cha != "-" ] && [ $cha != "*" ] && [ $cha != "/" ]
    then  
    ls /etv >/dev/null 2>&1   #do a mistake opretor for get a return that not a 0
    input_chara    #Because the input not a number so use the echo to output
    else 
    echo  $cha
    return   # use return to end the function
    fi
}

a=`input_num`

b=`input_chara`

c=`input_num`

echo $(($a$b$c))

猜你喜欢

转载自blog.csdn.net/qq_37699336/article/details/80783415
今日推荐