Code: Select all
#!/bin/bash
# Remember to make executable and check permisions
# To scan drives for a virus using ClamAV
# You will need clamav and it's depends installed.
# If you want to make use of any success and or error sounds using ogg or mp3
# I suggest installing mplayer and using it in a script.
# line below will run mplayer in the background avoiding
# mplayers quite verbose output, play the ogg file and close.
#
# Example : use below in a script making needed changes for your system.
#
# mplayer /usr/share/sounds/clam_ogg/Clam-Success.ogg </dev/null >/dev/null 2>&1 &
TARGET="/media/TvShows" # usb 1T HDD
VIRUS="/home/Greg/.VIRUS/" # directory you want the virus's moved to
HIDE="(hidden directory)" # uncomment if VIRUS is a hidden directory
RIGHT_NOW=$(date "+ %a %x %r")
TIME_STAMP="Updated on $RIGHT_NOW by $USER"
NO_DRIVE="Failed to scan: Not plugged in and or mounted"
YIKES="Yikes drive not plugged in and or mounted : Nothing to scan"
SCAN_LOG="$HOME/ScanLog/Clam.log" # directory you want the scan log placed
SUBJECT="----------- SCAN REPORT -----------" # Used for log
SUBJECT2="----------- SCAN FAILED -----------" # used for log
VERSION="11-01-2014" # Used for log
clear
if [ -d $TARGET ]; then
echo
echo -e "Getting ready to scan $TARGET for viruses\n"
echo -e "If any infected files are found they will be\n"
echo -e "moved to $VIRUS $HIDE\n"
echo -e "Scan Log will be in $SCAN_LOG\n"
echo -e "Scan will begin shortly, be patient $USER\n"
clamscan -r --move=$VIRUS $TARGET -l $SCAN_LOG # clamscan command and arguments/options
# to play a sound script when scan is done uncomment and edit as needed
#/usr/local/bin/Clam-Success # my sound script
# uncomment 2 lines below to remove old log and create new log
# leave commented to append to log
# rm -f "$HOME/ScanLog/Clam.log # Junk old logfile.
# echo -e "$SUBJECT - $(date "+ %a %x %r")\n" > $SCAN_LOG # Create new logfile
echo >> $SCAN_LOG # inserts blank line in log (for layout and formatting)
echo $SUBJECT >> $SCAN_LOG
echo -e "Script: $(basename $0) v$VERSION - for user: $LOGNAME" >> $SCAN_LOG
echo -e "Scanmode: Recursive\nFiles Scanned: $TARGET\n$TIME_STAMP\n" >> $SCAN_LOG
# above logging is used when drive/directory is present
sleep 12; clear
# below is used for log when drive is missing or not mounted
else
echo
echo -e "$YIKES\n"
echo >> $SCAN_LOG # makes a blank line in log (for layout and formatting)
echo -e "-------------------------------------------------------------------------------\n\n" >> $SCAN_LOG
echo $SUBJECT2 >> $SCAN_LOG
echo -e "Script: $(basename $0) v$VERSION - for user: $LOGNAME" >> $SCAN_LOG
echo -e "Missing target $TARGET\n$NO_DRIVE\n$TIME_STAMP\n" >> $SCAN_LOG
sleep 4; clear
# to play a sound script on "Failed to scan" uncomment and edit as needed
#/usr/local/bin/Clam-Error # my sound script
fi
CLAMSCAN=$? # Exit status.
if [ "$CLAMSCAN" -eq "1" ]; then # Found a virus? exit's as 1?
/usr/local/bin/Clam-Error1
elif [ "$CLAMSCAN" -gt "1" ]; then
/usr/local/bin/Clam-Error2 # Internal clam error? how to use for no usb drive or target present
else
/usr/local/bin/Clam-Success # No virus found exit's as 0
# No drive exit's as 0,how to make it greater than 1?
fi
exit
heres where i have a problem . when the script runs with the drive mounted and no viruse found it exits as 0 and plays the success sound script as it should.
but when the drive isn't there and runs the not there or not mounted portion it returns a 0 also. how do I get it to return a greater than 1 to run the error sound script?
I know i can put the commands after each portion and it works when i do that but i am trying to figure out how to use exit codes....heres the relevant portion of exit choices.
Code: Select all
CLAMSCAN=$? # Exit status.
if [ "$CLAMSCAN" -eq "1" ]; then # Found a virus? exit's as 1?
/usr/local/bin/Clam-Error1 # No drive exit's as 0,how to make it greater than 1?
elif [ "$CLAMSCAN" -gt "1" ]; then
/usr/local/bin/Clam-Error2 # Internal clam error? how to use for no usb drive or target present
else
/usr/local/bin/Clam-Success # No virus found exit's as 0
fi