bash 判断 Ubuntu 版本是否大于 18.04

2021-03-02 13:15:53 +08:00
 sudoy

就是想通过 .sh 文件判断 Ubuntu 版本是否高于 18.04 。搜了半天没找到合适的,下面这个还是错的。哪位大佬帮忙改下

VER=$(lsb_release --release | cut -f2)

if [ "$VER > 18.04" | bc ]; then
        echo "Ubuntu version is greater than 18"
else
        echo "Ubuntu version is less than 18"
exit 0
fi
996 次点击
所在节点    问与答
7 条回复
love
2021-03-02 13:30:00 +08:00
python -c "print($VER > 18.04)" | grep True && {
echo OK
}
Xusually
2021-03-02 13:34:34 +08:00
#cat ver.sh

function version { echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'; }

VER=$(lsb_release --release | cut -f2)

if [ $(version $VER) -ge $(version "18.04") ]; then
echo "Ubuntu version is greater than 18"
else
echo "Ubuntu version is less than 18"
fi

# chmod +x ver.sh
# ./ver.sh
Ubuntu version is greater than 18
miscnote
2021-03-02 13:35:06 +08:00
just my way:
perl -e 'open $fd,"/etc/issue";$str=<$fd>;($ver)=$str=~/(\d+\.\d+\.\d+)/;print "true" if $ver>18.04'
Xusually
2021-03-02 13:35:09 +08:00
# lsb_release --release
Release: 20.04
sudoy
2021-03-02 13:50:04 +08:00
@Xusually 感谢!我测试了这个方案可行的!
sudoy
2021-03-02 13:50:10 +08:00
@love @miscnote 两位这个方法好像是要依赖于 python 和 perl,我想要纯 bash 脚本的
taolu
2021-03-02 13:52:33 +08:00
test $(echo "$VER > 18.04" | bc) -eq 1 && echo "Ubuntu version is greater than 18" || echo "Ubuntu version is less than 18"

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/757598

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX