getting a shell script to know it's path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getting a shell script to know it's path
# 1  
Old 06-05-2004
getting a shell script to know it's path

Is it possible in Bash (or any other shell) to get a shell script to know it's own path without having to be part of $PATH or anything like that.

I need this cos i want the script to be able to rename the directory in which it resides.

is this possible?
# 2  
Old 06-05-2004
use the 'pwd' command.

you could save the result of the pwd command in a variable, and then have awk or something else take away the last part of that string. so you would get
/some/directory/to_be_renamed

then have it chop off the to_be_renamed part and save that in another variable.

then you would have it do
mv $var_1 $var_2"new_name"

or something. check the syntax cause mine probably isnt correct.
# 3  
Old 06-05-2004
unfortunately using pwd doesn't work unless i happen to be executing the command from the directory in which it resides. :-(

I made a test script

#! /bin/bash
echo `pwd` > ~/foo
exit 0

and found it i execute it from a shell it gives the pwd from the shell i am in and if i execute it outside of a shell (the way it needs to be execute in the way i want it to work) then it gives "/"
# 4  
Old 06-05-2004
hmmm... i would think that the path from where you are executing the script from would be stored in some variable, but i dont know which one or if that is even the case.

anyone else know?
# 5  
Old 06-05-2004
This is a very hard problem. The script may have been launched via a relatively non-standard exec system call. If that is not the case, $0 may be an absolute or relative path or an item that can be found by searching PATH. But with a non-standard exec, $0 may not be useful .

Another wrinkle involves the fd filesystem that some versions of unix have. These versions, and Solaris is one, allow suid shell scripts, but use an fd to prevent the shell from opening the script directly. $0 will be useless in this case.

One technique that might work is obtaining the pid of the shell running the script and using lsof on that. But even this assumes that the script was exec'ed one way or another. If you something odd like:
cat /path/to/script | ksh
the shell won't have the script open so finding the script becomes much tougher.
# 6  
Old 06-06-2004
$0 worked :-)
thank you very much

I wanted the file to be executed as the app/contents/MacOS/executable file of a cocoa bundle (if that means anything too you.)


"i would think that the path from where you are executing the script from would be stored in some variable, but i dont know which one or if that is even the case. "

I believe pwd operated as an alias to "echo $PWD"
# 7  
Old 06-06-2004
The working directory of programs launched by the Finder is always "/".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to change the path location within the shell script?

Hi ALL, I am trying to find the installed tomcat version and location of the server.xml file to get the tomcat port number. Using below script to do that. #!/usr/bin/env bash var1=$(find / -name "version.sh" ! -size 0 2>&1 |egrep -v "tmp|docker") for loc1 in $var1 do ... (1 Reply)
Discussion started by: sravani25
1 Replies

2. UNIX for Dummies Questions & Answers

Locating shell script files with the $PATH variable

I've created a test script, which is located in $HOME/bin. The script runs as expected with no issues. However, upon echo'ing the $path variable the location of my script is not located in any of the directories listed in $path. So my question is, how does shell know where the script is located... (2 Replies)
Discussion started by: BrandonD
2 Replies

3. Shell Programming and Scripting

Setting the path permanently using shell script

I'm trying to set the path permanently through a shell script. (/opt/quest/bin:/usr/bin/lab to /.profile.) I tired using echo option like below but it doesn't work. Please suggest me the right way to do so. echo "PATH=$PATH:/opt/quest/bin:/usr/bin/lab" >> /.profile (6 Replies)
Discussion started by: pjeedu2247
6 Replies

4. Shell Programming and Scripting

Shell FTP script to send all files to different path

Hello to all in forum I hope you can help me. I want to send via FTP to a linux server the 2 kind of files that are in the same folder, as follow: 1- Send all files that are named verified.SomeString.zz.pttp to path /var/verified 2- Send all files where the name begins with verified.conf... (3 Replies)
Discussion started by: Ophiuchus
3 Replies

5. Shell Programming and Scripting

A shell script for create a a file in the respective path

Hello forum members, I have to create a out file in the current path./aaa/bbb/ccc/hhh. i am writing script below. ###script Begins##### #!/bin/ksh echo "Weclome" if then echo "Hello" rm -rf $aaa/bbb/ccc/hhh #clean the exsisting o/p file echo "no... (2 Replies)
Discussion started by: rajkumar_g
2 Replies

6. Shell Programming and Scripting

Shell script to loop for a particular file in predefined path

Hi , i need a shell script to poll a particular file in a predefined directory , once that file is found , i need to launch another shell script . Eg i need to poll for A.txt in /home/username/Desktop once A.txt is created by Desktop(Created by another Script) , i need to launch another... (2 Replies)
Discussion started by: Vinod H C
2 Replies

7. Shell Programming and Scripting

How to set PATH using shell script [resolved]

Hi, Can anyone help me on how to set PATH using shell scripting.. Please find the shell script code here.... #!/bin/bash PATH = $PATH:/opt/app/oracle/product/10.2.0/bin export PATH echo $PATH exit When i execute this script i get the following error ./backup.sh: line 2: PATH:... (0 Replies)
Discussion started by: srinivasj
0 Replies

8. Shell Programming and Scripting

want only file names (not whole path) in shell script

hi i wrote following script, #!/usr/bin/sh for index in `ls /tmp/common/*.txt` do echo "$index" done here index is giving full path but in my program i want only file names (not along with whole path) Eg. if in /tmp/common files are a.txt and b.txt den out should be a.txt b.txt ... (6 Replies)
Discussion started by: crackthehit007
6 Replies

9. UNIX for Dummies Questions & Answers

Maximum length of a path given as an argument to a shell script

hi, I am using solaris10. I have to write a bourne shell script, which copies files for the said destination path which is passed as an argument to the script. it looks like this myscript.sh /var/test -->destination path now i would like to know what is the maximum length i can... (3 Replies)
Discussion started by: raghu.amilineni
3 Replies

10. UNIX for Dummies Questions & Answers

How to check path exist or not in UNIX shell script

UNIX Shell Script I'm in /home/suneel dirctory in that directory need to check for a path for example com/src/resources If Path exists need to copy the files from one directory If path not exist need to create the folders and copy the files UNIX shell script help required (3 Replies)
Discussion started by: suneelc
3 Replies
Login or Register to Ask a Question