shell编程 找到最小值

第一次写shell脚本,记录一下,什么都不懂,先来一个最简单的demo练练手,记录一下遇到的问题。

#!/bin/bash
        echo "input 3 nums:  "
        echo "the first num is "
        read num1
        echo "the second num is "
        read num2
        echo "the third num is "
        read num3

        if [ $num1 -gt $num2 ]
        then
                let min=$num2
        else
                let min=$num1
        fi

        if [ $num3 -le $min ]
        then
                let min=$num3
        fi

        echo "the min num is $min"    

首先第一行

bash是目前用的最多的(我也没数据,我也没调查,看别人这么说我也就这么认为吧)

上边的read是从屏幕中获取值。运行情况如图所示:

还可以这么写:

echo "the first num is $1"

echo "the second num is $1"

echo "the third num is $3"

那么就这么运行:

./3num.sh 1 2 3

 []  里侧要加空格,不然会出错。

  if [ $num1 -gt $num2 ]

等号=两边不能有空格,不然也会出错。(迷) 

let min=$num3
发布了125 篇原创文章 · 获赞 31 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/Fiverya/article/details/102664306