0

i am posting a new question to bugfix for my code.

    if [ -f /etc/centos-release ]; then
        OS="CentOs"
        VERFULL=$(sed 's/^.*release //;s/ (Fin.*$//' /etc/centos-release)
        VER=${VERFULL:0:1} # return 6 or 7
    elif [ -f /etc/lsb-release ]; then
        OS=$(grep DISTRIB_ID /etc/lsb-release | sed 's/^.*=//')
        VER=$(grep DISTRIB_RELEASE /etc/lsb-release | sed 's/^.*=//')
    elif [ -f /etc/os-release ]; then
        OS=$(grep -w ID /etc/os-release | sed 's/^.*=//')
        VER=$(grep VERSION_ID /etc/os-release | sed 's/^.*"\(.*\)"/\1/')
     else
        OS=$(uname -s)
        VER=$(uname -r)
    fi

Here elif [ -f /etc/lsb-release ]; then code shows the error.

2

If the shell reports that error on the 5th line of the script you posted, there must be some invisible error with the then on the first line.

You could view your script with cat -A to see if there are accidental ALT-space characters or other non-printable characters on the line. cat -A will show line-feed characters as $ at the end of each line, control characters with the ^ prefix, and nonprintable characters with the 8th bit set with the M- prefix. For example, TAB characters will show up as ^I.

The cat -A output can look a bit messy, but it's a great way to ensure there aren't any non-printable characters where they should not be.

  • 1
    likely a carriage return (^M) preventing the shell from recognizing the then at end of line – ilkkachu Jun 16 '18 at 19:03
  • That is one of the possibilities, yes; another common nuisance is Alt-space. cat -A should catch them all. – telcoM Jun 16 '18 at 19:27
  • Tried it. Still the same issue comes. – Kvvaradha Jun 17 '18 at 3:50
0

That script snippet runs fine for me. As @telecoM notes, is likely some odd characters within your script.

$ cat -A x1
#!/bin/bash$
    if [ -f /etc/centos-release ]; then$
        OS="CentOs"$
        VERFULL=$(sed 's/^.*release //;s/ (Fin.*$//' /etc/centos-release)$
        VER=${VERFULL:0:1} # return 6 or 7$
    elif [ -f /etc/lsb-release ]; then$
        OS=$(grep DISTRIB_ID /etc/lsb-release | sed 's/^.*=//')$
        VER=$(grep DISTRIB_RELEASE /etc/lsb-release | sed 's/^.*=//')$
    elif [ -f /etc/os-release ]; then$
        OS=$(grep -w ID /etc/os-release | sed 's/^.*=//')$
        VER=$(grep VERSION_ID /etc/os-release | sed 's/^.*"\(.*\)"/\1/')$
     else$
        OS=$(uname -s)$
        VER=$(uname -r)$
    fi$
$ ./x1
$
  • I exactly used the same code in new . it results the same issue – Kvvaradha Jun 17 '18 at 3:51
0

I ran into this type of problem. The solution for me was to run dos2unix on the file. Everything was fine afterward.

New contributor
Lazor is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.