Sourcing the source files using bash script - bash

Usually I source all the macros I have for the jobs run in a remote machine using this command:
macros=$\my_directory
But I see someone uses a different way to get all the macros for submitting the jobs in a remote machine. He uses this command:
macros=$(dirname $(readlink -f $BASH_SOURCE))
Now I want to know how the $dirname has the advantages over giving the specific macro location. It would be great if you just explain to me regarding the sourcing the macro using $dirname

By using dirname you get the directory of where the script is located, therefore it's easy to source other files locally close to your script and don't worry about specifying the correct path each time the script bundle is relocated.
For instance if you have in your script source $macros/some_script.sh then it will not break when the bundle is located in the /usr/local/bin/ or /bin/ or ...
Regarding $BASH_SOURCE see: https://stackoverflow.com/a/35006505/2146346

Related

Running an executable by calling it in a .sh file

I am very new to bash and using .sh files. I am trying to run the program aescrypt by calling it in a .sh file as follows (aescrypt is in the same directory as the .sh file) :
./aescrypt -e -p password file.txt
It throws the following error:
./aescrypt no such file or directory
Am I doing it wrong?
ps- I realy don't want to add it to the PATH variable as I will be using this on more than one computer that resets every day.
The location of the script is irrelevant. The thing that matters is the working directory of the process executing the script. The simplest solution really is to add aescrypt to a standard location like /bin or /usr/bin. If neither of those is acceptable, perhaps /usr/local/bin is an option. Otherwise, just use the full path of aescrypt in your script. Either hard code it, or if it is in the same directory as the script, try:
$(dirname $0)/aescrypt ...
(Note that hardcoding is more reliable, but less flexible. If you move the executable, the script will break. But using dirname will break if the script changes directory during execution.)
how about if you call the program like ./aescrypt.sh, thats the way to call an .sh programm througt the terminal
First off all, you have also to change the permissions of the file to make it executable, the way to make that is to write in the terminal, the command:
sudo chmod 765 aescrypt.sh
For that you have to be located where the file is

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..)

how to make the bash script portable?

I have written a bash script which is dependent on current folder structure,
What should I do to make it runnable in any other folder(become portable)?
let me explain more: I want the script to delete all the files in current directory that the script is running,
or to delete all the files that are in the parent folder,
what is the solution?
cd "$(dirname "$0")"
Add this to the top of your script to have it cd to the directory the script is installed at. Then you can use relative paths and they'll be relative to the script dir and not the user's pwd.
If for whatever reason dirname isn't available, here's an equivalent statement that uses basename instead.
cd "${0%%/$(basename "$0")}"
Don't use absolute paths in your script.
we need more information to give you a proper answer...BUT some tips if you need to make it portable are to store any files that you need on a network share and simply make a new folder off of / such as /temporaryScriptFolder/ and mount the network share to that empty folder, allowing you easy access to any resources that are necessary.

How to make a shell script global?

