LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-13-2008, 08:11 PM   #1
rwills
LQ Newbie
 
Registered: May 2008
Posts: 5

Rep: Reputation: 0
Bash script directory test


I am writing a script to verify some backups and am hitting a snag with using test operators. It seems that when testing if a variable is a directory it thinks all files are directories, even if they are not. Below is my script:

#!/bin/bash

if [ -d /vz/bkups/backup07-currentlist ]; then
echo is not a directory >> testdir.log
else
echo is a directory >> testdir.log
fi

the output from the script is:
is a directory

This file is NOT a directory:
-rw-r--r-- 1 root root 24 Dec 20 12:47 backup07-currentlist

Am I missing something here? Thanks for any advise!

Roy
 
Old 05-13-2008, 08:18 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
It looks like you have your echo statements around the wrong way. It should be:
Code:
if [ -d /vz/bkups/backup07-currentlist ]; then
  echo is a directory >> testdir.log
else
  echo is NOT a directory >> testdir.log
fi
The -d test will return true and execute the first echo statement if it is a directory. It will return fales and execute the second for everything else.
 
Old 05-13-2008, 08:43 PM   #3
rwills
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 0
ok, i changed the echo order and still nto displaying correctly. My script now looks like this:
#!/bin/bash

if [ -d /vz/bkups/dedbackups/ ]; then
echo is a directory >> testdir.log
else
echo is not a directory >> testdir.log
fi

And my outout is now:
is not a directory

Here is the directory:
drwxr-xr-x 2 root root 4096 Dec 18 09:45 dedbackups

It seems that it always fiding that the directory test is always false. but not sure why.
 
Old 05-13-2008, 08:52 PM   #4
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
What happens if you do the test without the trailing / (ie on "/vz/bkups/dedbackups")
 
Old 05-13-2008, 08:55 PM   #5
rwills
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 0
wuthout the training /:

#!/bin/bash

if [ -d /vz/bkups/dedbackups ]; then
echo is a directory >> testdir.log
else
echo is not a directory >> testdir.log
fi

Still not a directory.
 
Old 05-13-2008, 09:05 PM   #6
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
Do you have permission to see the /vz/bkups/dedbackups directory? When I create that directory structure here the script works fine. However, if I run chmod 0700 /vz and run it again I get is NOT a directory
 
Old 05-13-2008, 09:19 PM   #7
rwills
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 0
drwxr-xr-x 13 root root 4096 Dec 18 09:46 vz

are the permissions on the directory. I am running the script as root so it should not be a problem.
 
Old 05-13-2008, 09:23 PM   #8
rwills
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 0
well, decided to just 777 the entire dedbackups directory:
drwxrwxrwx 2 root root 4096 Dec 18 09:45 dedbackups

And still says it is not a directory. Is there any other way to check to see if its a directory other than the way I am using? I just don't understand why it fail on my server.
 
Old 05-13-2008, 10:03 PM   #9
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
Running ls -AlR /vz will show you what is there and whether you have permission to access it (but it looks like you already do). You could try some of the other tests from the command line to try and identify what is happening. For example:
Code:
$ [ -d /tmp ] && echo Is a directory
Is a directory
$ [ -f /tmp ] && echo Is a directory <= Returns nothing since it's not a file
$ [ -d /tmpzzz ] && echo Is a directory <= Returns nothing since it's not a directory
 
Old 11-20-2009, 04:27 PM   #10
als@tinet.cat
LQ Newbie
 
Registered: Nov 2009
Posts: 1

Rep: Reputation: 0
Just cd to that directory

I had the same problem. this code does work:
#!/bin/bash
# als_2009nov20. desa el separador de fitxers.
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
# als_2009nov20. modifica els fitxers.
cd $1
for fitxer in $(ls $1)
do
if [ -d "$fitxer" ]
then
echo "directori " /dades/scripts/trimtree $fitxer
else
echo "fitxer " /dades/scripts/trim $fitxer
fi
done
# als_2009nov20. recupera el separador de fitxers.
IFS=$SAVEIFS
 
Old 11-21-2009, 06:59 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
What is the output from
Code:
/usr/bin/ls -ld /vz
/usr/bin/ls -ld /vz/bkups
/usr/bin/ls -ld /vz/bkups/backup07-currentlist
/usr/bin/ls -ld /vz/bkups/dedbackups/
find /vz -maxdepth 2 -type d
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help with Bash script (test) Benanzo Linux - Software 1 04-09-2007 03:18 AM
Test output from bash script estratos Programming 6 11-16-2006 09:01 PM
bash file test - newline is a directory? haxcess Linux - General 2 03-26-2006 11:34 AM
bash script 'test' question linmix Programming 5 11-29-2005 11:49 AM
Bash, how do I test for *.txt files in a directory with IF? severian23 Programming 7 11-04-2004 09:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration