I have a backup script to run as a cron job and create a daily backup of /var/log/messages/. Please note this script is more for learning experience than actual application. I have added if / else statements to check the exit code after the tar command is executed. The script worked fine before, but after I added this feature it hangs. Code below:#!/bin/bashadmin=foo@bar.comof=messages-backup-$(date +%Y%m%d).tgzcd /var/log/tar -czvf $of messagesif [ $? -eq 0 ]; then  mail -s Backup of /var/log/messages was successful! $admin  exit 0else  mail -s Backup of /var/log/messages has failed! $admin  exit 1ficp $of /home/user/rm -rf $ofOutput:[root@nidesant2 Scripts]# ./backup.sh messages^C(Interrupt -- one more to kill letter)
Backup script is hanging
bash;shell script;backup
Change mail -s Backup of /var/log/messages was successful! $admintoecho hello | mail -s Backup of /var/log/messages was successful! $adminor mail -s Backup of /var/log/messages was successful! $admin < /dev/nullThe first option, you can use whatever text you like for the message body.  The second option will send the email with an empty message body.  Repeat the same change for the second email command for an unsuccessful backup.For learning purposes, you should check the successful completion of creating the tar file, copying it, and deleting it before determining that it was successful.  To do that, you could do this.Changetar -czvf $of messagestotar -czvf $of messages && cp $of /home/user/ && rm -rf $ofAlso, with this change you would remove cp $of /home/user/ and rm -rf $of from the end of the script.Then your if/else will execute based on the successful completion of all three commands or an unsuccessful completion of any one of the three commands in that string.