linux计算器shell,linux – Bash Shell中的BMI计算器
我試圖在Linux中使用Bash shell創建一個腳本來計算BMI.我知道我只是在做一些愚蠢的事情,但我似乎無法使它發揮作用.它不會做分裂.你能看出我出錯的地方嗎?
#!/bin/bash
#==============================================================
# Script Name: bmicalc
# By: mhj
# Date: March 25, 2014
# Purpose: calculates your bmi from your weight & height
#===============================================================
#Get your weight and height
echo -n "What is your weight in pounds? "
read weight
echo -n "What is your height in inches? "
read height
#calculate your bmi
let total_weight=$weight*703
let total_height=$height*$height
bmi=$total_weight/$total_height
echo "Your weight is $weight"
echo "Your height is $height"
echo -n "Your BMI is $bmi"
解決方法:
你快到了,你只需要另一個讓:
let bmi=$total_weight/$total_height
備擇方案
在shell中有很多種算術上下文的方法.首選的標準方法是$(())語法:
total_weight=$(( $weight * 703 ))
這個和expr(見下文)幾乎是唯一可以在POSIX中運行的. (還有$[],但是這個已被棄用,并且大部分都與雙parens相同.)
通過將變量聲明為整數,可以獲得一些語法效率.具有整數屬性的參數會導致所有賦值表達式的RHS具有算術上下文:
declare -i weight height bmi total_weight total_height
total_weight=weight*703
total_height=height*height
bmi=total_weight/total_height
不要再讓了.
您也可以直接使用(())語法.
(( total_weight=weight*703 ))
(( total_height=height*height ))
(( bmi=total_weight/total_height ))
最后,expr只是shell的痛苦.
total_weight=$(expr $weight '*' 703) # Have to escape the operator symbol or it will glob expand
total_height=$(expr $height '*' $height) # Also there's this crazy subshell
……但是,完整!
最后,在bash數組中,索引將始終具有算術上下文. (但這并不適用于此.)
但是,這些方法都不會進行浮點計算,因此您的分區將始終被截斷.如果需要小數值,請使用bc,awk或其他編程語言.
標簽:bash,shell,linux
來源: https://codeday.me/bug/20190629/1322459.html
總結
以上是生活随笔為你收集整理的linux计算器shell,linux – Bash Shell中的BMI计算器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 史上最全IT技能学习大全公众号
- 下一篇: Linux 句柄是什么