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
# 8  
Old 08-27-2012
Found one interesting thing... Please check...

Code:
#Script - 

$ cat Test_27_8.sh
echo "pwd-`pwd`"
echo "dir--`dirname $0`"  
echo "$(readlink -f $0)"
cd jobs/QA

echo "pwd-`pwd`"
echo "dir--`dirname $0`"
echo "$(readlink -f $0)"
cd QA

echo "pwd-`pwd`"
echo "dir--`dirname $0`"
echo "$(readlink -f $0)"


$ sh Test_27_8.sh
pwd-jobs
dir--.
jobs/Test_27_8.sh
pwd-jobs/QA
dir--.
jobs/QA/Test_27_8.sh
jobs/QA/QA
dir--.
jobs/QA/QA/Test_27_8.sh


#Run with the full path - Here we get the correct path....:)
sh /jobs/Test_27_8.sh
pwd-/jobs
dir--/jobs
/jobs/Test_27_8.sh
pwd-/jobs/QA
dre--/jobs
/jobs/Test_27_8.sh
pwd-/jobs/QA/QA
dir--/jobs
/jobs/Test_27_8.sh

@JC_1 - If you are using fix path for the script that you want it to use you can simply save it in a variable...

Last edited by pamu; 08-27-2012 at 06:41 AM.. Reason: added one more case...
# 9  
Old 08-27-2012
I still don't understand how readlink -f $0 is supposed to produce a full (or absolute) pathname for a shell script that was invoked as ./script or as bin/script. First, the readlink utility on openBSD and OS X systems doesn't have a -f option. Second, the man pages for systems I've seen that have a man page for readlink(1) that do have a -f option don't say anything about being able to turn a relative pathname into an absolute pathname (unless there is a symbolic link in the path being processed that expands to an absolute pathname). And, third, many systems (including Solaris 10 and 11) don't have a readlink utility at all.
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