scanvirus script won't start - opensuse 13.2 - KDE

I’v updated the script for opensuse 13.2 - KDE. I checked the executable flag on the scriptfile. On the command line, “scanvirus” gives “file not found”. It worked without “sh” before. If do “sh scanvirus”, it gives a bunch of errors. I only copied over from new sample code (see other thread) what I could understand. I have advanced programing experience, but not much with linux bash scripts. Example, internet searches gave me the “press any key” script code. Need help here. :stuck_out_tongue:

#!/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

Whitespaces? Tabs? BOM?

I copy pasted it and tested it quickly and saw no issues;

miuku@derpvm:~> ./woot.sh  -h

Scan Virus
help commands
-------------
-linux:  virus scan linux files
-windows:  virus scan windows files
--setup:  setup scanvirus
--kdeicons:  setup icons

Command line:

scanvirus

Should list the help command. It gives file not found. Do I need to add more to the setup commands?

sh scanvirus

Gives script errors(can’t get access now, will post them later).

The script is in ‘home/bin/’

./ (your current directory) is not in the path by default.

So, in order to run a script in your current directory, you’ll need to use: ./woot.sh , if the script is called woot.sh (and it has executable bit set).

Please post the errors here and we’ll see what they are. Or rather someone else will because I’m going to bed soon :wink:

Hi,

There is a difference between POSIX sh and Bash. That is a common pitfall for newbies writing shell scripts. You have the shebang

#!/bin/bash 

and you are running your script with

sh mybashscript

Also if there is no executable scanvirus within your current PATH (the value of echo “$PATH”), you will get the cnf error.
You can check if there is an executable named scanvirus within your PATH when using the bash shell by running

type -a scanvirus

Or with any POSIX compliant shell by running

command -V scanvirus

And please don’t use the which command IMHO it has no place in any POSIX compliant shell. Why you may ask? Good luck finding where is cd located by,

which cd

:stuck_out_tongue:

Now if you want to run the scanvirus that is inside where your current directory is, the value of

echo "$PWD" 

or

pwd

You need to prefix it with a dot and a slash like what Miuku said.

