【问题标题】:Bash script IF argument adviceBash 脚本 IF 参数建议
【发布时间】:2013-06-07 21:40:37
【问题描述】:

我今天刚开始编写 bash 脚本,我需要一些帮助 :(

我有一个变量 $BE,如果它的值小于 75 或大于 89,那么我需要将值限制在这些数字上,所以我需要在其中添加一个 IF。

我的剧本可能一团糟,但正如我所说,这是我的第一个剧本。任何帮助都会很棒。该部分是制动菜单功能。

# Braking menu function

get_menu2 () {
    dialog --title "Braking Calculator" --msgbox "Please Record the time to travel 5 metres then press OK" 15 40 ;
    T=$(dialog --output-fd 1 --title "Braking Calculator" --inputbox "Please enter Time taken (in Seconds)" 9 30) ;
    S=$(echo "scale=2; (18/$T)" |bc -l) ;
    BE=$(echo "scale=2; (100-($S*1.86))"|bc -l) ;
    D=$(echo "scale=1; ((0-($S/3.6))/(-1.19))"|bc -l) ;
    dialog --title "Braking Calculator Results" --msgbox "


        The speed is $S Km/h
        The Min. Braking Eff. is $BE %
        The Max.Stopping Distance is $D M" 15 40
    get_menu1

} ;

【问题讨论】:

  • 您能否考虑将您的代码摘录精简到相关部分? 您实际询问的是。这将大大提高您获得有用答案的机会。

标签: bash bc


【解决方案1】:

我认为你想要的是这样的:

BE=$(echo "scale=2; res=(100-($S*1.86)); if (res < 75) {res=75}; if (res > 89) {res=89}; res" | bc -l)

【讨论】:

  • 谢谢,看来我正需要我去试试看。
  • 成功了,非常感谢。
猜你喜欢
  • 2021-03-05
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-31
  • 2019-11-16
  • 2010-10-24
相关资源
最近更新 更多