Sponsored Content
Top Forums Shell Programming and Scripting Find out directory where command is located Post 302963589 by Don Cragun on Sunday 3rd of January 2016 08:05:09 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 09:05 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy