0

What is wrong with this script?

#!/bin/sh
SONAR="~/SonarQube/bin/linux-x86-64/sonar.sh"
echo "Waiting to start..."
$($SONAR start)

It says ./start.sh: 4: ./start.sh: ~/SonarQube/bin/linux-x86-64/sonar.sh: not found but it does exist. I tried a huge amount of variations it does work only with eval: eval $SONAR start.

1
  • I don't have access to a bourne shell at the moment, but you might try adding a -x after the /bin/sh... "#!/bin/sh -x" which will echo the commands before it executes them. This might give you more insight into what is actually happening. Commented Apr 27, 2017 at 22:56

3 Answers 3

4

Try this instead :

#!/bin/sh
SONAR=~/"SonarQube/bin/linux-x86-64/sonar.sh"
echo "Waiting to start..."
"$SONAR" start

Tilde expansion is disabled by quoting (like globbing), so you need to put the tilde outside the quotes.

The last line should not be inside $(), as doing this first executes the command, and then expands the content and tries to execute a commande expressed by such content.

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

2 Comments

Please try again, I fixed the code, the slash needs to be outside the double quotes too.
Please note I have added double quotes around the $SONAR expansions, in case the path were to contain characters that could trigger word splitting (such as spaces).
3

Scripts usually use $HOME instead of ~. $HOME will expand even inside double quotes.

#!/bin/sh
SONAR="$HOME/SonarQube/bin/linux-x86-64/sonar.sh"
echo "Waiting to start..."
"$SONAR" start

Comments

2

You're using the ~ between doublequotes, so it's not going to be interpreted as /home/whatever.

#!/bin/sh
SONAR=~/SonarQube/bin/linux-x86-64/sonar.sh

4 Comments

I'm not sure why biowep did that, so since the question isn't about this part, I don't think it's worth changing.
It sure warrants at least a warning, as someone who needs help with tilde expansion is very likely to not fully understand this line.
@yoones I wanted to verify the output of the command into an if. For faster read I provided only the minimal ammount of code necessary to expose the problem.
@biowep OK I removed the line. If this answers your question please accept it as answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.