Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Friday, June 1, 2012
unix shell script find out which directory the script file resides?
Basically I need to run the script with paths related to the shell script file location, how can I change the current directory to the same directory as where the script file resides?
Have a look at http://fritzthomas.com/open-source/linux/384-how-to-get-the-absolute-path-within-the-running-bash-script
The original post contains the solution (ignore the responses, they don't add anything useful). The interesting work is done by the mentioned unix command "readlink" with option -f. Works when the script is called by an absolute as well as by a relative path.
Here a slightly modified copy of the solution in case the other post vanishes....
This is for bash, sh, ksh:
#!/bin/bash # Absolute path to this script, e.g. /home/user/bin/foo.sh SCRIPT=`readlink -f $0` # Absolute path this script is in, thus /home/user/bin SCRIPTPATH=`dirname $SCRIPT` echo $SCRIPTPATH
Almost the same for tcsh, csh:
#!/bin/tcsh # Absolute path to this script, e.g. /home/user/bin/foo.csh set SCRIPT=`readlink -f $0` # Absolute path this script is in, thus /home/user/bin set SCRIPTPATH=`dirname $SCRIPT` echo $SCRIPTPATH
This script, when ran, should print the directory that you're in, and then the directory the script is in, for example, when calling it from / (the script is in /home/mez/), it outputs
/ /home/mez
Remember, when assigning variables from the output of a command, wrap the command in $( and ) - or you'll not get the desired output. `
I'm not sure how to do it, but neither of the previous answers work. This is because $0 is the command as called. If you call the script foo like this './foo', then $0 = ./foo, not /path/to/foo like you want.
that should do the trick -- it might look ugly depending on how it was invoked and the cwd but should get you where you need to go (or you can tweak the string if you care how it looks)
In bash you should get what you need like this:
ReplyDelete#!/bin/bash
BASEDIR=$(dirname $0)
echo $BASEDIR
Have a look at http://fritzthomas.com/open-source/linux/384-how-to-get-the-absolute-path-within-the-running-bash-script
ReplyDeleteThe original post contains the solution (ignore the responses, they don't add anything useful). The interesting work is done by the mentioned unix command "readlink" with option -f. Works when the script is called by an absolute as well as by a relative path.
Here a slightly modified copy of the solution in case the other post vanishes....
This is for bash, sh, ksh:
#!/bin/bash
# Absolute path to this script, e.g. /home/user/bin/foo.sh
SCRIPT=`readlink -f $0`
# Absolute path this script is in, thus /home/user/bin
SCRIPTPATH=`dirname $SCRIPT`
echo $SCRIPTPATH
Almost the same for tcsh, csh:
#!/bin/tcsh
# Absolute path to this script, e.g. /home/user/bin/foo.csh
set SCRIPT=`readlink -f $0`
# Absolute path this script is in, thus /home/user/bin
set SCRIPTPATH=`dirname $SCRIPT`
echo $SCRIPTPATH
Assuming you're using bash
ReplyDelete#!/bin/bash
current_dir=$(pwd)
script_dir=$(dirname $0)
echo $current_dir
echo $script_dir
This script, when ran, should print the directory that you're in, and then the directory the script is in, for example, when calling it from / (the script is in /home/mez/), it outputs
/
/home/mez
Remember, when assigning variables from the output of a command, wrap the command in $( and ) - or you'll not get the desired output.
`
If you're using bash....
ReplyDelete#!/bin/bash
pushd $(dirname "${0}") > /dev/null
basedir=$(pwd -L)
# Use "pwd -P" for the path without links. man bash for more info.
popd > /dev/null
echo "${basedir}"
cd $(dirname $(readlink -f $0))
ReplyDeleteAs theMarko suggests:
ReplyDeleteBASEDIR=$(dirname $0)
echo $BASEDIR
This works unless you execute the script from the same directory where the script resides, in which case you get a value of '.'
To get around that issue use:
current_dir=$(pwd)
script_dir=$(dirname $0)
if [ $script_dir = '.' ]
then
script_dir="$current_dir"
fi
You can now use the variable current_dir throughout your script to refer to the script directory. However this may still have the symlink issue.
I'm not sure how to do it, but neither of the previous answers work. This is because $0 is the command as called. If you call the script foo like this './foo', then $0 = ./foo, not /path/to/foo like you want.
ReplyDeleteecho \pwd\/\dirname $0\
ReplyDeletethat should do the trick -- it might look ugly depending on how it was invoked and the cwd but should get you where you need to go (or you can tweak the string if you care how it looks)
echo \pwd\/\dirname $0\ would NOT work in case that the script would be called with absolute path
ReplyDeleteThis checks if directory exists and is writable
ReplyDeleteif [ -d "$Directory" -a -w "$Directory" ]
then
#Statements
fi
-Muralikrishna.B
lol, you guys are way over-thinking this.
ReplyDelete"$_/../"
that will return the script directory :)