就是想通过 .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
感谢 @@Xusually 的解决方案,亲测可行,代码如下:
# Check if Ubuntu version is greater than 18.04
echo "Checking Ubuntu version..."
VER=$(lsb_release --release | cut -f2)
function version { echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'; }
if [ $(version $VER) -ge $(version "18.04") ]; then
echo "Ubuntu version is greater than 18.04"
else
echo "Ubuntu version is less than 18.04"
exit 0
fi
1
love 2021-03-02 13:30:00 +08:00
python -c "print($VER > 18.04)" | grep True && {
echo OK } |
2
Xusually 2021-03-02 13:34:34 +08:00 1
#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 |
3
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' |
4
Xusually 2021-03-02 13:35:09 +08:00
# lsb_release --release
Release: 20.04 |
7
taolu 2021-03-02 13:52:33 +08:00 1
test $(echo "$VER > 18.04" | bc) -eq 1 && echo "Ubuntu version is greater than 18" || echo "Ubuntu version is less than 18"
|