0

learning the basics of scripting and I have this line of code:

#!/bin/sh


if [ $# -ne 1]  
then
   echo "Please provide one directory to search"
   echo "Example usage: something.sh directory_name"
   exit 1
fi

however when I run this I get the error if: Expression Syntax. what exactly am I missing here? also the next if statement gives me the same error as well:(I want it to check if the variable is a directory or not

if [ !-d $1 ]
then
   echo "$1: Invalid Directory. Aborting Script."
   exit 1
fi

full script in case that helps:

#!/bin/sh
if [ $# -ne 1 ]
then
   echo "Please provide one directory to search"
   echo "Example usage: something directory_name"
   exit 1
fi
if [ ! -d $1 ]
then
   echo "$1: Invalid Directory. Aborting Script."
   exit 1
fi
cd "$1"
read=0
write=0
exe=0
numFiles=0
numDir=0
for filename in *
do
if [ -r filename ]
then
   `expr $read + 1`
fi
if [ -x filename ]
then
   `expr $exe + 1`
fi
if [ -w filename ]
then
   `expr $write + 1`
fi
if [ -f filename ]
then
   `expr $numFiles + 1`
fi
done
for dir in */
do
if [ -r dir ]
then
   `expr $read + 1`
fi
if [ -x dir ]
then
   `expr $exe + 1`
fi
if [ -w dir ]
then
    `expr $write + 1`
fi
if [ -d dir ]
then
   `expr $numDir + 1`
fi
done
total=`expr $numDir + $numFiles`
echo "In the directory $1"
echo "  Number of directories     : $numDir"
echo "  Number of files           : $numFiles"
echo "  Number of readable items  : $read"
echo "  Number of writable        : $write"
echo "  Number of executable items: $exe"
echo "-------------------------------"
echo "Total number of items: $total"

UPDATE: Just in case anyone stumbles upon this, I had header comments before the #!/bin/bash which was causing the if expression error

  • 1
    It's also a good practice to quote your variables."$1" – rahul Apr 21 '15 at 16:02
  • @taliezin thanks, i put the spaces in but its still giving me the same error, and I put a couple debug statemtns in that arent being read so it definitely lies within tjose lines of code – Tyler Kelly Apr 21 '15 at 16:11
  • Could it have to do with line breaks? Windows to unix mismatches? you can run dos2unix on the file if you have to confirm. It would also help if you paste your entire script. – rahul Apr 21 '15 at 16:14
  • Which shell are you using? Neither bash nor dash give me that error message (they both complain of a missing ]). Is it possible you're using csh/tcsh? How are you running this script? – derobert Apr 21 '15 at 16:20
  • @derobert im using bourne shell I believe, Ill go ahead and post the full script – Tyler Kelly Apr 21 '15 at 16:29
1

You have quite a few issues. Here is the working version. You need to use $ to denote the value of a variable ( $filename instead of filename ) and you need to assign the result of an expression to a variable, rather than just invoking the expression ( read=expr $read + 1 instead of expr $read + 1 ).

 #!/bin/bash
if [ $# -ne 1 ]
then
   echo "Please provide one directory to search"
   echo "Example usage: assignment4.sh directory_name"
   exit 1
fi
if [ ! -d "$1" ]
then
   echo "$1: Invalid Directory. Aborting Script."
   exit 1
fi
cd "$1"
read=0
write=0
exe=0
numFiles=0
numDir=0
for filename in *
do
if [ -r "$filename" ]
then
   read=`expr $read + 1`
fi
if [ -x filename ]
then
   exe=`expr $exe + 1`
fi
if [ -w "$filename" ]
then
   write=`expr $write + 1`
fi
if [ -f "$filename" ]
then
   numFiles=`expr $numFiles + 1`
fi
done
for dir in */
do
if [ -r "$dir" ]
then
   read=`expr $read + 1`
fi
if [ -x "$dir" ]
then
   exe=`expr $exe + 1`
fi
if [ -w "$dir" ]
then
    write=`expr $write + 1`
fi
if [ -d "$dir" ]
then
   numDir=`expr $numDir + 1`
fi
done
total=`expr $numDir + $numFiles`
echo "In the directory $1"
echo "  Number of directories     : $numDir"
echo "  Number of files           : $numFiles"
echo "  Number of readable items  : $read"
echo "  Number of writable        : $write"
echo "  Number of executable items: $exe"
echo "-------------------------------"
echo "Total number of items: $total"
$ ./script.sh
Please provide one directory to search
Example usage: something.sh directory_name

$ ./script.sh /home/username

In the directory /home/username
  Number of directories     : 8
  Number of files           : 104
  Number of readable items  : 120
  Number of writable        : 120
  Number of executable items: 8
-------------------------------
Total number of items: 112
  • I think my code is just cursed, made all those corrections and it still gives me that error, but when I run a new .sh file with the exact code it runs perfectly. thanks for the help! – Tyler Kelly Apr 21 '15 at 16:54
  • 1
    @user3470987 have you edited the original file in Windows at some point? Sounds like you might have some stray \r characters. – terdon Apr 21 '15 at 17:30
1

What you're missing is spaces.

This works:

if [ $# -ne 1 ]
then
   echo "One argument needed"
   exit 1
fi

Likewise:

if [ ! -d $1 ] 
  • thanks, I'm still getting the same error though...put in a couple debug statements after and it wont read them either so the problem definitely lies within those couple lines. – Tyler Kelly Apr 21 '15 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.