Scanvirus shell script update help

Hello. I did a script with opensuse forums help to make simple shell over clamav anti-virus scanner. I’m updating it for opensuse 13.2 - KDE, but i’m running into a few problems. I use one command to scan linux files and one to scan windows files. For linux, it masks out some directories. I believe the use of dir ‘windows’ has changed for linux. I need superuser permissions to access mswin drives. Here’s what I have so far. I’v added a clamav setup function and setup kde icons. I like to add gnome support. Can someone help me fix it?

#!/bin/bash  
#: Title       : /home/lord_valarian/bin/scanvirusu
#: Date Created: Thu Sep 2 19:27:00 PST 2010 
#: Last Edit   : Sat Feb 28 10:30:00 PST 2015
#: Author      : Lord Valarian #: Version     : 1.1.0
#: Description : Run virus scanning application 
#: Options     : p1: -w -l   p2: $2



# 
# shortcut code for clamscan
# 
    if  "$1" == "-windows" ]; then
            echo "Scanning...";  
            clamscan -r /windows $2
    elif  "$1" == "-linux" ]; then
            echo "Scanning...";  
            clamscan -r / --exclude-dir=windows --exclude-dir=/sys --exclude-dir=/proc --exclude-dir=/dev $2
    elif  "$1" == "-h" ]; then
        echo "";
        echo "Scan Virus";
            echo "help commands";
            echo "-------------";
            echo "-linux:  virus scan linux files";
            echo "-windows:  virus scan windows files";
            echo "--setup:  setup scanvirus";
            echo "--kdeicons:  setup icons";
            echo "";
    elif  "$1" == "--setup" ]; then
            echo "ScanVirus Setup";
            su -c 'zypper --non-interactive install clamav;chkconfig freshclam on;freshclam;exit'      
            read -p "Done. Press any key..." -n1 -s;echo "";
            exit
    elif  "$1" == "--kdeicons" ]; then
            #Create KDE Linux scan icon
            echo "#!/usr/bin/env xdg-open" > ScanVirus_KDE_Linux.desktop
            echo "[Desktop Entry]" >> ScanVirus_KDE_Linux.desktop
            echo "Comment[en_US]=" >> ScanVirus_KDE_Linux.desktop
            echo "Comment=" >> ScanVirus_KDE_Linux.desktop
            echo "Exec=sh scanvirus -linux -i" >> ScanVirus_KDE_Linux.desktop
            echo "GenericName[en_US]=" >> ScanVirus_KDE_Linux.desktop
            echo "GenericName=" >> ScanVirus_KDE_Linux.desktop
            echo "Icon=kde" >> ScanVirus_KDE_Linux.desktop
            echo "MimeType=" >> ScanVirus_KDE_Linux.desktop
            echo "Name[en_US]=ScanVirus - linux anti-virus scan" >> ScanVirus_KDE_Linux.desktop
            echo "Name=ScanVirus - linux anti-virus scan" >> ScanVirus_KDE_Linux.desktop
            echo "Path=$PATH" >> ScanVirus_KDE_Linux.desktop
            echo "StartupNotify=true" >> ScanVirus_KDE_Linux.desktop
            echo "Terminal=true" >> ScanVirus_KDE_Linux.desktop
            echo "TerminalOptions=\s--noclose" >> ScanVirus_KDE_Linux.desktop
            echo "Type=Application" >> ScanVirus_KDE_Linux.desktop
            echo "X-DBUS-ServiceName=" >> ScanVirus_KDE_Linux.desktop
            echo "X-DBUS-StartupType=" >> ScanVirus_KDE_Linux.desktop
            echo "X-KDE-SubstituteUID=false" >> ScanVirus_KDE_Linux.desktop
            echo "X-KDE-Username=" >> ScanVirus_KDE_Linux.desktop
            echo "X-SuSE-translate=true" >> ScanVirus_KDE_Linux.desktop
            chmod 744 ScanVirus_KDE_Linux.desktop
            #Create KDE windows anti-virus scan icon
            echo "#!/usr/bin/env xdg-open" > ScanVirus_KDE_Windows.desktop
            echo "[Desktop Entry]" >> ScanVirus_KDE_Windows.desktop
            echo "Comment[en_US]=" >> ScanVirus_KDE_Windows.desktop
            echo "Comment=" >> ScanVirus_KDE_Windows.desktop
            echo "Exec=sh scanvirus -windows" >> ScanVirus_KDE_Windows.desktop
            echo "GenericName[en_US]=" >> ScanVirus_KDE_Windows.desktop
            echo "GenericName=" >> ScanVirus_KDE_Windows.desktop
            echo "Icon=kde" >> ScanVirus_KDE_Windows.desktop
            echo "MimeType=" >> ScanVirus_KDE_Windows.desktop
            echo "Name[en_US]=ScanVirus - windows anti-virus scan" >> ScanVirus_KDE_Windows.desktop
            echo "Name=ScanVirus - windows anti-virus scan" >> ScanVirus_KDE_Windows.desktop
            echo "Path=$PATH" >> ScanVirus_KDE_Windows.desktop
            echo "StartupNotify=true" >> ScanVirus_KDE_Windows.desktop
            echo "Terminal=true" >> ScanVirus_KDE_Windows.desktop
            echo "TerminalOptions=\s--noclose" >> ScanVirus_KDE_Windows.desktop
            echo "Type=Application" >> ScanVirus_KDE_Windows.desktop
            echo "X-DBUS-ServiceName=" >> ScanVirus_KDE_Windows.desktop
            echo "X-DBUS-StartupType=" >> ScanVirus_KDE_Windows.desktop
            echo "X-KDE-SubstituteUID=false" >> ScanVirus_KDE_Windows.desktop
            echo "X-KDE-Username=" >> ScanVirus_KDE_Windows.desktop
            echo "X-SuSE-translate=true" >> ScanVirus_KDE_Windows.desktop
            chmod 744 ScanVirus_KDE_Windows.desktop
            echo "KDE Icons Created";
    else
        echo "";
        echo "Scan Virus";
        echo "-------------------";
            echo "-h:  show help file";
        echo "";
    fi
