shell脚本中的问题

时间:2016-08-09 07:46:02

标签: linux bash shell

我在运行shell脚本时遇到错误。请帮忙[脚本的意图是检查当前操作系统版本(RHEL / CentOS)是否小于7]

==================================错误============ ==================

./test.sh: line 5: 7]: No such file or directory
PHP 5.4 will be installed by default

=============================================== ========================

#!/bin/bash
# Script Name: test.sh

VERSION=`cat /etc/redhat-release|awk '{print $4}'|cut -d "." -f1`
if [ "$VERSION" < "7" ]
then
echo "PHP 5.4 need to be installed separately"

    else
    echo "PHP 5.4 will be installed by default"

fi`

2 个答案:

答案 0 :(得分:1)

[test)内置(和外部版本)(以及关键字[[)不支持<>样式算术比较。您需要算术比较运算符((或使用-lt(小于):

(( "$VERSION" < 7 ))
[ "$VERSION" -lt 7 ] 

答案 1 :(得分:0)

#!/bin/bash
# Script Name: test.sh

VERSION=$(awk '{print $7}' /etc/redhat-release|cut -d "." -f1)
if [ "$VERSION" -lt 7 ];then

    echo "PHP 5.4 need to be installed separately"

else
    echo "PHP 5.4 will be installed by default"

fi

注意:

  1. 避免使用反击。你可以使用var = $(command)
  2. 对于整数比较,使用-lt,-gt,-eq,-ne进行比较。
  3. 检查Shell-check处的代码语法。你可以自己解决这个问题。