There is corrected version of your script:
#!/bin/bash
for i in /home/*
do
if [ -f "$i/.forward" ]
then
echo "$i" >> /tmp/forwards.txt
cat "$i/.forward" >> /tmp/forwards.txt
fi
done
Some points:
we checks for presents of
.forward
file inside home directory instead of existence of home directory itselfon each iteration
$i
contains name of home directory (like/home/user1
). So we use its value instead of output ofpwd
command which always returns current directory (it doesn't change in our case)instead of
/home/*/.forward
we use"/home/$i/.forward"
because*
after substitution gives to us all directories, while we need only current
Another, shortest version of this script may looks like this:
find /home -type f -name '.forward' | while read F; do
dirname "$F" >>/tmp/forwards.txt
cat "$F" >>/tmp/forwards.txt
done