0

I stored the following script in a file and created an alias to that file in the user's bashrc, then sourced that bashrc:

#!/bin/bash
domain="$1" && test -z "$domain" && exit 2

environment() {
    read -sp "Please enter the app DB root password:" dbrootp_1 && echo
    read -sp "Please enter the app DB root password again:" dbrootp_2 && echo
    if [ "$dbrootp_1" != "$dbrootp_2" ]; then echo "Values unmatched. Please try again." && exit 2 fi

    read -sp "Please enter the app DB user password:" dbuserp_1 && echo
    read -sp "Please enter the app DB user password again:" dbuserp_2 && echo
    if [ "$dbuserp_1" != "$dbuserp_2" ]; then echo "Values unmatched. Please try again." && exit 2 fi
}
environment

When I run alias domain I get this output:

+ /user/nwsm/nwsm.sh x
/user/nwsm/nwsm.sh: line 12: syntax error near unexpected token `}'
user/nwsm/nwsm.sh: line 12: `}'

Why is the syntax error? I didn't recognize a syntax error. Do you? Maybe there is another problem.

I don't see any alien characters in Nano (nor in Visual Studio Code):

enter image description here

1 Answer 1

4

I seem to have missed 2 semicolons (;) before the fi (the closure of the if statement).

These are the correct if-then statements:

if [ "$dbrootp_1" != "$dbrootp_2" ]; then echo "Values unmatched. Please try again." && exit 2; fi
if [ "$dbrootp_1" != "$dbrootp_2" ]; then echo "Values unmatched. Please try again." && exit 2; fi

Note each semicolon before the fi, near the end of each line.

If the Bash error in stderr was something like "Expected a semicolon in lines 6 and 10", I might not publish the question and answer.

Seems writing a Bash if-then statement is slightly more verbose than say, in JavaScript.

1
  • 1
    +1 for finding the mistake. fi by itself would be a valid argument to any command, so the parser can't really see that you missed the semicolon. (Of course fi doesn't make sense for exit here, but the meaning of the commands isn't relevant at that phase and can't be known in general.) Commented Mar 4, 2018 at 9:42

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.