| The code is as follows |
Copy Code |
basedirforscriptself=$ (CD "$ (dirname" $) "; pwd) echo "full path to currently executed script is: ${basedirforscriptself}" Or fullpath=$ (CD "$ (dirname" $) "; pwd) echo "full path to currently executed script is: ${fullpath}" Basedir= ' DirName $FULLPATH ' echo "Parent dir is $BASEDIR"
|
This feature is very powerful to directly get the absolute path to the script entire directory and the superior directory all Novo Good Things
Method Two
| The code is as follows |
Copy Code |
#!/bin/sh Name=$0 echo "Name: $name" dir=$ (dirname ${name}) echo "dir: $dir" path=$ (PWD) echo "Path: $path" Path=${dir/./${path}} echo "Path: $path" |
Instance
Shell Code
| The code is as follows |
Copy Code |
#!/bin/sh This_dir= ' pwd ' DirName $0|grep "^/" >/dev/null If [$?-eq 0];then This_dir= ' DirName $ ' Else DirName $0|grep "^." >/dev/null Retval=$? If [$retval-eq 0];then This_dir= ' dirname $0|sed ' s#^.# $this _dir# ' Else This_dir= ' dirname $0|sed ' s#^# $this _dir/# ' Fi Fi Echo $this _dir |
This method can be problematic when the path contains a decimal point. Correction method for adding a judgment
| The code is as follows |
Copy Code |
#!/bin/sh path=$ (DirName $) if [${path:0:1} = =.] Then path=${path/./$ (PWD)} Fi |