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"
Related
This question already has answers here:
How to manually expand a special variable (ex: ~ tilde) in bash
(19 answers)
Closed last month.
I have the following variable that shows where I should write my report to
REPORT="ZFS(~/Q4Y22/report/q4y22.md)"
I have used sedto extract the path and dirname command to get the
~/Q4Y22/report/dir name which I assign to reportdir variable.
However when I run the following test, instead of the Q4Y22 dir being created in my home dir, it literally creates the following directory hierachy ~/Q4Y22/report inside my home for example farai/~/Q4Y22/report instead of farai/Q4Y22/report
if [ -d $reportdir ]
then
echo "dir exists, keep moving"
else
mkdir -p $reportdir
fi
How do I get around this, I tried using sed to remove the tilde from the path but i was wondering if there is a more clever way
My try on this:
#!/bin/bash
REPORT="~/Q4Y22/report/q4y22.md"
reportdir=$(dirname "${REPORT/'~'/$HOME}")
if [ -d "$reportdir" ]
then
echo "dir exists, keep moving"
else
mkdir -p "$reportdir"
fi
This is a bit fragile since you could have ~ in the filename, though, but you could improve this script to replace only the first char if it's a tilde.
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
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]}")")
I wonder if there's a good way to source another shell script without $(basename "$0") solution, since sometimes "$0" is not set.
For exmaple "$0" would be "-sh" for login shell.
There's same problem with regard to cwd solution.
You could use this expression to find out the directory of the parent Bash script:
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
Then you can call your script like this:
cd $dir; source ./other_script
or
source $dir/other_script
See this related post: Getting the source directory of a Bash script from within
This question already has answers here:
How do I get the directory where a Bash script is located from within the script itself?
(74 answers)
Closed 6 years ago.
I have a shell script here:
/node_modules/.bin/exec.sh
in the exec.sh script, I want to obtain the path of the directory's parent directory that the script is contained in (not pwd/cwd!). I can obtain the containing directory like so:
`dirname $0`
which will yield:
/node_modules/.bin
but I am looking to get at one directory higher, I just want to get
/node_modules
I am having trouble searching for the answer, my guess is:
`dirname $1`
but just a guess, not sure if that's right at all. Can anyone give an explanation of how to do this and how it works?
Run dirname twice (nested).
~$ dirname $PWD
/home
~$ dirname `dirname $PWD`
/
~$
I believe the answer is
$(dirname $(dirname "$0"))
don't forget about the double-quotes around "$0"