I have a bash script located in /home/http/mywebsite/bin/download.sh
I have a config file in /home/http/mywebsite/config/config.yaml
Now I want to read the yaml file no matter where I execute my script.
The problem:
When I cd into /home/http/mywebsite/bin/ and run ./download.sh everything works.
When I cd into /home/ and run http/mywebsite/bin/download.sh it can not find the config file because of the relative path.
How do I make sure I can always read the config file no matter where I execute the script. It is always located 4 directories up from the script in config/config.yaml
The script looks like this:
#!/bin/bash
# This will give me the root directory of my project which is /home/http/mywebsite/
fullpath="$( cd ../"$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cat ${fullpath}config/config.yaml
This works if I execute it inside the directory where the script is.
If I execute the script from another directory such as /home/ I get the following error:
cd: ../http/mywebsite/bin: No such file or directory
cat: config/config.yaml: No such file or directory
Solution?
If it is possible it would be great with a code snippet that can traverse up a path N amount of times, this would solve my problem. But it is too advanced for me.
For example you can set a variable "cd_up=1" how many times to go up. The run the loop/sed or whatever magic.
And it would turn the absolute string from:
/home/http/mywebsite/bin/
into:
/home/http/mywebsite/
And changing it to 2 it would change the string to:
/home/http/
Managed to solve it finally by using:
#!/bin/bash
cd "$(dirname "$0")"
BASE_DIR=$PWD
# Root directory to the project
ROOT_DIR=${BASE_DIR}/../
cat ${ROOT_DIR}config/config.yaml
This allows me to execute the script no matter where I am.
You can use which command to determine absolute path of the executing script no matter where you are running
BASE_DIR=$(which $0 | xargs dirname)
ROOT_DIR=${BASE_DIR}/../..
cat ${ROOT_DIR}/config/config.yaml
Lets try printing the path from different locations.
-bash-4.1$ /tmp/dir.sh
$0 - /tmp/dir.sh. Absolute path - /tmp
-bash-4.1$ cd /tmp
-bash-4.1$ ./dir.sh
$0 - ./dir.sh. Absolute path - /tmp
-bash-4.1$
-bash-4.1$ cd /usr/bin
-bash-4.1$ ../../tmp/dir.sh
$0 - ../../tmp/dir.sh. Absolute path - /tmp
Related
I am trying to cd to a different directory by using a path to open a file, but it fails to load correctly.
I am in the directory of ~/Dev/.../Examples to run a ./test.sh script.
Inside the shell script, I have the following:
#!/bin/bash
path = '~/Datasets/foo'
....
"path"/bar
The Dataset folder is right after cd ~/
I have tried $HOME instead of ~, but it still fails to load the respective file.
Here is a code to open the bar directory with nautilus (ubuntu's explorer):
#!/bin/bash
path='/home/your_user_name/foo'
nautilus $path/bar/
Rules:
Use absolute path: /home/your_user_name/, not ~/ nor $HOME.
Bash doesn't like spaces: path='~/Datasets/foo', and not path = '~/Datasets/foo'
How can I change my working directory in bash script in windows. I have
~dp0 = C:\test\docker\windows and I want to change my directory to C:\test\build
So it means 2 levels up and then int o build folder
Thanks
Since C:\ is mounted by default into /mnt/c this will work.
Create a .bashrc in your home path by following command:
echo "BUILDDIR=/mnt/c/test/build" >> ~/.bashrc;source ~/.bashrc
cd $BUILDDIR
# Do your work below for example ./configure.
./configure
On my system in Git bash the C:\ root is just /c/ and other dirs from there are whatever they are, so it would be cd /c/test/build/.
You could also still say cd ../../build/.
Good luck.
To change the directory using a bash script is just like you would using normal bash.
cd "C:/test/build"
echo "You're now in the folder, do what you will."
Save the file as .sh and it can be used as such.
Note, when navigation your folders using a bash script remember the directory you'll be starting from is always the home directory.
I'm working a script that reads back the input of a full path to a directory (e.g. ~/test) but when I run the shell script it come back with this:
test-script.sh: line 34: cd: ~/test: No such file or directory
I'm running the test-script.sh file in a different directory, so I was assuming that a simple:
cd ~/test in the script would do it, but I guess not.
I know that this is probably a redundant question, but most of the other issues/examples have been with loops and other cases outside of just doing a simple cd ~/test in a script.
And for reference this is all I'm doing in my script:
echo What directory do you want to change to?
read directory_name
cd $directory_name
Again, new to bash/shell scripting so if there are any other ideas or suggestions, I'm all for it. Thanks!
solution 1)
if you want to use ~ , you need to use eval .
#!/bin/bash
pwd
echo What directory do you want to change to?
read directory_name
eval "cd $directory_name"
pwd
But this solution is weak , because someone can enter some maliciuois command instead of a directory name and this script will execute .
solution 2)
check if the first character of directory_name is a ~ and replace it by the appropriate value , it can the current user home dir or another user home dir .
you can do cd ~ or cd ~usera to change to usera homedir
I've been working on a script but always with a fixed directory (/opt/mw/script).
I need to change that to be able to execute the script from any directory.
I think I will need to add a "." at the beginning of the line to be able to execute the script?
For example ./mw/script
Is this correct?
Thanks
You already can execute that script from any directory with that absolute file path. It's the relative file paths (that start with ./ or ../) that can only be executed from a specific directory.
You need to add the "." if the script is in the current directory (e.g. ./script), but it is optional if the script is already inside another directory (e.g. mw/script).
Also notice that if your script contains relative references to other files and directories, you may need to use this trick to properly refer to them from any directory.
For instance, consider the following script using absolute paths:
#!/bin/bash
# lists all files in this directory
ls /opt/mw
If it is converted to relative paths as follows:
#!/bin/bash
# lists all files in this directory
ls mw
Then this script will only work if you run it from its own directory (e.g. cd /opt/ && ./script).
But then you can generalize it like this:
#!/bin/bash
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# lists all files in this directory
ls $SCRIPT_DIR/mw
Now the script works even when executed from another directory.
I have a file called execute.command. When I execute it by opening it. It runs from my users folder. I would like it to run in the folder where it is located. is that possible?
You must change the script to change working directory to script-location directory:
#!/bin/bash
cd "$(dirname "$0")"
More answers here : Bash script: set current working directory to the directory of the script
I added cd dirname $0 to the top of the command line. This will cd you into the correct folder. Here are more details.
http://hints.macworld.com/article.php?story=20041217111834902