Visit Our UNIX and Linux User Community


test for directory being subdir of another directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting test for directory being subdir of another directory
# 1  
Old 12-04-2007
test for directory being subdir of another directory

I've been using the following code to make sure a shell script only runs under a "safe" directory.
Comments/Improvements?
Code:
#!/bin/sh
#----------------------------------------------------------------------------#
#...[top].[ subdir.sh ]......................................................#
#----------------------------------------------------------------------------#
#.........william.o.yates...hackware.at.tru2life.net...tru2life.info.........#
#----------------------------------------------------------------------------#
if [ X"${2}" = "X" ];
then
  echo -e "\a.\n..\n...\n...subdir.sh parent subdir\n...\n..\n.";
else
  dir=${1};
  dir_size=${#1};
  sub=${2};  # oops...    2, not 1...
  sub_size=${#2}; # oops...   2, not 1...
  #---------------------------------------------------------------------------
  # if sub_size is less than dir_size, it can't be under parent.
  #---------------------------------------------------------------------------
  if [ ${sub_size} -lt ${dir_size} ];
  then
    echo -e ".\n..\n...\n... sub_size -lt dir_size\n...\n..\.";
  fi
  #---------------------------------------------------------------------------
  # substring sub to dir_size to then compare sub and dir
  #---------------------------------------------------------------------------
  xdir="$(echo ${sub} | head -c ${dir_size})";
  if [ ${dir} = ${xdir} ];
  then
    echo -e ".\n..\n...\n... ${sub} IS a subdir of ${dir}\n...\n..\n.";
  else
    echo -e ".\n..\n...\n... ${sub} is NOT a subdir of ${dir}\n...\n..\n.";
  fi
fi
#----------------------------------------------------------------------------#
#...[end].[ subdir.sh ]......................................................#
#----------------------------------------------------------------------------#


Last edited by hackware; 12-05-2007 at 12:12 AM.. Reason: # oops... 2 not 1...
# 2  
Old 12-04-2007
I'd suggest using grep instead:
Code:
if echo "$1" | egrep "^$2" then ; echo "ok" ; else ; echo "not ok" ; fi

# 3  
Old 12-04-2007
so ^$2 is a SUBDIRECTORY of $1...?
# 4  
Old 12-04-2007
(a) does not work with symbolic links,

(b) or with a partial relative path for one, and full path for other.
# 5  
Old 12-04-2007
Quote:
Originally Posted by Smiling Dragon
I'd suggest using grep instead:
Code:
if echo "$1" | egrep "^$2" then ; echo "ok" ; else ; echo "not ok" ; fi

this is a bit restrictive.
how 'bout:
Code:
if echo "/tmp/foo/bar/fred" | egrep "/fred[/]*"; then  echo "ok" ; else  echo "not ok" ; fi

if echo "/tmp/foo/bar/fred" | egrep "/foo[/]*"; then  echo "ok" ; else  echo "not ok" ; fi

if echo "/tmp/foo/barbaz/fred" | egrep "/bar[/]*"; then  echo "ok" ; else  echo "not ok" ; fi

# 6  
Old 12-04-2007
Not sure what you're getting at there v'ger.
I'm pretty sure the OP is looking for subdirs - ie subtrings that both start at character 0 (thus the ^ symbol).

Adding the [/]* to the end of the subdir search is a good idea though. Possibly enforcing it would be better still to prevent the issue you showed (/barbaz/ vs /bar/).
# 7  
Old 12-04-2007
Quote:
Originally Posted by Smiling Dragon
Not sure what you're getting at there v'ger.
I'm pretty sure the OP is looking for subdirs - ie subtrings that both start at character 0 (thus the ^ symbol).
Hmmm....... not sure if that was the OP's intent, but I've been known to wrong before.
Quote:
Originally Posted by Smiling Dragon
Adding the [/]* to the end of the subdir search is a good idea though. Possibly enforcing it would be better still to prevent the issue you showed (/barbaz/ vs /bar/).

Previous Thread | Next Thread
Test Your Knowledge in Computers #362
Difficulty: Medium
The sed command first appears in Version 5 AT&T UNIX.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extract directory name from the full directory path in UNIX using shell scripting

My input is as below : /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/loyal/IFIND.HELLO.WROC.txt /splunk/scrubbed/triumph/ifind.triumph.txt From the above input I want to extract the file names only . Basically I want to... (5 Replies)
Discussion started by: IshuGupta
5 Replies

2. Shell Programming and Scripting

Test if a script can cd into a directory

Is there a way for a bash script to test if it can cd into a directory without actually attempting to cd into it? I am looking for something along the lines of: if ;then #code to execute fi except in this case I don't want to test if the directory exists; what I want is to test... (8 Replies)
Discussion started by: dexdex200
8 Replies

3. Shell Programming and Scripting

FIle (directory) test operator (bash)

I'm almost pulling out my hair trying to figure out what's wrong with this... there's no reason I can see that it shouldn't be working. It seems that the code acts as though the conditional statement is true no matter what - I've even tried removing the negation operator, but it always goes into... (5 Replies)
Discussion started by: wildbluefaerie
5 Replies

4. UNIX for Dummies Questions & Answers

rm: Unable to remove directory /mnt/users/test/logs/: File exists

rm: Unable to remove directory /mnt/users/test/logs/: File exists ls -latr total 191208 drwxrwxrwx 6 test echo 4096 Jul 3 22:36 .. -rwxrwxrwx 1 test echo 97692804 Jul 3 22:36 .nfsDFA4 drwxrwxr-x 2 test echo 4096 Jul 3 23:00 . M not able to delete... (4 Replies)
Discussion started by: solitare123
4 Replies

5. Shell Programming and Scripting

Problem when test to see if directory exists

Hi, I'm writing a shell script that will create a folder if it does not exist yet. Here's the script: (this if statement is inside a while loop) folderName="Pics" if ! test -d folderName then mkdir $folderName fi However, after the folder Pics has been created, every time the... (3 Replies)
Discussion started by: trivektor
3 Replies

6. Solaris

test and .test in same directory

i am using solaris 5.10. i can create two different files "test" and ".test" in the same directory. now suppose i want to change the attribute of the hidden file .test to visible is it possible??? since "." is just an attribute to mark a file hidden why is unix allows creation of "file" and... (14 Replies)
Discussion started by: vikashtulsiyan
14 Replies

7. UNIX for Advanced & Expert Users

test if there are files in directory

Hi, I am trying to test if there are files in a directory and if theres i want to get a list. Any ideas? Thanks in advance (1 Reply)
Discussion started by: RSAM2007
1 Replies

8. Shell Programming and Scripting

how to test filename is file or directory

hi all plz let me how to test output of "tail -1 filelist.lst" is file or directory. regards -Bali Reddy (1 Reply)
Discussion started by: balireddy_77
1 Replies

9. HP-UX

How to test directory availibility

Hello, I'm trying to test if a directory specified in a script parameter is available or not. I wrote a little code to do so, but there's a problem because I receive an error message. My code: #Verify command parameter if then echo 'Incorrect command parameter' echo... (3 Replies)
Discussion started by: santuna
3 Replies

Featured Tech Videos