alt-bc.sh
来自「Shall高级编程」· Shell 代码 · 共 57 行
SH
57 行
#!/bin/bash# Invoking 'bc' using command substitution# in combination with a 'here document'.var1=`bc << EOF18.33 * 19.78EOF`echo $var1 # 362.56# $( ... ) notation also works.v1=23.53v2=17.881v3=83.501v4=171.63var2=$(bc << EOFscale = 4a = ( $v1 + $v2 )b = ( $v3 * $v4 )a * b + 15.35EOF)echo $var2 # 593487.8452var3=$(bc -l << EOFscale = 9s ( 1.7 )EOF)# Returns the sine of 1.7 radians.# The "-l" option calls the 'bc' math library.echo $var3 # .991664810# Now, try it in a function...hypotenuse () # Calculate hypotenuse of a right triangle.{ # c = sqrt( a^2 + b^2 )hyp=$(bc -l << EOFscale = 9sqrt ( $1 * $1 + $2 * $2 )EOF)# Can't directly return floating point values from a Bash function.# But, can echo-and-capture:echo "$hyp"}hyp=$(hypotenuse 3.68 7.31)echo "hypotenuse = $hyp" # 8.184039344exit 0
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?