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

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

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"

Change directory in script bash [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Find file then cd to that directory in Linux
(12 answers)
Make a Bash alias that takes a parameter?
(24 answers)
Closed 2 years ago.
I want to find a file and goto his directory. I made a bash script :
#!/bin/bash
FILE=$1
FILEPATH=`find . -name "$FILE"`
if [ -f "$FILEPATH" ]
then
cd $(dirname "$FILEPATH")
fi
But this script does not work. I saw on this post that I have to add exec bash or $SHELL but it create a new bash prompt ans display my welcome message.
How can I do ? I just want a script, alias or something to find a file and go to the directory containing that file.
Source your script instead of running it like you do. When you run it like you do, you spawn a new shell that executes the cd, completes succesfully, closes the shell and returns to your current shell, leaving you in your pwd.
Use source myscript.sh or . myscript.sh instead of bash myscript.sh or myscript.sh.

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]}")")

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

Retrive the file system position where a bash script is saved [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In the bash script how do I know the script file name?
I often need the file system of a bash script to reference other needed resources. Normally use the following code in line after the shebang line. It sets a variable called 'scriptPos' contains the current path of the scripts
scriptPos=${0%/*}
It works fine, but is there something more intuitive to replace the shell expansion?
There's dirname but it requires a fork:
scriptPos=$(dirname "$0")
One option is
scriptPos=$(dirname $0)
which comes at the cost of an extra process, but is more self-descriptive. If the script is in the current directly, the output differs (for the better, in my opinion):
#!/bin/bash
scriptPos=$(dirname $0)
echo '$0:' $0
echo 'dirname:' $scriptPos
echo 'expansion:' ${0%/*}
$ bash tmp.bash
$0: tmp.bash
dirname: .
expansion: tmp.bash
UPDATE: An attempt to address the shortcomings pointed out by jm666.
#!/bin/bash
scriptPos=$( v=$(readlink "$0") && echo $(dirname "$v") || echo $(dirname "$0") )

Resources