enigma1234567890
asked on
Scripting with e-mails and errors, and some logic
I have created the script below and need to know how to make the following two change:
1. Check if a file is in the downloads directory if a file is in it move it to the archiced folder. As it statnds if nothing is in the downloads folder i get an error when I run the mv command.
2. Send a e-mail saying download succeded if a file is downloaded via SFTP and send a different one saying failure if it failed. The current script just sends a email for success or failure.
#!/usr/bin/bash
########################## ########## ########## ########## ########## ########## ####
HOST='host1'
USERNAME='test'
########################## ########## ########## ########## ########## ########## #
/usr/bin/mv -f /downloads/* /downloads/archived
/usr/bin/sftp -b /dev/fd/0 $USERNAME@$HOST: <<EOF
lcd /downloads/
mget *
bye
EOF
##############
/usr/bin/mailx -s "SFTP Server completed jobrun successfully" test@test.com <successful-email.txt
exit
1. Check if a file is in the downloads directory if a file is in it move it to the archiced folder. As it statnds if nothing is in the downloads folder i get an error when I run the mv command.
2. Send a e-mail saying download succeded if a file is downloaded via SFTP and send a different one saying failure if it failed. The current script just sends a email for success or failure.
#!/usr/bin/bash
##########################
HOST='host1'
USERNAME='test'
##########################
/usr/bin/mv -f /downloads/* /downloads/archived
/usr/bin/sftp -b /dev/fd/0 $USERNAME@$HOST: <<EOF
lcd /downloads/
mget *
bye
EOF
##############
/usr/bin/mailx -s "SFTP Server completed jobrun successfully" test@test.com <successful-email.txt
exit
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if find /downloads -type f -exec mv {} /downloads/archived/ \ ;
then
echo "Files Added to Archive" | mail -s ""New FTP job" $USERNAME@$HOST
else
echo "Nothing new" | mail -s "NO New FTP" $USERNAME@$HOST
fi
ofcourse adjust to your needs, but the idea is simple if (the find command was successfull) then mail 1 else send mail 2
then
echo "Files Added to Archive" | mail -s ""New FTP job" $USERNAME@$HOST
else
echo "Nothing new" | mail -s "NO New FTP" $USERNAME@$HOST
fi
ofcourse adjust to your needs, but the idea is simple if (the find command was successfull) then mail 1 else send mail 2
ASKER
Hi
this is what I have but keep getting enexpected EOF scripting is not my area of experties as you can see. i want to check if a file is in the folder called /data/uploads. if it is there sftp the file to remote server. If not record that the cronjob ran but no files were found.
Thanks
#!/usr/bin/bash
########################## ########## ########## ########## ########## ###
HOST='x'
USERNAME='y'
########################## ########## ########## ########## ########## ########## #
# Copy file upto remote server #
########################## ########## ########## ########## ########## ########## #
if [-f /data/uploads] ; then
/usr/bin/sftp -b /dev/fd/0 $USERNAME@$HOST: <<EOF
lcd /data/uploads/
cd /data/upload/
mput *
bye
EOF
echo "****File uploaded****" $(date +%D:+%H:%M)
/usr/bin/mailx -s "SFTP Server completed jobrun successfully" rob@x.com <successful-email.txt
else
echo "****Nothing to upload****" $(date +%D:+%H:%M)
fi
done
this is what I have but keep getting enexpected EOF scripting is not my area of experties as you can see. i want to check if a file is in the folder called /data/uploads. if it is there sftp the file to remote server. If not record that the cronjob ran but no files were found.
Thanks
#!/usr/bin/bash
##########################
HOST='x'
USERNAME='y'
##########################
# Copy file upto remote server #
##########################
if [-f /data/uploads] ; then
/usr/bin/sftp -b /dev/fd/0 $USERNAME@$HOST: <<EOF
lcd /data/uploads/
cd /data/upload/
mput *
bye
EOF
echo "****File uploaded****" $(date +%D:+%H:%M)
/usr/bin/mailx -s "SFTP Server completed jobrun successfully" rob@x.com <successful-email.txt
else
echo "****Nothing to upload****" $(date +%D:+%H:%M)
fi
done
ASKER
some typos is last posting now fixed however, still have the same error not sure if you can run sftp commnads inside if statement
#!/usr/bin/bash
########################## ########## ########## ########## ########## ###
HOST='x'
USERNAME='y'
########################## ########## ########## ########## ########## ########## #
# Copy file upto remote server #
########################## ########## ########## ########## ########## ########## #
if [ -f /data/uploads/* ] ; then
/usr/bin/sftp -b /dev/fd/0 $USERNAME@$HOST: <<EOF
lcd /data/uploads/
cd /data/upload/
mput *
bye
EOF
echo "****File uploaded****" $(date +%D:+%H:%M)
/usr/bin/mailx -s "SFTP Server completed jobrun successfully" rob@x.com <successful-email.txt
else
echo "****Nothing to upload****" $(date +%D:+%H:%M)
fi
done
#!/usr/bin/bash
##########################
HOST='x'
USERNAME='y'
##########################
# Copy file upto remote server #
##########################
if [ -f /data/uploads/* ] ; then
/usr/bin/sftp -b /dev/fd/0 $USERNAME@$HOST: <<EOF
lcd /data/uploads/
cd /data/upload/
mput *
bye
EOF
echo "****File uploaded****" $(date +%D:+%H:%M)
/usr/bin/mailx -s "SFTP Server completed jobrun successfully" rob@x.com <successful-email.txt
else
echo "****Nothing to upload****" $(date +%D:+%H:%M)
fi
done
ASKER
seems that the issue is sftp commands will not work inside if statement anyone got any ideas other that creating two scripts which is not ideal
ASKER