Welcome!

Please read this important information about Spectre and Meltdown vulnerabilities.
Please read this important information about MX sources lists.

News
  • MX Linux on social media: here
  • Mepis support still here
Current releases
  • MX-17.1 Final release info here
  • antiX-17 release info here
New users
  • Please read this first, and don't forget to add system and hardware information to posts!
  • Read Forum Rules

Make use of exit codes to run a called script ? SOLVED

Here you can exchange scripts that you created or have permission to share with other users.
Message
Author
User avatar
GDixon
Forum Regular
Forum Regular
Posts: 358
Joined: Fri Nov 02, 2007 4:39 pm

Make use of exit codes to run a called script ? SOLVED

#1 Postby GDixon » Sun Nov 02, 2014 4:14 am

heres the whole script

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


I'm at a dead end and none of the books i have cover this worth a damn.
Last edited by GDixon on Tue Nov 04, 2014 5:33 pm, edited 1 time in total.

User avatar
Adrian
Forum Veteran
Forum Veteran
Posts: 8147
Age: 43
Joined: Wed Jul 12, 2006 1:42 am

Re: how to make use of exit codes to run a called script ???

#2 Postby Adrian » Mon Nov 03, 2014 12:31 pm

Code: Select all

mountpoint -q /media/TvShows

(or whatever mount point you use) should return 1 if it's not mounted (or not a mountpoint) and 0 if it's mounted. Use this to test if the driver is mounted.

Code: Select all

if mountpoint -q /mnt/foo
then
   echo "mounted"
else
   echo "not mounted"
fi

User avatar
GDixon
Forum Regular
Forum Regular
Posts: 358
Joined: Fri Nov 02, 2007 4:39 pm

Re: how to make use of exit codes to run a called script ???

#3 Postby GDixon » Tue Nov 04, 2014 8:57 am

I/m looking more for exit codes so I can determine exit 1 virus found exit 0 none found exit -gt 2 error .

I've learned it's the LAST command that sets the exit code so what i want is possible but not straight forward at all and will take if elif else type comands nested in if else or if type trees. I need to catch the exit code after the command right away If i want to branch sounds or messages for that command.
Once the proper coding is figured out it is just reusable like most code, just makes for a bigger script.

My biggest mistake was not knowing it is the last command executed that sets the exit code. something like this below nested after the command I want to catch the exit code on should work. At least it will be the next testing phase to find out.

Code: Select all

exit=?

# useing echo to print to screen for testing purposes

if [  $? -eq 1    ]; then   
 call play sound script
    echo "playing found virus script because exit = 1 TEST"; sleep 6 

elif  [  $? -gt 1  ]; then
 call play sound script
    echo "playing error script because exit is greater than 1 TEST"; sleep 6

else
 play no virus sound script
    echo "playing no virus script because exit is less than 1 (exit=0) TEST"; sleep 6

fi
      exit


Once I figure out proper nesting then it will be easy peasy to get the exit code of the needed commands.

Not sure it's worth the effort but it will be more for learning than anything else.


I do have a place to test if mounted ( before i call the freshclam command to update the virus data base)
echo the yikes no drive or not mounted spiel and don't run freshclam or it is ok to run freshclam update and continue the scan so that looks like a useful test.
hmmm should also test for a internet connection for calling freshclam and either run or not run accordingly.

my my what a tangled web i can cause lol

User avatar
GDixon
Forum Regular
Forum Regular
Posts: 358
Joined: Fri Nov 02, 2007 4:39 pm

Re: how to make use of exit codes to run a called script ???

#4 Postby GDixon » Tue Nov 04, 2014 10:03 am

I checked the mountpoint man pages and it looks like mountpoint checks to see if the mountpoint is a directory.

For doing what I'm trying mountpoint may be a poor choice since not all virus scans start with a mountpoint but can start in any directory even if it is not a mount point.

or say /media/TvShows is my mountpoint (which it is)

does that mean /media/TvShows/ any other directory is also a mountpoint?

I did a test and one is a mountpoint and returns mounted and /media/TvShows/any other directory is not and returns

not mounted.

it's good for testing mounts but may not work for some of what i want but is good for other things i might need

User avatar
GDixon
Forum Regular
Forum Regular
Posts: 358
Joined: Fri Nov 02, 2007 4:39 pm

Re: how to make use of exit codes to run a called script ???

#5 Postby GDixon » Tue Nov 04, 2014 2:34 pm

Heres a working script that uses exit codes.

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
#   and call the script instead of the mplayer commands, much easier and cleaner

# mplayer /usr/share/sounds/clam_ogg/Clam-Success.ogg </dev/null >/dev/null 2>&1 &


####### Make sure to change the Target below to what you want scanned #######


TARGET="/CHANGE/TO/YOUR/TARGET/"    # change to your target                   ##### IMPORTANT #####
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"    # used for log
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-04-2014"                           # Used for log
VIRUS0="---------- NO VIRUS FOUND ----------"  # use for exit code 0
VIRUS1="----- VIRUS  FOUND : CHECK LOG -----"  # use for exit code -eq 1
VIRUS2="----- ERROR  FOUND : CHECK LOG -----"  # use for exit code -gt 1
SOUND0="location/of/no/virus/found/script"     # change to call your sound script
SOUND1="location/of/virus/found/script"        # change to call your sound script
SOUND2="location/of/error/script"              # change to call your sound script
exit=?                                         # Exit status


clear

if [ -d $TARGET ]; then
    echo  # Blank line to make reading easier   
    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


             

              if [ "$?" -eq "1" ]; then
                      echo  >> $SCAN_LOG           # inserts blank line in log (for layout and formatting)
                      echo $VIRUS1 >> $SCAN_LOG    # Prints to log
                      echo                         # Blank line to make reading easier
                      echo -e "$VIRUS1"            # Prints to screen
                     # Uncomment below to call your sound script
                     #$SOUND1

                    elif [ "$?" -gt "1" ]; then
                      echo  >> $SCAN_LOG
                      echo $VIRUS2 >> $SCAN_LOG
                      echo
                      echo -e "$VIRUS2"
                     #$SOUND2

                    else
                      echo  >> $SCAN_LOG
                      echo $VIRUS0 >> $SCAN_LOG
                      echo
                      echo -e "$VIRUS0"
                     #$SOUND0

              fi


# 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    # inserts 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 P\n$NO_DRIVE\n$TIME_STAMP\n" >> $SCAN_LOG

       sleep 4; clear


     fi

       exit


I've learned that the positioning or nesting of the portion that calls the error codes is very important it must be immediatly after the command you want the exit codes from and also the exit=? part has to be before said command.

Now to call freshclam to udate the virus data base in the beggining but checking to make sure you have an internet connection before so if there is no connection it will skip trying to update with freshclam.
hmmm or find a way to also check if the last update was within the last 3 to 6 hours and skip if so.
Still trying to found a way to have a progress bar or even a spinning wheel to show activity instaed of the verbose out put to screen. I know you can turn off the verbose but then you don't know if anything is happening.


Return to “Scripts”

Who is online

Users browsing this forum: No registered users and 2 guests