World writable home dirs

what is wrong with this script?
I get:

./perm.sh: command substitution: line 21: unexpected EOF while looking for matching `"'
./perm.sh: command substitution: line 22: syntax error: unexpected end of file

Script:

#!/bin/bash
for dir in `/bin/cat /etc/passwd | /bin/egrep -v "(root|halt|sync|shutdown) |\
/bin/awk -F: '($8 == "PS" && $7 != "/dev/null") { print $6 }'`; do
dirperm=`/bin/ls -ld $dir | /bin/cut -f1 -d" "`
if [ `echo $dirperm | /bin/cut -c6 ` != "-" ]; then
 echo "Group Write permission set on directory $dir"
fi
if [ `echo $dirperm | /bin/cut -c8 ` != "-" ]; then
 echo "Other Read permission set on directory $dir"
fi
if [ `echo $dirperm | /bin/cut -c9 ` != "-" ]; then
 echo "Other Write permission set on directory $dir"
fi
if [ `echo $dirperm | /bin/cut -c10 ` != "-" ]; then
 echo "Other Execute permission set on directory $dir"
fi
done

Missing double quotes:

for dir in `/bin/cat /etc/passwd | /bin/egrep -v "(root|halt|sync|shutdown)"

Also the script is poorly written. I see UUOC and other utilities. It can be improved a lot.

1 Like

any critiques would be appreciated....

Why use cat at the beginning?

/bin/egrep -v "(root|halt|sync|shutdown)" /etc/passwd |etc...

I usually set PATH at the top of the script, then forget about it.
This, and a while loop, increases readability IMHO.
Also, you can handle the egrep -v within the awk (that uses ERE like egrep).

#!/bin/bash
export PATH; PATH=/bin:/usr/bin:/usr/sbin:/sbin
< /etc/passwd awk -F: '
($0!~/(root|halt|sync|shutdown)/ && $8 == "PS" && $7 != "/dev/null") { print $6 }
' |
while read dir
do
  dirperm=`ls -ld "$dir" | cut -f1 -d" "`
  if [ `echo $dirperm | cut -c6 ` != "-" ]; then
    echo "Group Write permission set on directory $dir"
  fi
  if [ `echo $dirperm | cut -c8 ` != "-" ]; then
    echo "Other Read permission set on directory $dir"
  fi
  if [ `echo $dirperm | cut -c9 ` != "-" ]; then
    echo "Other Write permission set on directory $dir"
  fi
  if [ `echo $dirperm | cut -c10 ` != "-" ]; then
    echo "Other Execute permission set on directory $dir"
  fi
done

awk has the further advantage that you can limit the search to a certain field, e.g.

$1!~/(root|halt|sync|shutdown)/