Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
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.
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
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.
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
#!/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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.