Bash script - im missing something and cant's figure out what

I just put together a script for work that will essentially automate the migration of our Windows fileserver to my newly created Debian based SAMBA server.
My script will create the necessary directories then copy the data over to my new server, after that it will set the ACL's by using getfacl/setfacl.

I tried to run my script by using ./man_fs2-copy.sh:

./man_fs2-copy.sh: line 28: syntax error near unexpected token `done'
./man_fs2-copy.sh: line 28: `done cat /tmp/error.log | mail -s "Here is the log for $share" $email && rm /tmp/error.log'

I also tried to run the script like this, sh man_fs2-copy.sh and I get the following error,

man_fs2-copy.sh: 22: Syntax error: Bad for loop variable
#!/bin/bash
#
#
# This script will create the necessary directories, mount man_fs2 (c$, e$, f$ and g$)
# then read from directorylist.txt and start copying the directory's
#
#
echo "What is your email address?"
read email
#
#
mkdir -p /servers/backups /servers/man_fs2 /servers/man_fs2-logs
mkdir -p /home/man_fs2/c /home/man_fs2/e /home/man_fs2/f /home/man_fs2/g
chmod -R 777 /servers
cd /servers
#
# Mount man_fs2
mount //man_fs2/c$ /home/man_fs2/c -oacl,username=administrator@domain.local,password=*****
mount //man_fs2/e$ /home/man_fs2/e -oacl,username=administrator@domain.local,password=*****
mount //man_fs2/f$ /home/man_fs2/f -oacl,username=administrator@domain.local,password=*****
mount //man_fs2/g$ /home/man_fs2/g -oacl,username=administrator@domain.local,password=*****
#
#
for (( c=1; c<=2; c++ ))
do
cat directorylist.txt | while read source dest share

      cp -Rv "$source" "$dest" 2> /tmp/error.log
      echo "Setting ACLs from $source to $dest:"
         getfacl -R "$source" | setfacl -R --set-file=- "$dest"
done
cat /tmp/error.log | mail -s "Here is the log for $share" $email && rm /tmp/error.log

    echo "Pass # $c"
    if [ ! -d "$src" ];
    then
       cp -aR $src $dest
       getfacl -R $src | setfacl -R --set-file=- $dest
    else
       cp -uR $src $dest
       getfacl -R $src | setfacl -R --set-file=- $dest
    fi

done


echo "Unmount all drives"
umount /home/man_fs2/c
umount /home/man_fs2/e
umount /home/man_fs2/f
umount /home/man_fs2/g

exit

It looks to me like you're missing a do at the start of the while:

for (( c=1; c<=2; c++ ))
do
    cat directorylist.txt | while read source dest share
    do
        cp -Rv "$source" "$dest" 2> /tmp/error.log
        echo "Setting ACLs from $source to $dest:"
        getfacl -R "$source" | setfacl -R --set-file=- "$dest"
    done

    cat /tmp/error.log | mail -s "Here is the log for $share" $email && rm /tmp/error.log

    echo "Pass # $c"
    if [ ! -d "$src" ];
    then
       cp -aR $src $dest
       getfacl -R $src | setfacl -R --set-file=- $dest
    else
       cp -uR $src $dest
       getfacl -R $src | setfacl -R --set-file=- $dest
    fi
done

I've changed your indention to be what I think you intended as far as loop structure is concerned. Bash didn't complain about a bad loop var when I changed and ran the script through -n syntax checking.

1 Like

Thanks alot for your help. That fixed those errors

---------- Post updated at 10:10 PM ---------- Previous update was at 08:57 PM ----------

I fixed the issue that I was having but im now having trouble with another part of my script, specifically when I try to do the getfacl/setfacl.

Setting ACLs from "/home/man_fs2/e/couplings*" to "/servers/man_fs2/couplings":
getfacl: "/home/man_fs2/e/couplings*": No such file or directory
Usage: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ...

It's difficult to say for sure, but your problem might be the quotes:

getfacl -R "$source" | setfacl -R --set-file=- "$dest"

I think they should go....

By quoting the variable $source, you don't allow for any file glob expansion. Thus /home/man_fs2/e/couplings* does not expand and the file with the splat (*) at the end does not exist. This assumes that there are multiple files in /home/man_fs2/e/ that start with couplings.

If couplings is a directory, and you wish to have all files in the directory processed, then in addition to dropping the quotes, you'll need to add a slant:

/home/man_fs2/e/couplings/*

This is speculation as I don't really know what your source directory structure looks like. With luck this is it, and if not I hope it helps get you going again.

1 Like