Bash脚本。有没有办法缩短我的代码?

时间:2018-05-09 21:06:39

标签: bash shell

这是计算器的代码,可以为您提供单词结果。我使用很多条件。我打算用最多30个数字来做。有没有办法缩短它?

#! bin/bash

    while true; do

    read -p "First number: " n1
        read -p "Second number: " n2
        PS3="what's the operation? "
        select ans in add subtract multiply divide; do
            case $ans in
                add) op='+' ; break ;;
                subtract) op='-' ; break ;;
                multiply) op='*' ; break ;;
                divide) op='/' ; break ;;
                *) echo "invalid response" ;;
            esac
        done
        ans=$(echo "$n1 $op $n2" | bc -l)
        if [ $ans == 1 ] ; then
         printf "%s %s %s = one \n\n" "$n1" "$op" "$n2"
        elif [ $ans == 2 ] ; then 
       printf "%s %s %s = two \n\n" "$n1" "$op" "$n2";
     elif [ $ans == 3 ] ; then 
       printf "%s %s %s = three \n\n" "$n1" "$op" "$n2";
     elif [ $ans == 4 ] ; then 
       printf "%s %s %s = four \n\n" "$n1" "$op" "$n2";
     elif [ $ans == 5 ] ; then 
       printf "%s %s %s = five \n\n" "$n1" "$op" "$n2";
     elif [ $ans == 6 ] ; then 
       printf "%s %s %s = six \n\n" "$n1" "$op" "$n2";
     elif [ $ans == 7] ; then 
       printf "%s %s %s = seven \n\n" "$n1" "$op" "$n2";
     elif [ $ans == 8] ; then 
       printf "%s %s %s = eight \n\n" "$n1" "$op" "$n2";
     elif [ $ans == 9] ; then 
       printf "%s %s %s = nine \n\n" "$n1" "$op" "$n2";
     elif [ $ans == 10] ; then 
       printf "%s %s %s = ten \n\n" "$n1" "$op" "$n2";
        fi

0 个答案:

没有答案