Bash scripting, reading files

Hello all. This script works as designed. I want to add reading a data file to it.

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


#Open bin folder
#Place this file inside "/home/bin/"
#
#Right-click on scanvirus
#Click on properties
#Click on permissions
#Check executable and click ok 
#
#Click Control -> Tools -> Open Terminal
#Enter termal command:  scanvirus --setup
#Enter admin password, wait for it to finish. Don't close terminal.
#Enter termal command:  scanvirus --kdeicons
#close terminal windows
#
#In KDE, right-click, click on edit and copy and paste the adress for each MSWIN drive you want to scan     
#scanvirus_mswin1="/pathto/mswin/drive/"
scanvirus_mswin1="/run/media/alexr1984/TEMP1"
scanvirus_mswin2=""
scanvirus_mswin3=""
scanvirus_mswin4=""
scanvirus_mswin5=""
#
#Click on either desktop icon to scan

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
#if  $1 = "-mswin" -o $1 = "-w" ]; then    

##############################
# shortcut code for clamscan #
############################## 
    if  "$1" = "-mswin" ] ||  "$1" = "-w" ]; then
    
          echo "scanvirus mswin"
          echo "---------------"

          if  "$scanvirus_mswin1" != "" ]; then
            echo "scanning $scanvirus_mswin1"
            clamscan -r "$scanvirus_mswin1"
            echo ""
          fi

          if  "$scanvirus_mswin2" != "" ]; then
            echo "scanning $scanvirus_mswin2";
            clamscan -r "$scanvirus_mswin2" -i 
            echo ""
          fi

          if  "$scanvirus_mswin3" != "" ]; then
            echo "scanning $scanvirus_mswin3";
            clamscan -r "$scanvirus_mswin3" -i
            echo ""
          fi

          if  "$scanvirus_mswin4" != "" ]; then
            echo "scanning $scanvirus_mswin4";
            clamscan -r "$scanvirus_mswin4" -i
            echo ""
          fi

          if  "$scanvirus_mswin5" != "" ]; then
            echo "scanning $scanvirus_mswin5";
            clamscan -r "$scanvirus_mswin5"
          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


To make it easier to use, I want to read in the paths the user has in a file.


scanvirus_mswinpaths="scanvirus_mswin_paths"

##############################
#setup
if  -r $scanvirus_mswinpaths ] &&  -w $scanvirus_mswinpaths ]; then
   echo "scanvirus_mswin_paths file exists"
else
   echo "Creating blank file scanvirus_mswin_paths. Open file and paste in mswin paths."
   echo "/pathto/mswin/drive1/"
   echo "/pathto/mswin/drive2/"
   echo "/pathto/mswin/drive3/"
   cat > scanvirus_mswin_paths <<EOF
EOF

#set file permissions
chmod 744 scanvirus_mswin_paths

fi
###############################

#read file path line by line and scan
while read line
do
   if  "$line" != "" ]; then
     echo "scanning  $line"
     clamscan -r "$line" -i
     echo ""
   fi
done
#end

Is this correct? Thanks.

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


#Open bin folder
#Place this file inside "/home/bin/"
#
#Right-click on scanvirus
#Click on properties
#Click on permissions
#Check executable and click ok 
#
#Click Control -> Tools -> Open Terminal
#Enter termal command:  scanvirus --setup
#Enter admin password, wait for it to finish. Don't close terminal.
#Enter termal command:  scanvirus --kdeicons
#close terminal windows
#
#Click on either desktop icon to scan


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
#if  $1 = "-mswin" -o $1 = "-w" ]; then    

##############################
# shortcut code for clamscan #
##############################

scanvirus_mswin_paths="scanvirus_mswin_paths"

    if  "$1" = "-mswin" ] ||  "$1" = "-w" ]; then
        echo ""
        echo "scanvirus mswin"
        echo "---------------"

        #if  -f "$scanvirus_mswinpaths" ]; then
        if  -f  scanvirus_mswin_paths ]; then
          echo ""
        else
          echo "scanvirus_mswin_paths not found. Run setup"
          echo ""
          exit
        fi  
          
          #read file path line by line and scan
          while read line
          do
            if  "$line" != "" ]; then
              echo "scanning $line"
              clamscan -r "$line" -i
              echo ""
            fi
          done < "scanvirus_mswin_paths"     
          #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 "";
        echo "ScanVirus Setup";
        #su -c "zypper --non-interactive install clamav;chkconfig freshclam on;freshclam;exit";
 
        #if  -f "$scanvirus_mswinpaths" ]; then
        if  -f  scanvirus_mswin_paths ]; then
          echo "scanvirus_mswin_paths file exists"
        else
          echo "Creating blank file scanvirus_mswin_paths. Open file edit mswin paths."
          #In KDE, right-click, click on edit and copy and paste the adress for each MSWIN drive you want to scan

#create file
cat > scanvirus_mswin_paths <<EOF
/pathto/mswin/drive1/
/pathto/mswin/drive2/
/pathto/mswin/drive3/
EOF
#done

          fi
          echo "";

    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


This works, but doesn’t use a string for the filename. I want to be able to change the filename in a string, but if try to use that string. It always reads as file not found. Having the same name for both the string varname and string. Shouldn’t be causing a problem, but i’v changed it anyway. Please tell me if I have this right. Why does the file check fail when I use a string var?

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


#Open bin folder
#Place this file inside "/home/bin/"
#
#Right-click on scanvirus
#Click on properties
#Click on permissions
#Check executable and click ok 
#
#Click Control -> Tools -> Open Terminal
#Enter termal command:  scanvirus --setup
#Enter admin password, wait for it to finish. Don't close terminal.
#Enter termal command:  scanvirus --kdeicons
#close terminal windows
#
#Click on either desktop icon to scan


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
#if  $1 = "-mswin" -o $1 = "-w" ]; then    

#####################
# shortcut code for clamscan #
#####################

scanvirus_mswin_paths_data="scanvirus_mswinpaths"

    if  "$1" = "-mswin" ] ||  "$1" = "-w" ]; then
        echo ""
        echo "scanvirus mswin"
        echo "---------------"

        if  -f "$scanvirus_mswin_paths_data" ]; then
          echo ""
        else
          echo "scanvirus_mswin_paths not found. Run setup"
          echo ""
          exit
        fi  
          
          #read file path line by line and scan
          while read line
          do
            if  "$line" != "" ]; then
              echo "scanning $line"
              clamscan -r "$line" -i
              echo ""
            fi
          done < "$scanvirus_mswin_paths_data"     
          #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 "";
        echo "ScanVirus Setup"
        #su -c "zypper --non-interactive install clamav;chkconfig freshclam on;freshclam;exit"
 
        if  -f "$scanvirus_mswin_paths_data" ]; then
          echo "scanvirus_mswin_paths file exists"
        else
          echo "Creating blank file scanvirus_mswin_paths. Open file edit mswin paths."
          echo "In KDE, right-click on the drive, click on edit and copy and paste the adress for each MSWIN drive you want to scan."

#create file
cat > scanvirus_mswin_paths <<EOF
/pathto/mswin/drive1/
/pathto/mswin/drive2/
/pathto/mswin/drive3/
EOF
#done

          fi
          echo "";

    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,

You can always put

set -x

Just below the shebang and run your script to see what is really executed and why does it give the error.

also

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 "";

Can be done using the cat and HEREDOC trick (EOF)

cat <<EOF


Scan Virus
help commands
-------------
-linux or -l:  virus scan linux files
-mswin or -w:  virus scan windows files
--setup:  setup scanvirus
--kdeicons:  setup icons


EOF

but since you really want to use the echo then try this.

echo "
Scan Virus
help commands
-------------
-linux or -l:  virus scan linux files
-mswin or -w:  virus scan windows files
--setup:  setup scanvirus
--kdeicons:  setup icons
"

Also add the -r option to read. see

help read

to find out why.

Thanks for the info. :slight_smile:

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


#Open bin folder
#Place this file inside "/home/bin/"
#
#Right-click on scanvirus
#Click on properties
#Click on permissions
#Check executable and click ok
#
#Click Control -> Tools -> Open Terminal
#Enter termal command:  scanvirus --setup
#Enter admin password, wait for it to finish. Don't close terminal.
#Enter termal command:  scanvirus --kdeicons
#close terminal windows
#
#Click on either desktop icon to scan


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
#if  $1 = "-mswin" -o $1 = "-w" ]; then   
          #which scans linux only. Both?
          #clamscan -r / --exclude-dir=/sys --exclude-dir=/proc --exclude-dir=/dev --cross-fs=no 

#####################
# shortcut code for clamscan #
#####################

scanvirus_mswin_paths_data="scanvirus_mswinpaths"

    if  "$1" = "-mswin" ] ||  "$1" = "-w" ]; then
        echo ""
        echo "scanvirus mswin"
        echo "---------------"

        if  -f "$scanvirus_mswin_paths_data" ]; then
          echo ""
        else
          echo "scanvirus_mswin_paths not found. Run setup"
          echo ""
          exit
        fi 
         
          #read file path line by line and scan
          while read line
          do
            if  "$line" != "" ]; then
              echo "scanning $line"
              clamscan -r "$line" -i
              echo ""
            fi
          done < "$scanvirus_mswin_paths_data"    
          #read -p "Done. Press any key..." -n1 -s;echo "";

    elif  "$1" = "-linux" ] ||  "$1" = "-l" ]; then
        echo "Scanning linux..."; 
        #su -c "freshclam;clamscan -r / --exclude-dir=/sys --exclude-dir=/proc --exclude-dir=/dev --follow-dir-symlinks=0  --follow-file-symlinks=0"
         clamscan -r / --exclude-dir=/sys --exclude-dir=/proc --exclude-dir=/dev --follow-dir-symlinks=0  --follow-file-symlinks=0
         #clamscan -r / --exclude-dir=/sys --exclude-dir=/proc --exclude-dir=/dev --follow-dir-symlinks=0  --follow-file-symlinks=0 --log=scan.log

    elif  "$1" = "-help" ] ||  "$1" = "-h" ]; then
        echo "
Scan Virus
help commands
-------------
-linux or -l:  virus scan linux files
-mswin or -w:  virus scan windows files
--setup:  setup scanvirus
--kdeicons:  setup icons
        "           

    elif  "$1" = "--setup" ]; then
        echo "";
        echo "ScanVirus Setup"
        #su -c "zypper --non-interactive install clamav;chkconfig freshclam on;freshclam;exit"
 
        if  -f "$scanvirus_mswin_paths_data" ]; then
          echo "$scanvirus_mswin_paths_data file exists"
          echo "----------------------------------------"
          #read file path line by line and scan
          while read line
          do
            if  "$line" != "" ]; then
              echo "scanning $line"
              clamscan -r "$line" -i
              echo ""
            fi
          done < "$scanvirus_mswin_paths_data"

        else
          echo "Creating blank file $scanvirus_mswin_paths_data. Open file edit mswin paths."
          echo "In KDE, right-click on the drive, click on edit and copy and paste the adress for each MSWIN drive you want to scan."

#create file
cat > "$scanvirus_mswin_paths_data" <<EOF
/pathto/mswin/drive1/
/pathto/mswin/drive2/
/pathto/mswin/drive3/
EOF
#done

          fi
          echo "";

    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


The mswin paths file is setup with correct format to use. I copied directory paths from mswin partitions in correctly into mswin paths. Command output reports after “file not found” and zero files scanned, but “$line” is a directory path. The same happened with the other two directory paths. The name comes out right when it’s printed. Does this have to do with dirname spaces? I know at least one has spaces.

If I understand correctly, this might fix it, adding single quotes. If not, what might the problem? There is also a command called ‘eval’ I can use. Need help.


clamscan -r '"$line"' -i

Hi,

Single quote will not expand the variables, IIRC someone already pointed out that to you in this forum. You can always read the Quoting section of the bash manual.

PAGER='less +/^Quoting' man bash

Also spaces in the PATHNAME will be a problem if you do not quote your variable but look up that man page to see which quotes you need and don’t forget the

\r

Which is the Windows line endings if you are copying from a Windows machine and pasting it in your suse box.

#read file path line by line and scan
while read line
do
   if  "$line" != "" ]; then
       echo "scanning $line"
       clamscan -r "$line" -i
       echo ""
   fi
done < "$scanvirus_mswin_paths_data"    

The sample file is created by the script. I copied the names from opensuse - kde - filemanger into scanvirus_mswinpaths.

Double quotes inside singles quotes: ‘"$line"’ expands to ‘pathname(with spaces)’. Now, you have ‘pathname with spaces’. However, this didn’t work.

The last pathname in mswin paths has no spaces and should work but doesn’t. (Don’t have the starting path info handy.) root path> [path]/Temp1

http://www.tldp.org/LDP/abs/html/quoting.html
http://www.tldp.org/LDP/abs/html/varsubn.html
http://bash.cyberciti.biz/guide/Variables
http://bash.cyberciti.biz/guide/Quoting
http://linuxreviews.org/beginner/Bash-Scripting-Introduction-HOWTO/en/x555.html

Do I need to include the ending ‘/’ in the pathname? What is wrong with the above code? The path prints right. But, it says “file not found”, but it’s a directory name.

Through some educated guesses, I’ve been able to get it working. I just need to do some additional coding to get it more user friendly. This current problem seems to be fixed. Thanks to all.

Hi,

First you can get rid of the if-then-fi clause since test or is not part of it.

First example.

while read -r line; do  -n $line ]] && echo "$line"; done < <(printf '%s
' "" foo "" bar "" baz "" more)

But -n is not even needed inside

while read -r line; do  $line ]] && echo "$line"; done < <(printf '%s
' "" foo "" bar "" baz "" more)

The above code is bashism, a more portable way would be

while read -r line; do  -n "$line" ] && echo "$line"; done < <(printf '%s
' "" foo "" bar "" baz "" more)

Or using case

while read -r line; do case $line in '') continue;; esac; echo "$line"; done < <(printf '%s
' "" foo "" bar "" baz "")

If you want to have a single quotes around white spaces then use printf rather than echo

while read -r line; do  -n "$line" ] && printf "'%s'
" "$line"; done < <(printf '%s
' "" 'f o o' "" 'b a r' "" 'b a z' "")
while read -r line; do case $line in '') continue;; esac; printf "**'**%s**'**
" "$line"; done < <(printf '%s
' "" 'f o o' "" 'b a r' "" 'b a z' ""

or printf '%q

while read -r line; do  -n "$line" ] && printf "%q
" "$line"; done < <(printf '%s
' "" 'f o o' "" 'b a r' "" 'b a z' "" more)

see

help printf | grep -F '%q'

Thanks for the info. It’s filed away for reference.

FYI, I just ran a full test of improved scanvirus script. It scanned all mswin directories as designed. It even found a virus on two copies of winrar64 which I promptly deleted. Now, I have completely different problem, superuser permissions. I need to start over with a new topic. I need to examine and define the problem before I will post.