This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 2 years ago.
The community is reviewing whether to reopen this question as of 5 days ago.
I'm working with a bash script that is currently working on a server (RHEL4). I'm developing on my laptop with Ubuntu 10.04, but I don't think the platform is causing the problem.
Here's what's happening:
I have a skeleton script that calls another script that does most of the work. However, it makes calls to getConfig.sh a lot. getConfig.sh basically just parses some command line argument (using getopts) and calls a Java program to parse some XML files. Anyways, getConfig.sh is throwing up lots of errors (but still seems to work).
Here's the message that I'm getting
getconfig.sh: 89: [[: not found
getconfig.sh: 89: [[: not found
getconfig.sh: 94: [[: not found
I get those three errors every time it runs; however, the script completes and the Java code runs.
Here's the relavent code section
parseOptions $*
if [[ "${debugMode}" == "true" ]] ; then
DEBUG="-DDEBUG=true"
echo "${JAVA_HOME}/bin/java ${DEBUG} -Djava.endorsed.dirs=${JAXP_HOME} -jar $(dirname $0)/GetXPath.jar ${XML_File} ${XPath_Query}"
fi
Line 89 is "parseOptions $* and line 94 is "fi"
Thanks for the answers.
If your script is executable and you are executing it like ./getconfig.sh, the first line of your script needs to be:
#!/bin/bash
Without that shebang line, your script will be interpreted by sh which doesn't understand [[ in if statements.
Otherwise, you should run your script like bash getconfig.sh, not sh getconfig.sh. Even if your default shell is bash, scripts run with sh will use a reduced set of bash's features, in order to be more compliant with the POSIX standard. [[ is one of the features that is disabled.
Use:
bash scriptname.sh
instead of:
sh scriptname.sh
If you are checking for equality, shouldn't the if be ?
if [[ "${debugMode}" = "true" ]]; then
....
fi
Related
This question already has answers here:
Bash syntax error: "[[: not found" [duplicate]
(3 answers)
Closed 2 years ago.
I am creating a script using bash. However during the IF statement it raises this error: [[: not found.
I read this topic in other posts but it seems that my predecessors were writing bad their code (e.g. forgetting spaces or else). My question is a little bit different because THE SAME code if run within other parts does not work but if I launch it totally alone it correctly works.
Why does this happens? I permit that the variable used is the only one along the whole code.
echo "Digit how many codon positions do you want to use for your partition. [2-3]"
read codonpos
echo $codonpos
[[ "$codonpos" = "2" ]] && echo im here
I also tried:
echo "Digit how many codon positions do you want to use for your partition. [2-3]"
read codonpos
echo $codonpos
if [[ "$codonpos" = "2" ]]
then
echo im here
fi
I repeat you that if launch independently it works but, if this is embedded in a larger code it doesn't.
I solved the error myself. The issue was due to the presence of an error in the shebang.
I wrote #!/bin/sh instead of #!/bin/bash.
This question already has answers here:
Strange "echo" behavior in shell script
(3 answers)
Variables overwriting text problem with "echo" in Bash
(2 answers)
Closed 3 years ago.
This is the simple command that is giving me problems -
echo hafsda sfsdfdsfs $ymn $ymx $range
The output of this command is coming -
2.568 sfsdfdsfs 86.72
Where ymn = 86.72 ymx = 89.28 and range = 2.56. This only happens when I am using variables. The following command works fine -
echo hafsda sfsdfdsfs 1 2 $range
Also, the same command (the first one) works fine if I try running it directly in the terminal. This is only happening is a script. I also tried to use printf but encountered similar results.
I don't even understand what to google for to resolve this. I am unable to understand what is happening at all. So, what is happening here? Is this reproducible or is this just some error on my system, and if it is, what might be the problem?
Your script probably has DOS-style CRLF line endings. I suspect you actually have ymn="86.72\r" ymx="89.28\r" and range="2.56\r". You can test this in your script with
echo hafsda sfsdfdsfs $ymn $ymx $range | od -c
You can fix your script with dos2unix or sed -i 's/\r$// script.sh`.
Make sure you change the settings of your text editor do use unix line endings.
This question already has answers here:
Getting "command not found" error in bash script
(6 answers)
Closed 3 years ago.
I'm having a BASH script:
#!/usr/bin/env bash
PATH=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
YAML=$(envsubst < ${PATH}/test.yml)
echo "${YAML}"
It results:
./test.sh: line 5: envsubst: command not found
I see that having a variable inside command substitution causes such error.
If I'm not using any:
YAML=$(envsubst < ./test.yml)
Then I'm having an expected successful script execution.
I've tried different syntax using quotes all over the place, but nothing helped.
How do I successfully use variable inside command substitution?
Bash version:
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
UPDATE: A duplicate question. Found an answer here:
https://stackoverflow.com/a/5642584/5935309
The problem is that you are changing PATH, which is used by Bash internally to define where to look for programs (and in which order).
You changed PATH to only contain the current working dir, and that's not where envsubst is located.
The solution is to use something different than PATH, like FILE_PATH.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm trying to execute this script from the terminal (via sh PreStartupCommands.sh) and I get the following error in my terminal window:
/home/MinecraftServers/Ephona/serverconfig/PreStartupCommands.sh: line 14: syntax error near unexpected token `fi'
/home/MinecraftServers/Ephona/serverconfig/PreStartupCommands.sh: line 14: `fi'
PreStartupCommands.sh
#!/bin/bash
############################################
########### PreStartupCommands #############
############################################
#
### Update Ephona_sky ###
if [ -f '/home/MinecraftServers/Ephona/Ephona_sky_edit/lock.txt' ] ; then
echo "Lock exists"
else
echo "Lock Does Not Exist"
echo "Copying World ..."
echo "World Copied"
echo "Creating Lock"
fi
Everything appears fine to me.
The spacing around the [ and ] exists
The if, then, else, fi tags are all included
All quotations are opened and closed
Spelling is correct...
Update: The issue turned out to be that the script file had Windows-style line endings (\r\n) rather than what bash - and Unix utilities in general - expect (\n only), as #devnull suspected.
Normally, sh is symlinked to bash on CentOS, so your script SHOULD work fine.
(As an aside: any POSIX-compliant shell should work with the specific sample script in your question, however.
General caveat: As #Scrutinizer points out in a comment, invoking bash as sh implies subtle changes in behavior - see http://www.gnu.org/software/bash/manual/html_node/Bash-POSIX-Mode.html (*) - though bashisms still work).
Generally, however, if you're invoking a bash script, it's both conceptually clearer and safer to invoke it with bash - given that sh could have been redefined to invoke a different shell.
Of course, you can run chmod +x PreStartupCommands.sh to make your script executable by itself, which makes the problem go away (assuming the problem is related to what executable is processing the script).
(*) Invoking bash as sh on OSX has additional implications (since sh is a separately compiled executable rather than a symlink there), namely that shell option xpg_echo is turned ON by default, and that in the absence of $FCEDIT, ed is used (without checking for $EDITOR, as bash normally does).
This question already has answers here:
Bash and Test-Driven Development
(8 answers)
Unit testing Bash scripts
(16 answers)
Closed 5 years ago.
Can someone explain how to test for a bash shell script?
For example i've got a .sh file with this code in it...
#!/bin/sh
for file in *.txt; do
mv "$file" "`basename $file .txt`.doc"
done
How do I write a test for it? Like in Java you've got unit testing where you write code like assertEquals to test the code gives the desired output.
Try this out: assert.sh
source "./assert.sh"
local expected actual
expected="Hello"
actual="World!"
assert_eq "$expected" "$actual" "not equivalent!"
# => x Hello == World :: not equivalent!
You can do asserts in Bash. Check out this from the Advanced Bash-Scripting Guide:
http://tldp.org/LDP/abs/html/debugging.html#ASSERT
I'd add an echo in front of the mv to verify that the right commands are being created, for starters. (Always a good idea with commands that make possibly difficult to undo changes.)
Some possibly useful resources:
shUnit2 — xUnit framework for script unit testing
Bash IDE for Vim
Bash debugger