简体   繁体   中英

Shell scripting: How to check if an input argument is a Math operator

I want to build a basic calculator where i will give as arguments two numbers and one of these +,-,/,* . Example 1+2=3 . I know how to check if an argument is a number but how about the symbols?

Use case :

case "$op" in
    '*') result=$(($arg1 * $arg2))
        ;;
    /) # code for division
        ;;
    -) # code for subtraction
        ;;
    +) # code for addition
        ;;
    *) echo "Unknown operator '$op'"
        ;;
esac

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM