Bash: Path to symlink which calls this script - bash

I have the following situation:
I have a script with a path of: /usr/local/bin/rsnapshot.period
I want to have symlinks to it in various /etc/cron.[period]/ directories, like /etc/cron.hourly/rsnapshot
I'd like to have the script look up the full path to the symlink, and pull out the [period] part, so I can feed it to rsnapshot.
I can do all the text hacking. The problem I'm having trouble with is getting the path to the calling symlink from within the bash script. $0 seems to point to /usr/local/bin/rsnapshot.period
Is there a better way to get this info?

$0 seems to point to /usr/local/bin/rsnapshot.period
$0 is set by the calling program in its exec*() call, as the first word of the arg argument or the first element of the argv argument. If you feel that the tool you're using is setting this value incorrectly then you should open a bug with the developer.
In the meantime, using a hardlink instead of a symlink will allow you to detect the script name properly, but will break if you aren't careful with the tool you use to edit the main script.

Turns out my problem wasn't that $0 was incorrect - it was pointing to the right place. However, as I was trying to get the absolute path of it, I was using 'realpath' before it, which resolved symlinks.
Passing realpath the '-s' fixed it. Here's my test script and the output of it:
Script:
#!/bin/sh
echo \$0: $0
echo realpath -s \$0: $(realpath -s $0)
echo readlink -e: $(readlink -e $0)
Executed:
$0: ./rsnapshot
realpath -s $0: /etc/cron.hourly/rsnapshot
readlink -e: /usr/local/bin/rsnapshot.period

Related

In bash, what's the best way for a script to reference the path of another script?

scripts/a.sh calls scripts/b.sh through source or through sh.
But I cannot be sure that the working directory will be scripts or the parent of scripts or something else.
What is the best practice for referencing b.sh? I can find the directory of the current script, then cd to that directory, and then simply call ./b.sh. But that seems like a lot of code to put into every script that calls another.
There is no need for a cd, cause source or command take a full path. Just get the dir name of the full path of your script and run the script from there.
From bash manual:
0
($0) Expands to the name of the shell or shell script. ....
From man readlink:
-f, --canonicalize
canonicalize by following every symlink in every component of the given name recursively; ...
From man dirname:
dirname - strip non-directory suffix from file name
Altogether:
. "$(dirname "$(readlink -f "$0")")"/b.sh
I've seen some bash scripts that start with something similar to:
DIR=$(dirname "$(readlink -f "$0")")
cd "$DIR"
So the current working directory in a script stays the same, even if user runs it from another directory.
#edit
Like #GordonDavisson suggested in comments, we can also add your dir to PATH:
export PATH="$(dirname "$(readlink -f "$0")")":"$PATH"
Then running:
. a.sh
will search for a.sh script through inside directories listed in PATH variable, which it will find in the first dir.

Get current directory and concatenate a path

This is a shell script (.sh file). I need to create an absolute path based on the current directory. I know about pwd, but how do I concatenate it with another string? Here is an example of what I am trying to do:
"$pwd/some/path"
Sounds like you want:
path="$(pwd)/some/path"
The $( opens a subshell (and the ) closes it) where the contents are executed as a script so any outputs are put in that location in the string.
More useful often is getting the directory of the script that is running:
dot="$(cd "$(dirname "$0")"; pwd)"
path="$dot/some/path"
That's more useful because it resolves to the same path no matter where you are when you run the script:
> pwd
~
> ./my_project/my_script.sh
~/my_project/some/path
rather than:
> pwd
~
> ./my_project/my_script.sh
~/some/path
> cd my_project
> pwd
~/my_project
> ./my_script.sh
~/my_project/some/path
More complex but if you need the directory of the current script running if it has been executed through a symlink (common when installing scripts through homebrew for example) then you need to parse and follow the symlink:
if [[ "$OSTYPE" == *darwin* ]]; then
READLINK_CMD='greadlink'
else
READLINK_CMD='readlink'
fi
dot="$(cd "$(dirname "$([ -L "$0" ] && $READLINK_CMD -f "$0" || echo "$0")")"; pwd)"
More complex and more requirements for it to work (e.g. having a gnu compatible readlink installed) so I tend not to use it as much. Only when I'm certain I need it, like installing a command through homebrew.
Using the shell builtin pwd in a command substitution ($(...)) is an option, but not necessary, because all POSIX-compatible shells define the special $PWD shell variable that contains the current directory as an absolute path, as mandated by POSIX.
Thus, using $PWD is both simpler and more efficient than $(pwd):
"$PWD/some/path" # alternatively, for visual clarity: "${PWD}/some/path"
However, if you wanted to resolve symlinks in the directory path, you DO need pwd, with its -P option:
"$(pwd -P)/some/path"
Note that POSIX mandates that $PWD contain an absolute pathname with symlinks resolved.
In practice, however, NO major POSIX-like shell (bash, dash, ksh, zsh) does that - they all retain symbolic link components. Thus, the (POSIX-compliant) pwd -P is needed to resolve them.
Note that all said POSIX-like shells implement pwd as a builtin that supports -P.
Michael Allen's helpful answer points out that it's common to want to know the directory of where the running script is located.
The challenge is that the script file itself may be a symlink, so determining the true directory of origin is non-trivial, especially when portability is a must.
This answer (of mine) shows a solution.
wd=`pwd`
new_path="$wd/some/path"
with "dirname $0" you can get dynamin path upto current run scipt.
for example : your file is locateted in shell folder file name is xyz and there are anthor file abc to include in xyz file.
so put in xyz file LIke:
php "`dirname $0`"/abc.php

How to resolve a symbolic link to a parent directory in a shell script?

The following line introduces the local variable PROGUARD_HOME within a shell script:
PROGUARD_HOME=`dirname "$0"`/..
This points to the parent folder of the shell script. The script executes normally. - Then, I created the symlink /usr/local/bin/proguard which refers to ~/bin/proguard4.10/bin/proguard.sh. When I run proguard using the symlink PROGUARD_HOME is no longer resolved correctly. This causes the following error message output by the shell script:
Error: Unable to access jarfile /usr/local/bin/../lib/proguard.jar
How can I rewrite the allocation of the enviroment variable so that it resolves an symbolic link if present?
I am aware of a very similar question on resolving symbolic links in shell scripts but still cannot figure out how to combine those solutions with the parent folder approach here.
I think a readlink -f $0 to reveal the target of the shell script itself, then a dirname to strip off the script, then another readlink -f on the product of that should do the trick:
PROGUARD_HOME=$(readlink -f $(dirname $(readlink -f "$0"))/..)
A more step-by-step breakdown:
echo "\$0 is $0"
TRUENAMEOFSCRIPT=$(readlink -f $0)
echo "readlink -f of \$0 reveals $TRUENAMEOFSCRIPT"
DIRNAMEOFSCRIPT=$(dirname $TRUENAMEOFSCRIPT)
echo "The script lives in directory $DIRNAMEOFSCRIPT"
PARENTDIR=$(readlink -f "$DIRNAMEOFSCRIPT"/..)
echo "Its parent dir is $PARENTDIR"

How do I get the script name being executed in bash?

So I am trying to make a portable bashrc/bash_profile file. I have a single script that I am symbolically linking to .bashrc, .bash_profile, etc. I am then looking at $0 and switching what I do based on which script was called. The problem is what the shell calls the bashrc script of course it executes bash really which means $0 for me is -bash. $1 further more is not set to the script name.
So my question is, in bash how can I get the name of the script being executed. Not the binary executing it, e.g. bash?
I assume its giving me -bash with $1 not being set because it is really not a new process. Any ideas?
Try:
readlink -f ${BASH_SOURCE[0]}
or just:
${BASH_SOURCE[0]}.
Remarks:
$0 only works when user executes "./script.sh"
$BASH_ARGV only works when user executes ". script.sh" or "source script.sh"
${BASH_SOURCE[0]} works on both cases.
readlink -f is useful when symbolic link is used.
The variable BASH_ARGV should work, it appears the script is being sourced
$BASH_ARGV
create .sh file lets say view.sh then put
#!/bin/bash
echo "The script is being executed..."
readlink -f ${BASH_SOURCE[0]}

How to get parent folder of executing script in zsh?

In bash i get the executing script's parent folder name by this line
SCRIPT_PARENT=`readlink -f ${BASH_SOURCE%/*}/..`
Is there any way to achieve this in zsh in a way that works both in zsh and bash?
Assume i have got a file /some/folder/rootfolder/subfolder/script with the contents:
echo `magic-i-am-looking-for`
I want it to behave this way:
$ cd /some/other/folder
$ . /some/folder/rootfolder/subfolder/script
/some/folder/rootfolder
$ . ../../folder/rootfolder/subfolder/script
/some/folder/rootfolder
$ cd /some/folder/rootfolder
$ . subfolder/script
/some/folder/rootfolder
$ cd subfolder
$ . script
/some/folder/rootfolder
This should work in bash and zsh. My first implements this behavior, but does due to $BASH_SOURCE not work in zsh.
So basically its:
Is there a way to emulate $BASH_SOURCE in zsh, that also works in bash?
I now realized that $0 in zsh behaves like $BASH_SOURCE in bash. So using $BASH_SOURCE when available and falling back to $0 solves my problem:
${BASH_SOURCE:-$0}
There is a little zsh edge case left, when sourcing from $PATH like:
zsh> cat ../script
echo \$0: $0
echo \$BASH_SOURCE: $BASH_SOURCE
echo '${BASH_SOURCE:-$0}:' ${BASH_SOURCE:-$0}
zsh> . script
$0: script
$BASH_SOURCE:
${BASH_SOURCE:-$0}: script
bash> . script
$0: bash
$BASH_SOURCE: /home/me/script
${BASH_SOURCE:-$0}: /home/me/script
I could do a which script but this would not play nice with other cases
While it would be easy to do this in zsh, it is just as easy to use pure bash which is able to be evaluated in zsh. If you cannot use any command that may or may not be on your path, then you can only use variable alteration to achieve what you want:
SCRIPT_SOURCE=${0%/*}
This is likely to be a relative path. If you really want the full path then you will have to resort to an external command (you could implement it yourself, but it would be a lot of work to avoid using a very available command):
SCRIPT_SOURCE=$(/bin/readlink -f ${0%/*})
This doesn't depend on your $PATH, it just depends on /bin/readlink being present. Which it almost certainly is.
Now, you wanted this to be a sourced file. This is fine, as you can just export any variable you set, however if you execute the above then $0 will be the location of the sourced file and not the location of the calling script.
This just means you need to set a variable to hold the $0 value which the sourced script knows about. For example:
The script you will source:
echo ${LOCATION%/*}
The script that sources that script:
LOCATION=$0
<source script here>
But given that the ${0%/*} expansion is so compact, you could just use that in place of the script.
Because you were able to run the command from your $PATH I'll do something like that:
SCRIPT_PARENT=$(readlink -f "$(which $0)/..")
Is that your desired output?

Resources