0

I'm getting below error while running a shell script. Kindly help [intention for the script is to check whether current OS version (RHEL/CentOS) is less that 7 or not]

================================== ERROR ==============================

./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`
4
  • it is hard to understand your question. please rewrite it so we could see the script properly. Commented Aug 9, 2016 at 7:48
  • what is the (') apostrophe before "#!/bin/bash" ?? Commented Aug 9, 2016 at 7:51
  • 1
    Use -lt instead of <. Also use [[]] instead of [] to prevent the shell from interpreting < and > as file descriptors/operators. Commented Aug 9, 2016 at 7:52
  • @stzahi that was (`) which came by default while pasting the code on forum. Commented Aug 9, 2016 at 8:29

2 Answers 2

1

The [ (test) builtin (and external one) (and also keyword [[) does not support <, > style arithmetic comparisons. You need the arithmetic comparison operator (( or use -lt (less than):

(( "$VERSION" < 7 ))
[ "$VERSION" -lt 7 ] 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You so much to make me understand where i was wrong. Once again thank you.
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

Note:

  1. Avoid using backtick. you can use var=$(command)
  2. For integer comparison , use -lt ,-gt,-eq ,-ne for comparison.
  3. Check your code syntax at Shell-check. You could have solved this yourself.

1 Comment

Some explanation would be useful for OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.