Find out directory where command is located


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find out directory where command is located
# 1  
Old 01-03-2016
Find out directory where command is located

so i have a script that i do not want copies of that script to be roaming around. i want that script to be in only one location on the filesystem, and whoever wants to use it should just link to it.

any idea on how to exit from a script if it is detected that the running version is a copy and not a link to the original??

my attempt so far:

Code:
#!/bin/sh
expectedDIR=/var/home/mon
actualdir=$(echo $0 | awk -F"/" '{print $1}')
if [ "${expectedDIR}" != "${actualdir}" ] ; then
echo "Running copies of this script is not allowed. please link to the original instead of copying!"
exit 2
fi

# 2  
Old 01-03-2016
You could try something like this, using pwd and ls -l to determine the absolute pathname:

Code:
file=$(cd -P -- "${0%/*}" 2>/dev/null; pwd -P)/${0##*/}  # get the physical path name of the file that is being specified
while :                                                  # iterate through symbolic links to arrive at the actual directory
do
  ls=$(/bin/ls -l "$file") 
  case $ls in
    l*) file=${file%/*}/${ls##*-> } ;;                   # if the file is a symbolic link then follow it, by replacing
    *)  break ;;                                         # the file name with what the link points to, if not then break the loop
  esac
done
actualdir=${file%/*}


--
You can never force this of course, since one could copy the script and make an adjustment of the variable, but it may help to make users aware....

--
On Linux one could also use readlink, but this is a more general approach, which should also work on most Unix platforms...

Last edited by Scrutinizer; 01-03-2016 at 04:16 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 01-03-2016
Hi,
Another way would check inode file, examples:
my file:
Code:
$ ls -l aa1.txt 
-rw-rw-r-- 2 disedorgue disedorgue 66 janv.  3 21:21 aa1.txt

Hard link of my file:
Code:
$ ls -l aa2.txt 
-rw-rw-r-- 2 disedorgue disedorgue 66 janv.  3 21:21 aa2.txt

Symbolic link of my file:
Code:
$ ls -l aa3.txt 
lrwxrwxrwx 1 disedorgue disedorgue 7 janv.  3 21:37 aa3.txt -> aa1.txt

A copy of my file:
Code:
$ ls -l aa4.txt 
-rw-rw-r-- 1 disedorgue disedorgue 66 janv.  3 21:50 aa4.txt

inode's files:
Code:
$ ls -1i aa*.txt
956473 aa1.txt
956473 aa2.txt
941974 aa3.txt
942170 aa4.txt

here, inode of aa3.txt and aa4.txt are differents, but aa3.txt is a symbolic link, so with ls option "-L":
Code:
$ ls -L1i aa*.txt
956473 aa1.txt
956473 aa2.txt
956473 aa3.txt
942170 aa4.txt

Now, just aa4.txt is different, but this is normal because it's a copy...

Regards.
# 4  
Old 01-03-2016
Quote:
Originally Posted by Scrutinizer
You could try something like this, using pwd and ls -l to determine the absolute pathname:

Code:
file=$(cd -P -- "${0%/*}" 2>/dev/null; pwd -P)/${0##*/}  # get the physical path name of the file that is being specified
while :                                                  # iterate through symbolic links to arrive at the actual directory
do
  ls=$(/bin/ls -l "$file") 
  case $ls in
    l*) file=${file%/*}/${ls##*-> } ;;                   # if the file is a symbolic link then follow it, by replacing
    *)  break ;;                                         # the file name with what the link points to, if not then break the loop
  esac
done
actualdir=${file%/*}

--
You can never force this of course, since one could copy the script and make an adjustment of the variable, but it may help to make users aware....

--
On Linux one could also use readlink, but this is a more general approach, which should also work on most Unix platforms...
Code:
cd -P

is limited to bash and ksh. The OP's script is a sh script:

Code:
#!/bin/sh
expectedDIR=/var/home/mon
actualdir=$(echo $0 | awk -F"/" '{print $1}')
if [ "${expectedDIR}" != "${actualdir}" ] ; then
echo "Running copies of this script is not allowed. please link to the original instead of copying!"
exit 2
fi

Yes. Linux conflates bash and sh. That doesn't make such usage portable.
# 5  
Old 01-03-2016
Quote:
Originally Posted by achenle
Code:
cd -P

is limited to bash and ksh. The OP's script is a sh script:

Code:
#!/bin/sh
expectedDIR=/var/home/mon
actualdir=$(echo $0 | awk -F"/" '{print $1}')
if [ "${expectedDIR}" != "${actualdir}" ] ; then
echo "Running copies of this script is not allowed. please link to the original instead of copying!"
exit 2
fi

Yes. Linux conflates bash and sh. That doesn't make such usage portable.
Let us be very clear here. When doing tests like this with a shell script, sometimes you have to know what shell you're using. Claiming that cd -P can't be used in /bin/sh might or might not be true. If you're using a Linux system where /bin/sh is a link to bash, you can use cd -P. If you're using a Solaris system where /bin/sh is a legacy Bourne shell, you can't use cd -P, but you can't use $(command) either (you'd have to use `command` instead).

Furthermore, the awk script shown in the code above is going to return an empty string any time $0 expands to an absolute pathname for the script being executed and . otherwise. That is the basis of the problem that kept the script from doing what the submitter intended to do. But..., as has been said before, I'm not sure that I understand this thread. If someone copies a script and slightly modifies it for some reason (any reason), modifying the original script to see if it has been moved doesn't help. Anybody that can copy and modify the script can obviously and easily also remove any code that verifies that the script hasn't been moved or modified.
# 6  
Old 01-04-2016
Quote:
Originally Posted by achenle
Code:
cd -P

is limited to bash and ksh. The OP's script is a sh script:

Code:
#!/bin/sh
expectedDIR=/var/home/mon
actualdir=$(echo $0 | awk -F"/" '{print $1}')
if [ "${expectedDIR}" != "${actualdir}" ] ; then
echo "Running copies of this script is not allowed. please link to the original instead of copying!"
exit 2
fi

Yes. Linux conflates bash and sh. That doesn't make such usage portable.
This is not correct. cd -P is part of the POSIX standard.
Code:
SYNOPSIS

cd [-L|-P] [directory]

cd -

cd: Synopsis.


It also works with ksh88, so the only (Bourne family) shell it will not work with is the classic Bourne shell...

So cd -P is in fact very portable...

Also, as Don Cragun noted, the OP used $( ... ) so it is clear that the OP is not using classic Bourne shell and that #!/bin/sh points to a POSIX type shell.

----

Quote:
Originally Posted by Don Cragun
[..]But..., as has been said before, I'm not sure that I understand this thread. If someone copies a script and slightly modifies it for some reason (any reason), modifying the original script to see if it has been moved doesn't help. Anybody that can copy and modify the script can obviously and easily also remove any code that verifies that the script hasn't been moved or modified.
I agree as I noted in post #1. So if someone wants to circumvent this, it is very easy. The reason however, as I can see it, might be that you want to avoid a sprawl of the same script in a Company situation and to make users of the script aware that they should use the centrally maintained version by either using it directly or linking to it. If they want to bypass that, fine but then they should not complain if things break in the future..

Last edited by Scrutinizer; 01-04-2016 at 12:23 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How-To Exclude Directory in find command

How can i tweak the below find command to exclude directory/s -> "/tmp/logs" find . -type f \( ! -name "*.log*" ! -name "*.jar*" \) -printNote: -path option/argument does not work with the version of find that i have. bash-3.2$ uname -a SunOS mymac 5.10 Generic_150400-26 sun4v sparc sun4v (7 Replies)
Discussion started by: mohtashims
7 Replies

2. UNIX for Advanced & Expert Users

Linux command to show where bin files are located

I was having trouble remembering the linux command to show where bin files are located. I eventually figured it out from googling that it was "which". How to find command location in Linux? Since I saw a few other interesting commands like whereis and type it got me curious. Are there any... (5 Replies)
Discussion started by: cokedude
5 Replies

3. Shell Programming and Scripting

Find Command Include Sub Directory

This script writes the output files to FILES but I don't want to exclude all directories from ABC_CHQ and LYS_ADV, I want to include one sub directory name process which is under ABC_CHQ and LYS_ADV in the search. Right now its excluding everything from prune directories such as ABC_CHQ, LYS_ADV... (10 Replies)
Discussion started by: John William
10 Replies

4. Shell Programming and Scripting

Find command with ignore directory

Dear All, I am using find command find /my_rep/*/RKYPROOF/*/*/WDM/HOME_INT/PWD_DATA -name rk*myguidelines*.pdf -print The problem i am facing here is find /my_rep/*/ the directory after my_rep could be mice001, mice002 and mice001_PO, mice002_PO i want to ignore mice***_PO directory... (3 Replies)
Discussion started by: yadavricky
3 Replies

5. Shell Programming and Scripting

find command with wildcard directory

I want to look if there is any file inside a specific directory which was modified before 2 days. I wrote the find command, but the problem is there is one directory and that is a random directory generated by unix, so not sure on how to code for that on the find command. find... (5 Replies)
Discussion started by: srini0603
5 Replies

6. UNIX for Dummies Questions & Answers

find command to look for current directory only

i have this find command on my script as: for i in `find $vdir -name "$vfile" -mtime +$pday` the problem with this code is that the sub-directories are included on the search. how do i restrict the search to confine only on the current directory and ignore the sub-directories. please advise.... (7 Replies)
Discussion started by: wtolentino
7 Replies

7. UNIX for Dummies Questions & Answers

Command to find all soft links in a directory

I need the command to find all soft links in a directory. Can someone please help. Thank you. (2 Replies)
Discussion started by: jgeo01
2 Replies

8. Shell Programming and Scripting

Find command, -name by directory and subdirectory?

Hi All, I'm trying to use the find command to return matches for a directory and file. For example, given the following directories: /one/two/three/file1.txt /one/three/two/file1.txt /one/four/two/three/file1.txt I'm expecting the following to be returned: ... (16 Replies)
Discussion started by: makodarear
16 Replies

9. UNIX for Dummies Questions & Answers

how to find a file named vijay in a directory using find command

I need to find whether there is a file named vijay is there or not in folder named "opt" .I tried "ls *|grep vijay" but it showed permission problem. so i need to use find command (6 Replies)
Discussion started by: amirthraj_12
6 Replies

10. UNIX for Dummies Questions & Answers

using find command only in current directory

I am trying to use the find command to find files in the current directory that meet a certain date criteria. find . -type -f -mtime +2 However, the above also checks the directories below. I tried -prune, but that seems to ignore this directory completely. I read about using -path w/... (5 Replies)
Discussion started by: jliebling
5 Replies
Login or Register to Ask a Question