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
-nefor string comparisons. You need to do this:if [ "$permission" != "$perm" ] then n=`expr $n + 1` fiYou need to initialise
nbefore you can increment it.n=0You need to fix your command substitution:
permission=$(ls -al $1 | cut -d" " -f1) perm=$(ls -l $i | cut -d" " -f1)exit1should beexit 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
加载中,请稍侯......
精彩评论