Error when composing commands [duplicate] - bash

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

Related

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

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

Fix paths on bash running on windows with MinGW [duplicate]

This question already has answers here:
Windows PATH to posix path conversion in bash
(8 answers)
Closed 7 months ago.
If I have a bash script like:
PATH_SCRIPT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
echo "$PATH_SCRIPT"
If will echo something like:
/c/path/to/where/the/file/is
And generally, that is okay. But in some places, if running on windows, I'll actually need to parse this variable so it looks like this:
C:\path\to\where\the\file\is
How can I do that properly in bash and be able to pass it around?
If you are using Git-Bash, try
cygpath -aw /c/path/to/where/the/file/is

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"

Why does DIR="$( cd "$( dirname "$( readlink "${BASH_SOURCE[0]}" )" )" && pwd )" return "no such file or directory" when calling ${DIR}? [duplicate]

This question already has answers here:
Bash path contained space no such file or directory
(1 answer)
Spaces in bash variable path name
(1 answer)
Closed 4 years ago.
I am currently trying to find the full directory name of a script I am calling. I found code online that uses:
DIR="$( cd "$( dirname "$( readlink "${BASH_SOURCE[0]}" )" )" && pwd )"
to set the directory, then afterwards, I try to open a file by calling:
open ${DIR}/file_open.jpg
to which I get -bash: /Volumes/Drive: No such file or directory. I am unsure why this doesn't work. I am running it from Mac OSX's terminal. Does anyone have any ideas?
Because you didn't quote your variable.
open "${DIR}/file_open.jpg"
You must quote it. See here why quoting is important.
Also DIR is simpler as:
DIR=$(dirname "$(readlink "${BASH_SOURCE[0]}")")

Bash make and open directory [duplicate]

This question already has answers here:
One command to create and change directory
(9 answers)
Closed 7 years ago.
Let's say I have the following command:
mkdir directory && cd directory
I normally do this a lot during the day so I'm wondering if there is a simpler shorter way of doing this.
Does anybody know?
you can call last argument by &_
mkdir directory && cd $_
this is result
system:/tmp # mkdir directory && cd $_
system:/tmp/directory #
Put the following code in your ~/.bashrc or ~/.zshrc :
mkcd () {
mkdir "$1"
cd "$1"
}
Then in your shell, enter the following command mkcd foo. As you can see, this function need one argument which are the name of the directory.

Resources