exit 0 
# End Of Script

Hi,

If all you’re after is to have root priv to scan windows machine then you can do the su - when scanning windows drive, otoh windose and unix permissions are different if that is what you are asking.

If the command requires a log-in shell, you can do

su - root -s /bin/bash -c 'my-command-to-execute'

If not then you can remove the

-s /bin/bash

I believe the use of dir ‘windows’ has changed for linux. I need superuser permissions to access mswin drives

It will depend on how you have the mount defined in /etc/fstab really. FWIW, I don’t have any fstab entry for my windows partition, but instead access it directly via Dolpin/udisks2, which does require polkit authentication (root credentials to mount), but that can be changed as required (subject to any security implications that need to be considered).

Hi,

No bug fixes made on your script, i just aligned the indentations.
You might want to test it first on a clean directory. :wink:

#!/usr/bin/env bash


#: Title       : scanvirusu
#: Date Created: Thu Sep 2 19:27:00 PST 2010
#: Last Edit   : Sat Feb 28 10:30:00 PST 2015
#: Author      : Lord Valarian
#: Version     : 1.1.0
#: Description : Run virus scanning application
#: Options     : p1: -w -l   p2: $2
#
# shortcut code for clamscan
#


Usage() {
  cat <<EOF


${BASH_SOURCE##*/} OPTION ${2:-ARGUMENT}


OPTIONS


-l,  --linux,      virus scan linux files
-w,  --windows,    virus scan windows files
-s,  --setup,      setup scanvirus
-k,  --kdeicons,   setup icons


EOF
}


Create_KDE_Linux_scan_icon() {
  cat > ScanVirus_KDE_Linux.desktop <<EOF
#!/usr/bin/env xdg-open
[Desktop Entry]
Comment[en_US]=
Comment=
Exec=sh scanvirus -linux -i
GenericName[en_US]=
GenericName=
Icon=kde
MimeType=
Name[en_US]=ScanVirus - linux anti-virus scan
Name=ScanVirus - linux anti-virus scan
Path=$PATH
StartupNotify=true
Terminal=true
TerminalOptions=\s--noclose
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=
X-KDE-SubstituteUID=false
X-KDE-Username=
X-SuSE-translate=trueEOF
EOF
}


Create_KDE_windows_anti_virus_scan_icon() {
  cat > ScanVirus_KDE_Windows.desktop <<EOF
#!/usr/bin/env xdg-open
[Desktop Entry]
Comment[en_US]=
Comment=
Exec=sh scanvirus -windows
GenericName[en_US]=
GenericName=
Icon=kde
MimeType=
Name[en_US]=ScanVirus - windows anti-virus scan
Name=ScanVirus - windows anti-virus scan
Path=$PATH
StartupNotify=true
Terminal=true
TerminalOptions=\s--noclose
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=
X-KDE-SubstituteUID=false
X-KDE-Username=
X-SuSE-translate=true
EOF
}


case $1 in
    -w|--windows)
      echo "Scanning..."
      clamscan -r "/windows" "$2"
         ;;
    -l|--linux)
      echo "Scanning..."
      clamscan -r "/" "--exclude-dir=windows" "--exclude-dir=/sys" "--exclude-dir=/proc" "--exclude-dir=/dev" "$2"
         ;;
    -s|--setup)
         echo "ScanVirus Setup"
         su - root -c 'zypper --non-interactive install clamav;chkconfig freshclam on;freshclam;exit'
         read -p "Done. Press any key..." -sn1;echo
         exit
         ;;
     -k|--kdeicons)
        Create_KDE_Linux_scan_icon && chmod 744 ScanVirus_KDE_Linux.desktop
        Create_KDE_windows_anti_virus_scan_icon && chmod 744 ScanVirus_KDE_Windows.desktop
        echo "KDE Icons Created..."
         ;;
     -h|--help)
       Usage
         ;;
           *)
         Usage >&2
         exit 1
         ;;
esac


exit 0
# End Of Script

In previous versions of opensuse to scan mswin directories only , I used ‘clamav -r /windows’. In superuser mode, where are all the mswin directories from root level path?

clamscan -r "/" "--exclude-dir=/sys" "--exclude-dir=/proc" "--exclude-dir=/dev"

This command took over an hour to scan. Does this command scan all files on all partitions but those excluded?

clamscan -r "/" "--exclude-dir=/sys" "--exclude-dir=/proc" "--exclude-dir=/dev" **--follow-dir-symlinks=0  ****--follow-file-symlinks=0**

This command took about same time as the previous script that scanned linux files only. This should be the one for scanning all linux files.

clamscan -r "/" "--exclude-dir=/sys" "--exclude-dir=/proc" "--exclude-dir=/dev" **--cross-fs=no**

Do I use this alternate? Don’t scan other file systems means clamscan will only scan linux file systems, not mswin NTFS and FAT32.

As a matter of style, I prefer using 'if-elseif-else-endif. For users, I want to make it as easy to read as possible. Thanks for the assist. I have advanced programing experience, but not much linux bash. I follow most of it. Can you explain how it works? line groups.

Hi,

Case is the portable pattern matching builtin that is define by POSIX sh, which works in any POSIX compliant shell like bash. You can find it here.

 http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_09_04

Under the

Case Conditional Construct

Or looking at man bash

PAGER='less +/case\ word' man bash

Well it maybe a matter of style but IMHO if you’re repeating your code too many times in a script/program then you probably doing it wrong. In this case the use of the if-statement and echo. Since you have an advance programing experience then you will probably agree with my sentiments. :slight_smile:

One last thing there is no elseif endif in bash or any posix compliant shell at least not according to this site.

http://pubs.opengroup.org/onlinepubs/009695399/utilities/sh.html

Also i don’t have a windows machine so I’m not the person that can assist you with clamav.

