Skip to content

Instantly share code, notes, and snippets.

@aimtiaz11
Last active September 18, 2018 00:45
Show Gist options
  • Save aimtiaz11/da7ed93857e09047a26c to your computer and use it in GitHub Desktop.
Save aimtiaz11/da7ed93857e09047a26c to your computer and use it in GitHub Desktop.
UNIX Script: Function to check if the last command ran successfully

Script to run another script

START_TIME=`date +'%Y%m%d-%H%M'`
BACKUP_LOG_FILE=/home/bitnami/scripts/drupal_backup_$START_TIME.log

/home/bitnami/scripts/backupDrupal_v2.sh >> $BACKUP_LOG_FILE 2<&1

RUN_RESULT=$?
echo "Run Result is $RUN_RESULT"

if [ $RUN_RESULT -gt 0 ]; then
	OUTCOME="FAILURE"
	mailx -s "$OUTCOME - Bitnami Drupal Archive Job (weave04vm6) - $START_TIME" dba@test < $BACKUP_LOG_FILE
	rm $BACKUP_LOG_FILE
else
	OUTCOME="SUCCESS"
	mv $BACKUP_LOG_FILE /windowsbackup/weave04vm6
	mailx -s "$OUTCOME - Bitnami Drupal Archive Job (weave04vm6) - $START_TIME" dba@test < /windowsbackup/xx/drupal_backup_$START_TIME.log
fi

The following function need to placed at the top of the script before it is called.

# $1 = success message
# $2 = failure message
check_err()
{
        if [ $? -gt 0 ]; then
                echo $2
                exit 1
        else
                echo $1
        fi

}
Usage
check_err "all good" "cannot find path"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment