开发者

Shell program doesn't work

For multiple folders provided as input from the user, I'd like to count how many of the files and folders they contain have different permission settings as the container folder itself.

I've written the following shell code. Why does it display the rights, but not count anything?

#!/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=`exp开发者_Python百科r $n + 1`
fi
echo   $n
done


  • 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


you want to use command substitution:

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


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.


Adding to other's answers:

exit1 should be exit 1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