**./**myscript

You don’t need to prefix with sh nor bash if it has a shebang the shebang will determine which interpreter to use. Mind you that the builtin read only has **-r **option specified by POSIX and the other options are just an additional features that was added by bash and other shells.

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

Since you have more options to the builtin read than just -r (among other bashishm features) and calling it your script with

sh mybashscript

Thats why you get the error. One might argue that in openSUSE sh is just a symlink to bash so it should work (Well not according to your errors :slight_smile: )

An aside note you don’t need to add an .sh extension to your script, it will run just fine without it

type -a scanvirus
command -V scanvirus

The file has the executable flag. They both show scanvirus.

Now if you want to run the scanvirus that is inside where your current directory is, the value of

echo "$PWD" 

or

pwd

You need to prefix it with a dot and a slash like what Miuku said.

**./**myscript

scanvirus
: No such file or directory

sh scanvirus
scanvirus: line 2: $’\r’: command not found
scanvirus: line 9: $’\r’: command not found
scanvirus: line 10: $’\r’: command not found
scanvirus: line 11: syntax error near unexpected token $'{\r'' 'canvirus: line 11: Create_KDE_linux_scan_icon() {

./scanvirus
: No such file or directory

You don’t need to prefix with sh nor bash if it has a shebang the shebang will determine which interpreter to use. Mind you that the builtin read only has **-r **option specified by POSIX and the other options are just an additional features that was added by bash and other shells.

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

Since you have more options to the builtin read than just -r (among other bashishm features) and calling it your script with

sh mybashscript

Thats why you get the error. One might argue that in openSUSE sh is just a symlink to bash so it should work (Well not according to your errors :slight_smile: )

An aside note you don’t need to add an .sh extension to your script, it will run just fine without it

Those command were working before I did full reinstall of suse.

Hi,

You seem to have edited your bash script using windows file editor. The

$'\r'

is a windows file ending. A quick fix would be

dos2unix **mybash_script_with_windows_line_endings**

You need the package

dos2unix

I’ll try these corrections tomorrow night. Thanks for the assist.

I want this code to not specific to my file directory structure. Can someone check that?

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


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                                                         #not tested
           #clamscan -r / --exclude-dir=/sys --exclude-dir=/proc --exclude-dir=/dev --follow-dir-symlinks=0  --follow-file-symlinks=0 -i           #seems to work
    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

Hi,

I’m just curious, are you scanning openSUSE with clamav? :wink:

> dos2unix scanvirus
dos2unix: converting file scanvirus to Unix format...

I created a new text file and copied the info over. Then corrected the other errors. I tried this i’m still getting errors.

jetchisel
Hi,

I'm just curious, are you scanning openSUSE with clamav? 

Yes, that what the script is for. It worked fine before all the changes.

I redid the script and it works. Now, I have a whole new set of errors. :stuck_out_tongue:

#!/bin/bash 
#: Title       : scanvirus
#: 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 -mswin
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
}

#pwd

##############################
# shortcut code for clamscan #
############################## 
    if  $1 == '-mswin' ] ||  $1 == '-w' ]; then
          #In KDE, right-click, click on edit and copy and paste the adress for each MSWIN drive you want to scan
          
          set mswin1= 'clamscan -r /run/media/alexr1984/TEMP1';
          #set scanvirus_mswin2= '';
          #set scanvirus_mswin3= '';
          #set scanvirus_mswin4= '';
          #set scanvirus_mswin5= '';
 
          echo 'Scanning mswin...';  
          echo  '$scanvirus_mswin1';
          #read -p 'Done. Press any key...' -n1 -s;echo '';

    elif  $1 == '-linux' ] ||  $1 == '-l' ]; then
            echo 'Scanning linux...';  
            #which scans linux only. Both?
            #clamscan -r / --exclude-dir=/sys --exclude-dir=/proc --exclude-dir=/dev --cross-fs=no  
            #su -c 'freshclam;clamscan -r / --exclude-dir=/sys --exclude-dir=/proc --exclude-dir=/dev --follow-dir-symlinks=0  --follow-file-symlinks=0 --log=scan.log'
            clamscan -r / --exclude-dir=/sys --exclude-dir=/proc --exclude-dir=/dev --follow-dir-symlinks=0  --follow-file-symlinks=0
 
    elif  $1 == '-help' ] ||  $1 == '-h' ]; then
        echo '';
        echo 'Scan Virus';
        echo 'help commands';
        echo '-------------';
        echo '-linux or -l:  virus scan linux files';
        echo '-mswin or -w:  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';
            
    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 want to make it easy for users to simply fill in windows directory on the mswin lines. I can’t get the varibles to work. Need help.

clamscan -r [mswin1 var]
clamscan -r [mswin2 var]
clamscan -r [mswin3 var]
clamscan -r [mswin4 var]
clamscan -r [mswin5 var]

>scanvirus
scanvirus: line 75: : ==: unary operator expected
scanvirus: line 75: : ==: unary operator expected
scanvirus: line 88: : ==: unary operator expected
scanvirus: line 88: : ==: unary operator expected
scanvirus: line 95: : ==: unary operator expected
scanvirus: line 95: : ==: unary operator expected
scanvirus: line 106: : ==: unary operator expected
scanvirus: line 110: : ==: unary operator expected

Scan Virus
-------------------
-h:  show help file


I can’t figure what’s causing this.

Hi,

In bash use for testing, now since i don’t think i can convince you otherwise just quote your variables use

"$1"

and not

$1

The quotes are not just for decoration, they are vital to shell scripts. Also POSIX sh does not know == only = inside the **** , Again some one might argue that since sh is just a symlink to bash in openSUSE it should just work.

When two executable file are hard or symlinks of each other, that does NOT mean that they will behave the same. The program will have argument 0 and that will tell it how it is called and it may behave accordingly. Calling the program as bash will have different results from calling the same program as sh (not much, but there are differences, and of course allways at those points onne, as a human being, did not think about).

Another example:

boven:~ # ls -l /bin | grep 'vim'
lrwxrwxrwx 1 root root       3 Apr 11  2014 ex -> vim
lrwxrwxrwx 1 root root       3 Apr 11  2014 vi -> vim
-rwxr-xr-x 1 root root 2396872 Apr  3  2014 vim
boven:~ #

Whne you call ex the results will be different from when you call vi and that will again be different when you cal vim.

Which set of quotes do I use for scripts? ’ or " Single or Double?

Assignments in programing are ‘=’ and if statement use ‘==’. For bash scripts, I use single ‘=’ for ‘if’ statements. Right?

set var1= ‘somestring’. Right?

Should I use ‘sh scanvirus’ or ‘scanvirus’ within the shortcut code? They give the same result on the command line.

#!/bin/bash 
#: Title       : scanvirus
#: 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

#Place this file inside '/home/bin/'.


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 -mswin
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
}

#pwd

##############################
# shortcut code for clamscan #
############################## 
    if  $1 = '-mswin' ] ||  $1 = '-w' ]; then
          #In KDE, right-click, click on edit and copy and paste the adress for each MSWIN drive you want to scan
          
          set scanvirus_mswin1= '';
          #set scanvirus_mswin2= '';
          #set scanvirus_mswin3= '';
          #set scanvirus_mswin4= '';
          #set scanvirus_mswin5= '';
 
          echo 'Scanning mswin...';  
          if [scanvirus_mswin1 != '']
               clamscan -r '$scanvirus_mswin1';
          fi

          #read -p 'Done. Press any key...' -n1 -s;echo '';

    elif  $1 = '-linux' ] ||  $1 = '-l' ]; then
            echo 'Scanning linux...';  
            #which scans linux only. Both?
            #clamscan -r / --exclude-dir=/sys --exclude-dir=/proc --exclude-dir=/dev --cross-fs=no  
            #su -c 'freshclam;clamscan -r / --exclude-dir=/sys --exclude-dir=/proc --exclude-dir=/dev --follow-dir-symlinks=0  --follow-file-symlinks=0 --log=scan.log'
            clamscan -r / --exclude-dir=/sys --exclude-dir=/proc --exclude-dir=/dev --follow-dir-symlinks=0  --follow-file-symlinks=0
 
    elif  $1 = '-help' ] ||  $1 = '-h' ]; then
        echo '';
        echo 'Scan Virus';
        echo 'help commands';
        echo '-------------';
        echo '-linux or -l:  virus scan linux files';
        echo '-mswin or -w:  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';
            
    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

Through some educated guesses, I managed to fix it. Thanks to all. This is topic is closed.