2

I wrote a script and parts of the script are as follows:

#!/bin/bash

if [ "$1" == "this_script" ] then
      this_script --parameters
elif [ "$1" == "other_script" ] then
      other_script --parameters
else
      echo "missing argument"
fi

When I run this script, I get the error,

syntax error near unexpected token `elif'
`elif [ "$1" == "SWDB" ] then'

1) Is it an issue with line endings? I wrote the script with Notepad++ on Windows BUT i have enabled the EOL conversions under Edit to UNIX/OSX format.

2) If not line endings, what is the error?

I am running this script in bash shell on a Redhat Linux OS.

1
  • 3
    shellcheck.net would have caught this for you. Commented May 25, 2016 at 13:13

2 Answers 2

9

You need a semi-colon after if [ ... ] and before then, and the same with elif:

if [ "$1" == "this_script" ]; then
#                           ^
#                           here!
#                              v
elif [ "$1" == "other_script" ]; then

From Bash manual - 3.2.4.2 Conditional Constructs:

The syntax of the if command is:

if test-commands; then
  consequent-commands;
[elif more-test-commands; then
  more-consequents;]
[else alternate-consequents;]
fi

The test-commands list is executed, and if its return status is zero, the consequent-commands list is executed. If test-commands returns a non-zero status, each elif list is executed in turn, and if its exit status is zero, the corresponding more-consequents is executed and the command completes. If ‘else alternate-consequents’ is present, and the final command in the final if or elif clause has a non-zero exit status, then alternate-consequents is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.

Sign up to request clarification or add additional context in comments.

1 Comment

or put it on the next line.
2

The 'then' statements should be on new lines:

#!/bin/bash

if [ "$1" == "this_script" ]
then
    this_script --parameters
elif [ "$1" == "other_script" ]
then
    other_script --parameters
else
    echo "missing argument"
fi

Works for me in that format.

Comments

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.