Bad substitution error in shell script while extracting directory [duplicate] - bash

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 6 months ago.
I am getting bad substitution error on running the following shell script. (Line numbers written just for reference):
Line 11> SCENARIO_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
Line 12> SCENARIO_PATH="${SCENARIO_DIR}/scenarios"
The error in TeamCity is
| ./k6-run-all.sh: line 12: syntax error: bad substitution
Please note that on running this in local, I do not get this error and the scenario path is correctly extracted. But when I run this on TeamCity (which runs on Docker) it is giving me the above error.
Scenario path in my local is: /Users/sonaliagrawal/Documents/antman/src/scenarios/full-card-visa
Scenario path in TeamCity is extracting correctly despite the error which is:
//scenarios/full-card-visa
Solution tried:
Since in TeamCity, SCENARIO_DIR is itself just / hence I wrote an if then else to handle it, but it didn't help solve the substitution error, it just corrected the path to /scenarios/full-card-visa. The code I had added is as follows-
SCENARIO_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
echo "Scenario directory $SCENARIO_DIR"
SCENARIO_PATH=""
if [[ "$SCENARIO_DIR" = "/" ]]; then
SCENARIO_PATH="/scenarios"
else
SCENARIO_PATH="${SCENARIO_DIR}/scenarios"
fi
Reference:
In case it helps, Dockerfile is as follows:
FROM loadimpact/k6:0.34.1
COPY ./src/lib /lib
COPY ./src/scenarios /scenarios
COPY ./src/k6-run-all.sh /k6-run-all.sh
WORKDIR /
ENTRYPOINT []
CMD ["sh", "-c", "./k6-run-all.sh"]

It's because your script isn't being executed as a bash script. Put the following on the top of the .sh file.
#!/bin/bash

Related

Bash script get absolute path [duplicate]

This question already has answers here:
How do you normalize a file path in Bash?
(24 answers)
Difference between sh and Bash
(11 answers)
Closed last year.
I'm not familiar with bash scripting. In my app, I have three folders. Frontend, backend, and script are the three folders. My bash script is contained within the script. This is the structure of my app Image. I'd like to retrieve absolute path variables (frontend and backend) from my script. I'm not sure what to do. I was successful in obtaining the script path folder. My objective is to obtain an absolute route from a script.
#!/bin/sh
root_path="$( cd "$(dirname "$0")" >/dev/null 2>&1 || exit ; pwd -P )"
#frontend_path=
#backend_path=
echo "Getting absolute path ${root_path}"
Adding /.. after the output of dirname should do the trick to get you the root path:
root_path="$( cd "$(dirname "$0")/.." >/dev/null 2>&1 || exit ; pwd -P )"
From that on, you can just add /frontend / /backend to get the other paths:
frontend_path="$root_path/frontend"
backend_path="$root_path/backend"

"basename" used in subshell returns "command not found" [duplicate]

This question already has an answer here:
Find "command not found" when executed in bash loop
(1 answer)
Closed 4 years ago.
When running this script:
#!/bin/sh -ex
if [[ $# -ne 1 ]]; then
echo "./import-public-ssh-key.sh <absolute path to public key>"
exit 1;
fi
PATH=$1
KEY=$(basename ${PATH})
I get:
./import-public-ssh-key.sh: line 9: basename: command not found
without the subshell basename works:
$ basename /Users/mles/.ssh/id_rsa.pub
id_rsa.pub
Why is basename not working in the subshell? I'm on a mac if this is relevant.
You reset the PATH. Don't do that. The shell searches all the directories listed in PATH, and you have changed it so that PATH no longer contains the directory that contains basename.

Error on Bash script when running from windows

I am new to bash. I am trying to configure searchguard(a plugin of elasticsearch). For this, I need to run the sgadmin.sh file. Below is the content of the file.
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BIN_PATH="java"
if [ -z "$JAVA_HOME" ]; then
echo "WARNING: JAVA_HOME not set, will use $(which $BIN_PATH)"
else
BIN_PATH="$JAVA_HOME/bin/java"
fi
"$BIN_PATH" $JAVA_OPTS -Dorg.apache.logging.log4j.simplelog.StatusLogger.level=OFF -cp "$DIR/../*:$DIR/../../../lib/*:$DIR/../deps/*" com.floragunn.searchguard.tools.SearchGuardAdmin "$#"
Below is the error received when we run the sgadmin.sh file.
$(BASH_SOURCE[0]): bad substitution
Error: Could not find or load main class com.floragunn.searchguard.tools.SearchGuardAdmin
Can anyone explain what does the above code means, more specifically the last line
"$BIN_PATH" $JAVA_OPTS -Dorg.apache.logging.log4j.simplelog.StatusLogger.level=OFF -cp "$DIR/../*:$DIR/../../../lib/*:$DIR/../deps/*" com.floragunn.searchguard.tools.SearchGuardAdmin "$#"
Note:
Our environment is Windows server 2012
To execute the .sh file, we have copied the SH folder to C:\ drive and added this to environment variables(path).

Error when composing commands [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 6 years ago.
I obtain the directory of the current file using this code:
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
I would like to go down one level in the file structure.
For instance if my file is at:
/a/b/c/myFile.sh
dir will be /a/b/c
I would like to go to /a/b
So I am running this command:
containingdir = "$( cd "$dir" && cd ".." && pwd )"
However, I'm getting this error on the line where I defined the command:
myFile.sh: line 13: containingdir: command not found
What's the cause of this error?
Just use dirname (twice if needed):
reut#reut:~$ dirname $(dirname /a/b/c/myFile.sh)
/a/b

dart2js throwing no such file or directory error

Quite simply, I have a link to dart2js in my /usr/local/bin/ which throws repeated erros when run.
I ran
sudo ln -s /Users/macbook/Development/dart/dart-sdk/bin/dart2js /usr/local/bin/dart2js
When running dart2js from terminal, I'm presented with
...
/usr/local/bin/dart2js: line 9: 1=/Users/macbook/Development/dart/dart-sdk/bin/dart2js: No such file or directory
/usr/local/bin/dart2js: line 9: 1=/Users/macbook/Development/dart/dart-sdk/bin/dart2js: No such file or directory
/usr/local/bin/dart2js: line 9: 1=/Users/macbook/Development/dart/dart-sdk/bin/dart2js: No such file or directory
/usr/local/bin/dart2js: line 9: 1=/Users/macbook/Development/dart/dart-sdk/bin/dart2js: No such file or directory
/usr/local/bin/dart2js: line 9: 1=/Users/macbook/Development/dart/dart-sdk/bin/dart2js: No such file or directory
... etc
I imagine I'm simply using links incorrectly, but I'm not knowledgable enough to know why.
I run into this a while ago and posted this issue Can't run dart command line script using a symlink
I also added a workaround which may help in your situation as well - you probably need to adapt it a bit but it should get you started.
A workaround I use now is to create a bash script named 'testscript' in the same directory as testscript.dart and link to this script instead
I lookup the current directory of the bash script and start the dart script
(Getting the source directory of a Bash script from within).
#!/bin/bash
ME="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
${DIR}/${ME}.dart ${BASH_SOURCE[0]}
Instead of getting the command's (symlinks) name with io.Platform.script I have to pass it as an argument to have it available inside the dart script (I use the name of the symlink used to start the script like an argument inside the dart script)
This way I have to distinguish if the script was started directly and use io.Platform.script and args[0] otherwise.
I think this should all be easier.

Resources