日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux计算器shell,linux – Bash Shell中的BMI计算器

發布時間:2024/8/1 linux 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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计算器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。