Operating System - HP-UX
1835041 Members
2162 Online
110073 Solutions
New Discussion

Re: Shell script: how to check whether a directory is empty?

 
SOLVED
Go to solution
cybermilky
Occasional Contributor

Shell script: how to check whether a directory is empty?

I'm writing a shell script that checks the read/write permission of a file. I encounter some problems of the if-else statement which consists of some syntax errors. What's wrong with my shell programming?
---------------
#!/bin/sh
echo "Enter a file: \c"
read file
if [ -r "$file -a -w "$file" ]
then
echo "$file is readable and writable."
elif [ -r "$file" -a ! -w "$file" ]
then
echo "$file is readable but not writable."
else
echo "$file is not readable and not writable"
fi
---------------
The script is supposed to read a file from user and then checks whether it is readable or writable. How do I correct my errors?
3 REPLIES 3
Tom Danzig
Honored Contributor
Solution

Re: Shell script: how to check whether a directory is empty?

You're missing a double quote:

if [ -r "$file -a -w "$file" ]

S/B

if [ -r "$file" -a -w "$file" ]
Martin Robinson
Frequent Advisor

Re: Shell script: how to check whether a directory is empty?

I am curious as the difference between the title of your post and the problem you describe therein. Have you solved the problem described by the title?
Theresa Patrie
Regular Advisor

Re: Shell script: how to check whether a directory is empty?

Martin,
The title of this post is the same as another post from cybermilky on the same day. Must have forgotten to change the title on this one.
Theresa
This is my easy job!