How to get the absolute path of shell script in linux
In this issue, the editor will bring you the absolute path of how to get the shell script in linux. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Script name: a.sh
Location: / tmp/whuang/study/java
Content of the script:
The code is as follows:
#! / bin/sh
This_dir= `pwd`
Dirname $0 | grep "^ /" > / dev/null
If [$?-eq 0]; then
This_dir= `dirname $0`
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
-
Function: get the absolute path of the script.
Test:
[root@ppc40 java] # pwd
/ tmp/whuang/study/java
[root@ppc40 java] # sh a.sh
/ tmp/whuang/study/java
[root@ppc40 java] # cd..
[root@ppc40 study] # sh java/a.sh
/ tmp/whuang/study/java
[root@ppc40 study] # sh. / java/a.sh
/ tmp/whuang/study/java
The above is the absolute path of how to get the shell script in the linux shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.