“使用问题”;Mul";及;Mod";Bash中的运算符
我不明白为什么在这个代码里“使用问题”;Mul";及;Mod";Bash中的运算符,bash,math,module,multiplication,arithmetic-expressions,Bash,Math,Module,Multiplication,Arithmetic Expressions,我不明白为什么在这个代码里 echo "Please, give me two numbers:" echo 1: read a echo 2: read b echo "a = $a" echo "b = $b" OPT="Sum Sub Div Mul Mod" select opt in $OPT; do if [ $opt = "Sum" ]; then sum=$(echo
echo "Please, give me two numbers:"
echo 1:
read a
echo 2:
read b
echo "a = $a"
echo "b = $b"
OPT="Sum Sub Div Mul Mod"
select opt in $OPT; do
if [ $opt = "Sum" ]; then
sum=$(echo $a + $b | bc -l)
echo "SUM is: $sum"
elif [ $opt = "Sub" ]; then
sub=$(echo $a - $b | bc -l)
echo "SUB is: $sub"
elif [ $opt = "Div" ]; then
div=$(echo $a / $b | bc -l)
echo "DIV is: $div"
elif [ $opt = "Mul" ]; then
mul=$(echo $a * $b | bc -l)
echo "MUL is: $mul"
elif [ $opt = "Mod" ]; then
mod=$(echo $a % $b | bc -l )
echo "MOD is: $mod"
else
clear
echo "wrong choise"
exit
fi
done
正确执行SUM、SUB和DIV,但如果我想执行MUL或MOD操作,它会给我一个错误:
(标准_in)1:语法错误
(标准)1:非法字符:~
(标准)1:非法字符:~
这应该像你期望的那样工作。您需要转义
*
和%
。而你有echo“MOD is$mul”
:
您需要引用
*
,否则它会被shell扩展
mul=$(echo $a '*' $b | bc -l)
%
可以不加引号,但为了简单起见,您可以引用所有运算符。是的,谢谢。我还发现了这个解决方案:mul=$(echo“$a*$b”| bc-l)和%
。
mul=$(echo $a '*' $b | bc -l)