I didn’t use the case function much. It seems more readable with ‘if-elseif-else-endif’. As I stated before, just my programing style. Whoever else dual boots, I’m stuck. Here’s the updated code. I left out the shortcuts ‘-l’, ‘-w’, etc for my benefit. I’ll add it back in later. Need help with linux and windows only scans. I’v not had a chance to test the code yet. Removing the ‘-i’ might tell me if I’m scanning windows and/or linux, but that’s a lot of output to look over.

#!/bin/bash
 
#: Title       : Scan Virus Clamscan Shell
#: Date Created: Thu Sep 2 19:27:00 PST 2010 
#: Last Edit   : Wed Mar 4 3:00:00 PST 2015
#: Author      : Lord Valarian #: Version     : 1.1.0
#: Description : Run virus scanning application 
#: Options     : p1: -windows -linux


Create_KDE_linux_scan_icon() {
  cat > ScanVirus_KDE_Linux_Scan.desktop <<EOF
#!/usr/bin/env xdg-open
[Desktop Entry]
Comment[en_US]=
Comment=
Exec=sh scanvirus -linux
GenericName[en_US]=
GenericName=
Icon=kde
MimeType=
Name[en_US]=ScanVirus - linux anti-virus scan
Name=ScanVirus - linux anti-virus scan
Path=$PATH
StartupNotify=true
Terminal=true
TerminalOptions=\s--noclose
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=
X-KDE-SubstituteUID=false
X-KDE-Username=
X-SuSE-translate=trueEOF
EOF

#set file permissions
chmod 744 ScanVirus_KDE_Linux_Scan.desktop
}


Create_KDE_windows_scan_icon() {
  cat > ScanVirus_KDE_Windows_Scan.desktop <<EOF
#!/usr/bin/env xdg-open
[Desktop Entry]
Comment[en_US]=
Comment=
Exec=sh scanvirus -windows
GenericName[en_US]=
GenericName=
Icon=kde
MimeType=
Name[en_US]=ScanVirus - windows anti-virus scan
Name=ScanVirus - windows anti-virus scan
Path=$PATH
StartupNotify=true
Terminal=true
TerminalOptions=\s--noclose
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=
X-KDE-SubstituteUID=false
X-KDE-Username=
X-SuSE-translate=true
EOF

#set file permissions
chmod 744 ScanVirus_KDE_Windows_Scan.desktop
}



##############################
# shortcut code for clamscan #
############################## 
    if  "$1" == "-windows" ]; then
            echo "Scanning mswin...";  
            clamscan -r /windows -i     #windows only partitions scan (not working)
    elif  "$1" == "-linux" ]; then
            echo "Scanning linux...";  
            #which scans linux only. Both?
            clamscan -r "/" "--exclude-dir=/sys" "--exclude-dir=/proc" "--exclude-dir=/dev" --cross-fs=no -i
           #clamscan -r "/" "--exclude-dir=/sys" "--exclude-dir=/proc" "--exclude-dir=/dev" --follow-dir-symlinks=0  --follow-file-symlinks=0 -i
    elif  "$1" == "-h" ]; then
        echo "";
        echo "Scan Virus";
            echo "help commands";
            echo "-------------";
            echo "-linux:  virus scan linux files";
            echo "-windows:  virus scan windows files";
            echo "--setup:  setup scanvirus";
            echo "--kdeicons:  setup icons";
            echo "";
    elif  "$1" == "--setup" ]; then
            echo "ScanVirus Setup";
            su -c 'zypper --non-interactive install clamav;chkconfig freshclam on;freshclam;exit'      
            read -p "Done. Press any key..." -n1 -s;echo "";
            exit
    elif  "$1" == "--kdeicons" ]; then
            Create_KDE_linux_scan_icon
            Create_KDE_windows_scan_icon    
            echo "KDE Icons Created";
    else
        echo "";
        echo "Scan Virus";
        echo "-------------------";
            echo "-h:  show help file";
        echo "";
    fi
exit 0 
# End Of Script

I made changes to the script. Now the whole won’t work at all. I’m going to need to restart and figure out what’s wrong. Please don’t respond to this thread. Thanks to all.