I am on Mac's OS 10.6, and I am trying to learn a thing or two about shell scripting. I understand how to save a shell script and make it executable, but I am wondering what I can do or where I can save the file to make it global (that is, accessible no matter what folder I am in).
For example, if I save a .sh file in the /Users/username/ directory and make it executable, I can only execute that script in that specific directory. If I navigate to /Users/username/Downloads, for example, I can't execute the script.
Also, any suggestions of resources for learning more about shell scripting would be helpful. Thanks
/usr/local/bin would be the most appropriate location. Mac OS X has it in the PATH by default
There are two ways to do it -
Put your script in usr/local/bin and make sure it is executable(chmod +x my_script)(This is already set in the path, you can check by doing an echo $PATH)
Create a folder in your home directory called bin. (For your personal scripts)
cd ~ (Takes you to your home directory)
mkdir bin (create a bin folder)
vim .bash_profile (to set path environment variable)
export PATH=~/bin:$PATH (Press i then add this line and then do esc and type :wq)
Now you can just type the name of your script and run it from anywhere you want.
** NOTE: If you want to run the script with a shortened command rather than typing your entire filename, add the following to your .bash_profile:
alias myscript='my_script.sh'
Then you can run the script by simply typing myscript. (you can sub in whatever alias you'd like)
Traditionally, such scripts either go in ~/bin (ie: the bin directory in your home directory) or /usr/local/bin/ The former means the script will only work for you, the latter is for scripts you want anybody on the system to be able to run.
If you put it in ~/bin, you may need to add that to your PATH environment variable. /usr/local/bin should already be on the path.
In mac operating system
Open bash ~/.bashrc file.
add path of your script in your bashrc file , using
export PATH="$PATH:/Users/sher.mohammad/Office/practice/practiceShell"
Open your ~./bash_profile file and add [[ -s ~/.bashrc ]] && source ~/.bashrc
open new terminal window
Now whenever you will open your terminal your script will be loaded
This one is super easy if you are familiar with your bashrc file! This will entirely use just your .bashrc file and takes 2 seconds to accomplish.
(I use Arch Linux Manjaro so I use .bashrc located in my home directory)
The code to be placed in your .bashrc file:
# Simple bashrc method to launch anything in terminal from any directory
YOURCOMMAND () {
cd /path/to/directory/containing/your/script/ && ./YOURSCRIPT
}
As you can see, first you use the simple 'cd' command and give it the directory of the scripts location, then use '&&' so that you can make the next command executed right after, and finally open your script just as you would normally! Super easy and saved right in your .bash file! :)
Hope I've helped someone!
Sincerely,
AnonymousX
On using bash shell, write that script as function and then put it to the .bashrc or source the file which containing that function by "source file_name"
Now execute the script by function call in the shell.
Either saving it in /usr/bin (or any other directory present in PATH) or editing PATH to include the directory you saved it in will basically make it run in any directory.
from the working directory of 'script.sh'" mv [script.sh] /usr/local/bin"( not tested but seems to be the least complex way IMO.)
You should put it in the global executable directory on your machine. I think that would usually be /usr/bin on Unix-based operating systems (this would however most often require super user privileges on that machine).
You could also put it in any other directory that is in the $PATH environment variable, although it would only work for those users who have that directory in that variable.
You can find the value of $PATH by typing echo $PATH in a shell. The directories are separated by :.

Where do I put mxmlc so that I could just type 'mxmlc' in the terminal to compile a swf file?

I'm on a Mac and I'm trying to make a Vim plugin for compiling/running actionscript files.
First, I need to run mxmlc on the command line, but to do that I have to keep on typing the path to it. Where do I place it so that I don't have to retype the path?
You need to modify your "$PATH" environment variable, so that the tool is in that directory. However, if you want to make this very easy... you can download my macosx-environment-setup.tar.bz2 program. If you execute the "install.sh" script using "sudo ./install.sh", it will setup your environment in such a way that if you use "/Library/Flex4SDK" as the location for the Flex4SDK, it will automatically find it, define FLEX_HOME to point to that location, and it will also ensure that the binaries from the Flex4SDK are in your PATH.
Side Note: This is up on the web, because I use it in my Development Environment Setup How-To Guides. If you aren't too keen about running "sudo ./install.sh", you need to choose a location (I am going to assume "/Library/Flex4SDK", so that the tools are located in "/Library/Flex4SDK/bin"), and then you would simply need to edit your "~/.profile" file (using "nano ~/.profile"), adding the following to the very end:
export FLEX_HOME=/Library/Flex4SDK
export PATH="$PATH":"$FLEX_HOME/bin"
Note that these changes occur in your shell... they will not affect programs that are launched by double-clicking them in Finder. In order to affect those programs, you will need to place the environment variables in a file named ~/.MacOSX/environment.plist. See Automatically build ~/.MacOSX/environment.plist for a script that will automatically generate such a file using the current environment variables defined in your shell.
There are a few ways to answer this:
In one of your directories searched
by PATH (see the list with echo
$PATH)
Add a new directory to PATH
(e.g. in your ~/.bashrc
export PATH=$PATH:/path/to/bindir)
Add an
alias to your program (e.g. in your
~/.bashrc alias
mxmic=/path/to/mxmic)
(I'm assuming you're using bash shell, which is usually the case you can check with echo $SHELL)

Resources