Bash CLI计算器计算结果异常问题求助
Bash CLI计算器计算错误排查及修复尝试
我用Bash编写了一款依赖bc工具的CLI计算器,但出现多处问题:
- 计算
1.2+1.2得到结果4 - 计算
-1*-1得到结果65025 - 输入小数时触发
numeric argument required参数错误提示
已尝试修改返回值传递方式,改为通过输出传递数值,但仍需进一步排查问题。
原脚本代码
主脚本
#!/bin/bash source utilities/menu.sh source utilities/input_validation.sh header="Choose from the options below(Example: 1, 2, 3 etc...):" options=("1" "2" "3" "4" "5") option_descriptions=("Add" "Subtract" "Multiply" "Divide" "Exit") prompt="> " calculate(){ if [[ $option == "1" ]] then echo "$num1 + $num2" | bc elif [[ $option == "2" ]] then echo "$num1 - $num2" | bc elif [[ $option == "3" ]] then echo "$num1 * $num2" | bc elif [[ $option == "4" && $num2 != "0" ]] then echo "scale=2; $num1 / $num2" | bc elif [[ $option == "4" && $num2 == "0" ]] then echo "Dividing by 0 is undefined" else exit=0 fi } calculator(){ persistant_get_menu_option "$header" "$options" "$option_descriptions" option=$? if [[ $option == "5" ]] then return 1 else persistent_get_number "$prompt" num1=$? persistent_get_number "$prompt" num2=$? echo "------------------------------------" calculate "$option" "$num1" "$num2" echo "------------------------------------" return 0 fi } will_exit="0" while [[ $will_exit == "0" ]] do calculator if ! [ $? = 0 ] then will_exit="1" else will_exit="0" fi done echo "Good bye"
工具脚本:utilities/menu.sh
#!/bin/bash is_option(){ for option in "${options[@]}" do if [ "${option}" = "$choice" ] then return 0 fi done return 1 } persistant_get_menu_option(){ choice="" will_exit="0" while [[ $will_exit == "0" ]] do echo "" echo "$header" echo "-----------------------------------------------------------" i=0 for description in "${option_descriptions[@]}" do echo "${options[i]}: $description" i=$((i + 1)) done echo "" read -r choice is_option "${options[@]}" "$choice" if [[ $? == "1" ]] then echo "You must choose from the options given" else will_exit="1" fi done return "$choice" }
工具脚本:utilities/input_validation.sh
#!/bin/bash is_numerical(){ if ! [[ $user_input =~ (^-?0\.[0-9]*[1-9]+[0-9]*$)|(^-?[1-9]+[0-9]*((\.[0-9]*[1-9]+[0-9]*$)|(\.[0-9]+)))|(^-?[1-9]+[0-9]*$)|(^0$){1} ]] then return 0 else return 1 fi } persistent_get_number(){ user_input="" will_exit="0" while [ $will_exit == "0" ] do echo "$prompt" read -r user_input is_numerical "$user_input" if ! [ $? = 0 ] then echo "Input must be numerical" else will_exit="1" fi done return "$user_input" }
报错示例
计算1.2+1.2时的报错
1 > 1.2 utilities/input_validation.sh: line 27: return: 1.2: numeric argument required > 1.2 utilities/input_validation.sh: line 27: return: 1.2: numeric argument required ------------------------------------ 4 ------------------------------------
计算-1*-1时的报错
> -1 > -1 ------------------------------------ 65025 ------------------------------------
更新:修改返回值传递方式
尝试将calculator和persistent_get_number函数改为通过输出传递数值,替代原来的返回值传递:
修改后的calculator函数
calculator(){ persistant_get_menu_option "$header" "$options" "$option_descriptions" option=$? if [[ $option == "5" ]] then return 1 else echo "> " num1=$(persistent_get_number) echo "> " num2=$(persistent_get_number) echo "------------------------------------" calculate "$option" "$num1" "$num2" echo "------------------------------------" return 0 fi }
修改后的persistent_get_number函数
persistent_get_number(){ will_exit="0" while [ $will_exit == "0" ] do read -r user_input is_numerical "$user_input" if ! [ $? = 0 ] then echo "Input must be numerical" else will_exit="1" fi done echo $user_input }
内容的提问来源于stack exchange,提问作者Daniel
相关产品推荐
相关产品推荐

