views:

59

answers:

4

i have my shell code:

#!/bin/sh
if [ ! -d $1 ]
 then echo $1 nu este director
  exit1
fi 
ls -R $1 >temp
permission= ls -al $1 | cut -d" " -f1   
for i in `cat temp`
do 
  perm= ls -l $i | cut -d" " -f1  
if [ $permission -ne $perm ] 
   then n=`expr $n + 1`
fi
echo   $n
done

and my statement which sounds like this:for an folder given as a parameter from keyboard i have to show how many files or sub-folder from inside the folder have different rights as the folder.this program it show the rights..but doesn't count nothing..plss help me

A: 

you want to use command substitution:

permission=$(ls -al $1 | cut -d" " -f1)
# ...
perm=$(ls -l $i | cut -d" " -f1)
knittl
A: 

You are not initializing your variable $n, so your call to expr expands to expr + 1 which is a syntax error. You should see lots of "expr: syntax error" messages on stderr. Just add the line n=0 before your loop and you should be fine.

Sven
A: 

Adding to other's answers:

exit1 should be exit 1

codaddict
A: 
  • You shouldn't use -ne for string comparisons. You need to do this:

    if [ "$permission" != "$perm" ] 
    then 
        n=`expr $n + 1`
    fi
    
  • You need to initialise n before you can increment it.

    n=0
    
  • You need to fix your command substitution:

    permission=$(ls -al $1 | cut -d" " -f1)  
    perm=$(ls -l $i | cut -d" " -f1)
    
  • exit1 should be exit 1

dogbane