利用bash将整数乘以带有if语句的浮点数

利用bash将整数乘以带有if语句的浮点数,bash,if-statement,floating-point,int,multiplication,Bash,If Statement,Floating Point,Int,Multiplication,利用bash将一个整数乘以一个带有if语句的浮点数,我将用0.90之外的另外两种情况进一步扩展它,只是尝试学习基本知识 #!/bin/bash echo -n "give me an integer: [ENTER]: " read that_integer if -n [ $that_integer <= 5000 ]; then awk print $that_integer*0.90 done #/bin/bash echo-n“给我一个整数:[输入]:” 读那个整数 如果-

利用bash将一个整数乘以一个带有if语句的浮点数,我将用0.90之外的另外两种情况进一步扩展它,只是尝试学习基本知识

#!/bin/bash

echo -n "give me an integer: [ENTER]: "
read that_integer

if -n [ $that_integer <= 5000 ]; then
awk print $that_integer*0.90 
done
#/bin/bash
echo-n“给我一个整数:[输入]:”
读那个整数

如果-n[$that_integer则
-n
不做任何操作;将其删除

对于整数比较,请使用
-le

if [ $that_integer -le 5000 ]; then
awk
调用的正确语法为

awk -v x=$that_integer 'BEGIN { print x*0.90 }' < /dev/null
awk-vx=$that_integer'BEGIN{print x*0.90}'

如果
语句以
fi
终止,而不是
done

如您所知,
bash
无法处理浮点运算,但您可以重新强制转换该语句以使用整数数学:

x=$((5000*90/100))

echo $x
4500
所以,只是为了好玩:

#!/bin/bash

echo "give me an integer: [ENTER]: "
read that_integer

multiplier=40
[ $that_integer -le 5000 ] && multiplier=90
[ $that_integer -gt 5000 -a $that_integer -le 50000 ] && multiplier=70
echo $(($that_integer*$multiplier/100))
您可以使用该实用程序在shell中执行浮点运算

echo "give me an integer: [ENTER]: "
read that_integer

if [ $that_integer -le 5000 ]; then
    echo "$that_integer*0.90"|bc
elif [ $that_integer -gt 5000 -a $that_integer -le 50000 ]; then
    echo "$that_integer*0.70"|bc
else
    echo "$that_integer*0.40"|bc
fi

您不需要在
awk
中使用
,但是使用
BEGIN
它将不需要它。
awk-vx=4'BEGIN{print 5*x}“
也可以。啊,我没有意识到如果唯一的规则是
开始
规则,它会忽略标准输入。+1作为一个好的早期问题。当然,你可以在一行awk中完成所有这些(在所有情况下只创建一个进程),即
awk-vi=$that_int'BEGIN{if(i5000&&i)
echo "give me an integer: [ENTER]: "
read that_integer

if [ $that_integer -le 5000 ]; then
    echo "$that_integer*0.90"|bc
elif [ $that_integer -gt 5000 -a $that_integer -le 50000 ]; then
    echo "$that_integer*0.70"|bc
else
    echo "$that_integer*0.40"|bc
fi