How to handle change in the $PWD value in shell script? - linux

I have a shell script executed by a tool.
When it is executed by a this tool then the value of $PWD is set by that tool.
However when I executed the script manually the value of $PWD the current directory of script.
Now I'm using this $PWD environmental variable to locate different file location in the script.
But when I execute it manually the file path get changed and it give unexpected results.
Any suggestion how can I handle this change in the value of $PWD while executing the script manually or by that tool?

If you need access to the directory where the script itself is located, in bash you can use
script_dir=$(readlink -f ${0%/*})
It takes the relative path to the script ($0), cuts off the script name and transforms it to a full path.

Related

call bash script in script not found, directory is in $PATH

I have added a directory to my $PATH var to access all my bash scripts. The Problem is, wen i call them in another script they are not found. Only if i use the full path to there location they run in the script.
I tried two files to add the $PATH globally on the system /etc/profile.d/custom.sh and .bashrc, on both the same Problem.
All Scripts have a "#!/usr/bin/env bash" at the top.
In there another way, or have i missed something?

How to execute a bash script without calling its path?

I have the file script.sh in my-directory folder.
How to run this script with the command `script' from the terminal with no regards to the location I am in the terminal?
You can do so by exporting the path where your script in the PATH environment variable, so that you don't ever have to worry about what your actual script's location is, i.e. if your script is present under say /path/to/dir, do
export PATH=$PATH:/path/to/dir
so that your script's path gets appended to an already existing set if paths under PATH, also remember if you run the above from the command-line, it is not permanent and gets lost soon after the session is terminated. To make it permanent add the same line in .bashrc (or) .bash_profile, depending upon your environment.
Or creating a symbolic link from /usr/bin that is what you intent to do you can do something like ln -s /full/path/to/myscript.sh /usr/bin/myscript and then run as just myscript directly from command line. You can also confirm if is properly added by checking the script's location by which command,
$ which myscript
/usr/bin/myscript
Say your directory is /home/Cristian/my-directory then you can make that part of PATH environment variable like export PATH=$PATH:/home/Cristian/my-directory and then you will be able to call it by typing script.sh and not script. If you want it to be called as script then you should name it script and rename the extension.
The export command will make the directory in question part of PATH temporarily. To make it permanent you may want it to part of .bashrc or other shell rc file if you are in other shells.

Execute bash and sh file on Mac without writing extension

I have a .sh file and I want to execute it from shell without writing the extension.
What I did:
I created a directory and added it to $PATH
I gave to the file.sh chmod 711
and the file contain #!/bin/sh (I tried also bash).
However when I try to execute myscript without sh I get command not found
while if I try with myscript.sh I get the right result.
How could I do?
I read also: How to run a shell script on a Unix console or Mac terminal? and executing shell script without calling sh implicitly but no solution
Result of ls -l
ls -l /Users/Mitro/scripts
total 8
-rwx--x--x 1 Mitro staff 22 Nov 26 10:25 myscript.sh
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Users/Mitro/scripts
Two problems...
Firstly, file is already an executable program in /usr/bin that tells you the type of a file - i.e. whether it is an image, or a song or a database. Quick example:
file a.png
a.png: PNG image data, 1 x 1, 1-bit colormap, non-interlaced
So, file is a bad name for a shell script - likewise is test.
Secondly, if you want to execute a script or program in your current directory, also known as dot (.), you either need to have dot in your PATH, or you need to explicitly tell your shell that the file you want to run is in the current directory. The easier option is the second, which means if your script is called fred, you run it with
./fred
which tells the shell it is in your current directory.
The longer option, if you want to always be able to run scripts in the current directory, is to add dot to your PATH. So, you locate your login script (probably $HOME/.profile) and you find the line that sets your PATH and you add the current directory to it.
export PATH=$PATH:.
Once you have set that, you are best off logging out and back in to have it take effect.
Some folks disapprove of the idea of adding dot to their PATH - I don't. YMMV.
You can add alias. If you have /some/path/to/script.py, do:
alias my_script='/some/path/to/my_script.py'
Now when you enter my_script, your script would be executed.
For mac the profile file is ~/.bash_profile as opposed to ~/.profile which did not work.

Include binary from relative path in bash script

I have a project/bin/ directory which is not included in the PATH. It contains
the binaries project/bin/one and project/bin/two.
A bash script is located in project/shell/script1/run.sh. Now, I want to use the binary from the project/bin folder in the project/shell/script1/run.sh script. I would like to have a global script which can be sourced, and include those binaries automatically.
So, I created a script project/bin/load_bin:
#!/usr/bin/env bash
$local_bin_one='./one'
$local_bin_two='./two'
In my project/shell/script1/run.sh I execute the script:
#!/usr/bin/env bash
source '../../bin/load_bin'
However, when I try to use $local_bin_one I get a file not found error. This is because the $local_bin_one points to ./one instead of ../../bin/one.
Question: How can I find the path of the script that is calling source '../../bin/load_bin' from the project/bin/load_bin script itself?
You can get the path of a Bash script this way:
${0%/*}
That is, take $0 (argv[0], the full path and filename the script was invoked with), and strip off the last slash and what comes after that (equivalent to $(dirname "$0")).
You can then do this:
mydir=${0%/*}
source "$mydir/../../bin/load_bin"
Or you can even do this:
cd ${0%/*}
source '../../bin/load_bin'

How to set PATH envirnment variable through a shell script though that shell script gets terminated?

I am having a shell script ./my_shellscript.sh, its contents are as follows :
source /path/to/shell_script.sh
Where shell_script.sh contains:
export PATH=/path/to/a/dir:$PATH
which command_name
when I execute the my_shellscript.sh then it shows accurate path to a command, that I executed using "which" command?
When the shell_script.sh terminates and when I again do "which command_name" from command line it doesn't shows any path as it shown when I executed the scripts.
My question is that how to set/persist that path to environment variable [PATH:$PATH] though shell_script.sh terminates?
That's not how environments work, you can't change the parent environment. You can only change your environment, and (optionally) that of child processes to your process.
You could run your ./my_shellscript.sh with source (or .) to export it's variables to your current environment.
source my_shellscript.sh
or
. my_shellscript.sh
Other option to put the PATH variable extension into your .profile file in your home directory. (/home/your_username/.profile) That will be permanent.

Resources