I wrote a script a while back that would do some simple install procedures in linux for people that don't like the command line, but it seems that they are running the script from a location (such as root). So I have a solution in mind but trying to find out how to reference where the file is located.
Example. I have a script called Install.sh in a folder /root/Server/Scripts/ which references itself by using the following:
SCRIPTSDIR=`pwd`
But I have come into problems with people running this script from root by doing sh Server/Scripts/Install.sh
How could I make SCRIPTS= something that references where the file is located, not where it is being run from?
Thanks, ask if you need more info!
Edit: All answers were good, I meant to put I needed absolute path.
You can get dir with
dirname $0
If your script is called with relative path, dirname will also return relative path. If you want to resolve it for some reason, you can do
readlink -f `dirname $0`
You can use:
SCRIPTDIR=$(dirname $0)
If you need absolute path, then try:
cd $(dirname $0)
SCRIPTDIR=$(pwd)
cd -
您可以添加如下内容:
fullscriptpath=$( dirname $0 )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.