How to know the directory of the file containing the running script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to know the directory of the file containing the running script?
# 1  
Old 03-16-2006
How to know the directory of the file containing the running script?

When you run a script, how will you know the directory for the actual file that contains the script. The code should be like .Me in VB. I dont know how we do it in unix. Is it possible?
# 2  
Old 03-16-2006
Maybe this is too simplistic of an answer but wouldn't "pwd" get you what you want?
# 3  
Old 03-16-2006
There are several things you could try.
Usually the special variable $0 contains the name of the process,
or in your case of the shell script.
Chances are that your script was started by specifying the absolute path,
at least this is common for cronjob launched scripts.
Then you could extract the dirname by simply

PATH2SCRIPT=$(dirname $0)

You could also try to retreive it from the proc table like

PATH2SCRIPT=$(dirname $(ps -C name_of_script -o args=|awk '{print$2}'))

If your OS is Linux you could as well look up the command line of your script
in the proc virtual filesystem, remembering that the special shell variable $$
refers to your script's PID, like

PATH2SCRIPT=$(dirname $(cat /proc/$$/cmdline))

But this all seems redundant to me,
or I simply haven't understood your objective.
# 4  
Old 03-16-2006
actually my objective is to have the scripts which are portble. the is usully a case in which you will deploy various scripts to various user. thus your directory will depend on the user.

I will then have a directory which I will call "main". this directory will become portable where the "main script" is located. inside this directory are various subfolders which contains scripts. These scripts will be called by the "main script".
# 5  
Old 03-17-2006
directory of the current file

Use the variable PWD which holds the current directory information of the cd command only in ksh
# 6  
Old 03-17-2006
# 7  
Old 03-20-2006
how to know the directory of the file in the script

Hello, I think you can use the find command in the script to get the path of the file residing



regards,
deepshree
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

2. Shell Programming and Scripting

Running script in crontab in a specific directory

I am trying to run a script from crontab but the entire script (which is 70+ lines) is written in bash and I need it to run from a certain directory. So when I run it manually I cd to /local/mnt/scr and then type ./reordersc and it works fine. However, I want it to run from the crontab and I... (20 Replies)
Discussion started by: newbie2010
20 Replies

3. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

4. Shell Programming and Scripting

Display current directory for a running process for script

I'm trying to create a ksh script to do the following: 1) Ask the user what process they want to search for. 2) Have the script read the input as a variable and use it to search for the process. 3) Display the current time & date, the working directory of the process, and finally display the... (6 Replies)
Discussion started by: seekryts15
6 Replies

5. Shell Programming and Scripting

Running shell script from any directory

Greetings all, We have an older environment that we are trying to bring up to par with our current Production and Test servers. We have a command on our production boxes, UniqueScriptName.sh, and we can issue that command from any directory and it works. On our older environment, our... (1 Reply)
Discussion started by: jeffs42885
1 Replies

6. Shell Programming and Scripting

Script which removes files from the first directory if there is a file in the second directory

Script must removes files from the first directory if there is a file with same name in the second directory Script passed to the two directories, it lies with them in one directory: sh script_name dir1 dir2 This is my version, but it does not work :wall: set - $2/* for i do set -... (6 Replies)
Discussion started by: SLAMUL
6 Replies

7. Shell Programming and Scripting

ksh script not running in another directory on same server

I have a script that runs fine under my home directory. No syntax errors, runs and returns as expected. When I migrate the script to the UAT directories on the same server for User testing, I get a syntax error. I've checked to make sure the .profile I'm using is the same in the UAT... (1 Reply)
Discussion started by: mpflug
1 Replies

8. Shell Programming and Scripting

Rename folder/directory after running sort script

Hello, I'm trying to create a script that sorts documents by author (found in file name) and then create a directory for that author. For example, Input: John - Paper_1.txt John - Paper_2.txt Mark - Paper_1.txt Jill - Paper_1.txt Output: dir/John/Paper_1.txt dir/John/Paper_2.txt... (1 Reply)
Discussion started by: jl487
1 Replies

9. Shell Programming and Scripting

running a looping script for all files in directory

I have three different linux command scripts that I run for 20+ files in one directory. it goes like this FIRST SCRIPT: grep 'something' -w file > newfile1 . . . grep 'something -w file > newfile20 then I take all these 'newfileN' and run this: awk 'BEGIN {... (20 Replies)
Discussion started by: labrazil
20 Replies

10. Solaris

running script in any directory

hi im a cobol programmer but im new in unix. I just want to know how can i run script even if it is in other directory?? Thanks, (1 Reply)
Discussion started by: shinjeck
1 Replies
Login or Register to Ask a Question