how to get the scripts full dir path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get the scripts full dir path
# 1  
Old 08-26-2012
how to get the scripts full dir path

Code:
lyang0@lyang0-OptiPlex-755:~$ ./test.sh 
.
lyang0@lyang0-OptiPlex-755:~$ cat test.sh 
#!/bin/bash
echo `dirname $0`
lyang0@lyang0-OptiPlex-755:~$ pwd
/home/lyang0

it doesn't get "/home/lyang0" and only when run /home/lyang0/test.sh it will get, but how can I do, then it can get the real full path anytime?
# 2  
Old 08-26-2012
Code:
#!/bin/bash

echo "$(readlink -f $0)"

============

./testme 

/tmp/testme

This User Gave Thanks to in2nix4life For This Post:
# 3  
Old 08-26-2012
Try this...

Code:
echo `pwd`

# 4  
Old 08-26-2012
Quote:
Originally Posted by pamu
Code:
echo `pwd`

You assume that the script being executed is in the current working directory which it is likely not. Consider the typical case where the script (foo.sh) resides in /usr/local/bin and the user's current working directory is /tmp/wrk. Assuming that /usr/local/bin is in PATH, and the user types foo.sh at the command line, echo `pwd` returns the wrong answer as foo.sh doesn't reside in the current directory.
# 5  
Old 08-26-2012
Quote:
Originally Posted by in2nix4life
Code:
#!/bin/bash
 
echo "$(readlink -f $0)"
 
============
 
./testme 
 
/tmp/testme


can we get the script's dirctory dir, here is /tmp, and only with one line to get this.
# 6  
Old 08-26-2012
Not a one-line but it uses shell build-in functions
Code:
#!/bin/sh

n="$(readlink -f $0)"
echo "${n%/*}"

exit 0

# 7  
Old 08-26-2012
I'm confused. I see several references to readlink(1) in the responses to this request, but I didn't see any indication that the file being executed is a symbolic link.

In practice I would expect that $0 in a shell script would be set to an absolute pathname of the script if the script was found by invoking an absolute pathname for the script or if the script was found using a directory (with an absolute pathname) found in $PATH.

Otherwise, I would expect that $PWD/$0 would be an absolute pathname of the script if the shell script was invoked by using a relative pathname on the command line or was found using a directory (with a relative pathname) found in $PATH.

So, at least with the Bourne shell, bash, and ksh, the following should give an absolute pathname of the script if the following is included in the script before it makes any changes to its current working directory:
Code:
case "$0" in
(/*)    script_path="$0" ;;
(*)     script_path="$PWD"/"$0" ;;
esac

When this uses relative paths, the resulting $script_path may include dot and dot-dot components and may also contain unresolved symbolic links. But, it should always give you an absolute path to the file containing the executing script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Can I mount partition at given dir path

Hi, In Linux, I had modified fstab file which used to mount ~/Music, ~/Pictures, etc with disk partitions containing corresponding content or binding directory located at other partition. But I am wondering can I do same in El-Capitan as well? No linking! /media/L-Store/Desktop/Documents ... (0 Replies)
Discussion started by: ezee
0 Replies

2. Shell Programming and Scripting

Wild card for dir path

I have dir structure like this : /opt/oracle/product/abc/sqlplus/admin/ /opt/oracle/product/def/sqlplus/admin /opt/oracle/product/ghi/sqlplus/admin I am trying to use wildcard ( for dirs abc,def,ghi) ..something like this : cp xyz.txt ... (1 Reply)
Discussion started by: talashil
1 Replies

3. Shell Programming and Scripting

How to extract strings from full path when full path is not fixed

/Path/snowbird9/nrfCompMgrRave1230100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird6/nrfCompMgrRave1220100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird14/nrfCompMgrRave920100920.log.gz:09/20/2010 06:14:51 ERROR Error Message.... (0 Replies)
Discussion started by: Shirisha
0 Replies

4. Shell Programming and Scripting

Retrieve directory path from full file path through sh

Hi, I have a file abcd.txt which has contents in the form of full path file names i.e. $home> vi abcd.txt /a/b/c/r1.txt /q/w/e/r2.txt /z/x/c/r3.txt Now I want to retrieve only the directory path name for each row i.e /a/b/c/ /q/w/e/ How to get the same through shell script?... (7 Replies)
Discussion started by: royzlife
7 Replies

5. Windows & DOS: Issues & Discussions

DOS Dir - listing of full path and timestamp

Hi, (Apologies, I'm sure I'm not the first person to raise this question but so far in my searches haven't found a good answer). I would like to output a listing per line of filename (including full path) and 'last updated' timestamp. e.g: Z:\dir1\file1.txt 01/02/2010 10:43... (5 Replies)
Discussion started by: GM_AIX
5 Replies

6. Shell Programming and Scripting

Executing all scripts in /DIR except one

First i need to find all scripts directly under /DIR that end with ".sh" extension except "noallow.sh". That can be done with: find /DIR -maxdepth 1 -name "*.sh"|grep -v "noallow.sh" Now i want to run all the files output from the previous command. The following code: for filename in... (6 Replies)
Discussion started by: proactiveaditya
6 Replies

7. Shell Programming and Scripting

How do I define a particular dir in PATH variable and then unset that dir

How do I define a particular dir in front of PATH variable and then run some job and then at the end of job SET the PATH variable to original? in a script, WILL something like this work: ORIG_PATH=$PATH export PATH=/dir1/dir2:$PATH RUN SOME JOBS ..... unset PATH EXPORT... (2 Replies)
Discussion started by: Hangman2
2 Replies

8. Shell Programming and Scripting

full path of a file situated either in parent's dir. or parent's parent dir. so on...

hi experts(novice people can stay away as it is no child's game), i am developing a script which works like recycle bin of windows. the problem i am facing is that when ever i am trying to delete a file which is situated in parent directory or parent's parent directory i am unable to... (1 Reply)
Discussion started by: yahoo!
1 Replies

9. Shell Programming and Scripting

getting full path from relative path

given a relative path, how do i convert it into a full one. i.e. if i am in /home/polypus and i am given foo/bar then to get a full path i can just concatinate it with pwd, but what if i am given "../mama" how do i programmatically convert: /home/polypus and ../mama into ... (4 Replies)
Discussion started by: polypus
4 Replies

10. UNIX for Dummies Questions & Answers

Need to know path of dir currently in.

Which command will help us to get the directory path? (4 Replies)
Discussion started by: videsh77
4 Replies
Login or Register to Ask a Question