Run a Shell Script from the Directory it is in [duplicate] - bash

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 9 years ago.
I am relatively new to shell scripts. I am trying to create a script that can initiate grunt.js by launching the script in Finder (Mac). I want to be able to place this script anywhere and for it to just work by running the version of grunt in the folder the script is located.
How can I achieve this? I have looked for a few hours trying to find a solution but due to my lack of knowledge I am struggling.
Does anyone have a snippet that can CD to the directory the script is stored and run a command?

If you call cd "$(dirname "${BASH_SOURCE[0]}" )" then from that point on the current directory will be the one that the script is stored in.

Related

Run batch file located in sub directory with another batch file [duplicate]

This question already has answers here:
Get current batchfile directory
(4 answers)
Closed 1 year ago.
So I know you think I should do something like: start C:\parentfolder\subfolder\batchfile.bat
or something along those but I was wonder if there was like a way to reference the sub directory without using it's actual name? Since the batch files will be generated by another batch file.
Don't ask why I need it to be generated I'd much rather it not but couldn't find any other way.
You have to cd into a subdirectory and then run start start.bat. That should run another batch file. To go into a relative subdirectory, run the command cd foldername. You have to make sure you are currently in the parent directory.

How to make local .command Terminal script? [duplicate]

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 1 year ago.
I created a .command file that runs a certain command java -jar myApp.jar in the Terminal, however it doesn't work because it first needs to cd into a certain directory.
Is there a way to make the .command file automatically go to the address where the .command file currently is, and execute the command from there?
I can't just add the cd line into the script because I need to distribute this and it needs to work no matter where on the computer it is.
In a bash script:
cd "$(dirname "$0")"
will cd into the directory containing the script.

How to find the location of the script that is being executed? [duplicate]

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 4 years ago.
How can I find (if possible) the location of the script that is being executed?
So I have a script that I'll use to do some magic, and I'll need to call it from random locations on a system (this system can be mine, my servers, my friend's PC, my mom's PC, etc).
Now, I need to execute some binaries present along with the script, How can I do that?
Say my script folder has 3 files:
binary1
myScript.sh
binary2
Now, this whole folder can be located in "/tmp/scripts", on one system, and in "/home/user/Downloads" on other system(random locations).
And I'll need to run this script from, lets say "/home/user/Desktop".
So my question is, how can I execute the binary1 and binary2 from my script without knowing their actual path before hand?
The only executables that you can execute are either present in your PATH variable or initialization files (like aliases and functions).
So, couple of options. Try to determine the location where your scripts are and at the beginning of your script, add the path like.
# Your script beginning
PATH="$PATH:/<location>"
Other option, create a folder like say $HOME/bin and copy your script in this location on all your machines or better yet you can add the scripts /usr/bin or /usr/local* folder in all machines
If foo is an alias of the script, try:
n="$(type -f foo)" ; n="${n/*\`}"; n="${n%?}"
dirname "$(realpath "$(which "$n")")"

Running scripts without dot slash (Ubuntu vs Mac) [duplicate]

This question already has answers here:
Why do you need ./ (dot-slash) before executable or script name to run it in bash?
(9 answers)
Closed 2 years ago.
In ubuntu scripts can be executed with following commands:
$ chmod +x manage.py
$ manage.py
However in mac you need to use ./ in order to actually run the script, as follow:
$ chmod +x manage.py
$ ./manage.py
I would like to know what is exactly ./ (especially that both system use bash by default) and if there is a way to run scripts directly in mac?
It's because you (very sensibly) don't have . in your PATH environment variable. If you do, it becomes an attack vector for people to get you to execute their own code instead of real stuff.
For example, let's say your path is:
.:/usr/bin
so that commands will first be searched for in your current directory, then in /usr/bin.
Then another user creates an executable script file ls in their home directory which changes to your home directory and deletes all your files. Then they tell you they've got something interesting in their home directory. You run ls to see what they have, and your files are deleted. All because it ran ls from your current directory first.
This is a particular favorite attack vector against naive system admins.
To be honest, on my home machines, I don't worry too much, since I'm the only user and I'm not prone to downloading stuff I don't trust. So I usually add . to my path for convenience, but usually at the end so it doesn't get in the way of my more regular commands.
When you are executing a command that file (script/binary) needs to be found by the system. That is done by putting directories where to look for scripts into the PATH environment variable. So if it works in ubuntu it means PATH includes '.' (the current directory). If you want the same behavior on mac then put something like export PATH="$PATH:." in your .bashrc (asuming you are using bash..)

Create folder in same directory as shell script [duplicate]

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 8 years ago.
I'm new to Shell Scripting:
I'm trying to create a script that asks for user input as to the folder name, finds the directory the script is being run from, and creates a folder within that same directory with a bunch of files (I can do that part)
My issue is I can't seem to figure out how to make the script find its current directory without explicitly spelling it out.
I want to be able to run it from anywhere and have it create the folder right next to it without the user having to path it from the home folder every time.
Could someone help with this?
You could try this one liner:
DIR="$( cd "$( dirname "$0" )" && pwd )
Will leave you with a $DIR variable that contains the full path to the current directory. See this answer for more information!
script_dir=$(dirname "$0")
$0 is the name of the running script, as entered on the command line (for example: ./bin/script)
If you want the full path:
script_dir=$(cd -P -- "$(dirname "$0")" && pwd -P)

Resources