Home > OS >  Syntax error: end of file unexpected (expecting "fi"))
Syntax error: end of file unexpected (expecting "fi"))

Time:06-22

I have the below shell script.

#!/bin/sh


i=1
for label in 'Mod Name' 'Issue No' 'UT Status' 'Dev ID' 'Loc'; do
    if ! grep -q "^    $((i  )). $label:" $1; then
        cat <<- EOF
            Proposed message does not satisfy this repository's exacting standards
            It must match the template:
                1. Mod Name: xxx
                2. Issue No: xxx
                3. UT Status: xxx
                4. Dev ID: xxx
                5. Loc: xxx

            The proposed message does not contain the label: $label" >&2
        EOF
        exit 1
    fi
done

It fails with below error.

28: /mtp-test/script.sh: Syntax error: end of file unexpected (expecting "fi"))

Why does this error occur?

CodePudding user response:

I tried by removing the space/tab before "EOF" as shown below and it worked. I am not sure why space creates an issue.

#!/bin/sh
i=1
label='Mod_Name'
for label in 'Mod_Name' 'Issue No' 'UT Status' 'Dev ID' 'Loc'; do
    if ! grep -q "^    $((i  )). $label:" $1; then
        cat <<- EOF
            Proposed message does not satisfy this repository's exacting standards
            It must match the template:
                1. Mod_Name: xxx
                2. Issue No: xxx
                3. UT Status: xxx
                4. Dev ID: xxx
                5. Loc: xxx

            The proposed message does not contain the label: $label" >&2
EOF
        exit 1
    fi
done
  • Related