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
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 have a small script that runs a jar file :
#!/bin/bash
prefix="foo";
name=`ls ${prefix}*.jar`;
echo $name;
java -jar $name prop1.properties prop2.properties
when i run it in the terminal using ./myscript.sh, it works fine and the jar file executes, but when i rename it in myscript.command and double click it, i have this error :
ls: foo*.jar : No such file or directory
I saw that apparently a .command file opens a terminal at the root directory, so I tried finding the directory containing myscript.command using that :
dir = 'find <dir> -maxdepth 1 -type d -name '*myDirContainingJar*' -print -quit'
cd $dir
but dir is just blank, any ideas ???
Opening a shell script from Finder on macOS (whether it has extension .command or is an extension-less executable shell script) makes the current user's home directory the current directory (as of macOS 10.12).
To explicitly change to the directory in which the script itself is located in a bash script, use:
cd -- "$(dirname -- "$BASH_SOURCE")"
In this invocation scenario, the following POSIX-compliant variation, which uses $0 in lieu of $BASH_SOURCE, would work too: cd -- "$(dirname -- "$0")"
Note:
Generally, if your script is invoked via a symlink and you want to change to the target's directory (the actual script file's directory as opposed to the directory in which the symlink is located), more work is needed - see this answer of mine.
When opening a script from Finder, however, this issue does not apply, because Finder itself resolves a symlink to its (ultimate) target and then invokes the target directly.
The problem with your script is that it runs with a different working directory than you tested it with. When you do ls something in a script, the script looks in the current working directory (where you cded last in case you're in a terminal), not in the directory the script is in. In general, when writing scripts like this you should use the full path of the file you're referring to or figure out the directory of the script and point to the file relative to that. One solution works in Bash, but it might not in the shell you're using.
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 created a simple bash script. The script works just fine.
When I run echo $PATH this prints my paths, I have:
/usr/local/sbin:/usr/local/bin/:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
So i moved my script to /usr/local/bin and ran chmod +x mybash.sh. I've even chmod 0777 mybash.sh
Now, when I run ./mybash.sh I just get the "No such file or directory"
Why is this happening and where's the best place to put my scripts
Once the script is in your path, you can run it just with the filename: mybash.sh rather than the path to the file: ./mybash.sh
./mybash.sh means run mybash.sh from the current folder. If you've moved mybash.sh to /usr/local/bin, then it's no longer in ./ (your current folder), so it can't find it.
Either move to /usr/local/bin to run it using ./mybash.sh or just use mybash.sh from any folder once you've moved it into a path folder.