Loop Script and not opening files containing spaces

Hello,

I wrote a simple script, that basically wait for a *.dat-file in a certain folder, which is always a zipped file and extracts it.

It worked before and i changed nothing in the script, but since last week i have the problem, that it doesnt extract files containing a space. How do i make it also extract files containing spaces again?
If found something with

IFS="$IFS"
IFS=$'\n'

but it doesnt seem to work.

and another problem i have is, i am looking for pictures in the extracted files, but if no .png or .jpg files are found it still opens the literal ".jpg" and ".png"-files in sublime, how can i make it, that if no pictures are found, it doesnt open that file or sets "${BILDER_JPG[@]}" "${BILDER_PNG[@]}" to "" ?

#!/bin/bash

#for handling spaces in filenames
IFS="$IFS"
IFS=$'\n'


DOWNLOAD_DIR=~/Downloads/neu
DSM=dsm

while true;
do
    for file in $(ls $DOWNLOAD_DIR/*.dat)
    do
        if [ -f $file 0 ]
        then
            DATE=$(date +"%H_%M_%S")
            unzip -q {$file} -d $DOWNLOAD_DIR/debug_$DATE
            if [ $? -eq 0 ] # remove if successfully extracted
            then
            	mv $file $DOWNLOAD_DIR/debug_$DATE
            	#ln -s $DOWNLOAD_DIR/debug_$DATE/dsm/var/log/messages $DOWNLOAD_DIR/debug_$DATE/messages
            	echo $file " erfolgreich entpackt."
            	echo "DOWNLOAD_DIR: " $DOWNLOAD_DIR
				if [ -f $DOWNLOAD_DIR/debug_$DATE/packages.list ]
				then	DSM=""
				fi
            	DEBUG_DIR=$DOWNLOAD_DIR/debug_$DATE
                if [ -f $DOWNLOAD_DIR/debug_$DATE/$DSM/proc/mdstat ]
                then    MDSTAT=$DOWNLOAD_DIR/debug_$DATE/$DSM/proc/mdstat
                fi
                if [ -f $DOWNLOAD_DIR/debug_$DATE/$DSM/result/df.result ]
                then    DF=$DOWNLOAD_DIR/debug_$DATE/$DSM/result/df.result
                fi
                if [ -f $DOWNLOAD_DIR/debug_$DATE/$DSM/result/ifconfig.result ]
                then    IFCONFIG=$DOWNLOAD_DIR/debug_$DATE/$DSM/result/ifconfig.result
                fi
                for file in $(ls $DOWNLOAD_DIR/debug_$DATE/$DSM/result/smart*.result)
    			do
                    echo $file >> $DOWNLOAD_DIR/debug_$DATE/$DSM/smart_grep.result
                    echo " "  >> $DOWNLOAD_DIR/debug_$DATE/$DSM/smart_grep.result
                    awk '/SMART Error Log Version: 1/{f=1;next} /Selective self-test flags/{f=0} f' $file >> $DOWNLOAD_DIR/debug_$DATE/$DSM/smart_grep.result
    				echo " "  >> $DOWNLOAD_DIR/debug_$DATE/$DSM/smart_grep.result
                    echo " "  >> $DOWNLOAD_DIR/debug_$DATE/$DSM/smart_grep.result
                    echo " "  >> $DOWNLOAD_DIR/debug_$DATE/$DSM/smart_grep.result
                    echo " "  >> $DOWNLOAD_DIR/debug_$DATE/$DSM/smart_grep.result
$DOWNLOAD_DIR/debug_$DATE/$DSM/smart_grep.result
    			done
    			declare -a SMART_FILES
    			SMART_FILES=( "${DOWNLOAD_DIR}/debug_${DATE}/${DSM}/result/smart"*.result )
                if [ -f $DOWNLOAD_DIR/debug_$DATE/$DSM/smart_grep.result ]
                then    SMART_GREP=$DOWNLOAD_DIR/debug_$DATE/$DSM/smart_grep.result
                fi
                if [ -f $DOWNLOAD_DIR/debug_$DATE/$DSM/var/log/messages ]
                then    MESSAGES=$DOWNLOAD_DIR/debug_$DATE/$DSM/var/log/messages
                fi
                declare -a BILDER_JPG
                for file in "${DOWNLOAD_DIR}/debug_${DATE}/"*.jpg; do
  					[[ -f "${file}" ]] # Do something with this
  					BILDER_JPG+=( "${file}" )
				done
    			alt: BILDER_JPG=( "${DOWNLOAD_DIR}/debug_${DATE}/"*.jpg )
    			declare -a BILDER_PNG
    			for file in "${DOWNLOAD_DIR}/debug_${DATE}/"*.png; do
  					[[ -f "${file}" ]] # Do something with this
  					BILDER_PNG+=( "${file}" )
				done
				#echo "JPG: " $BILDER_JPG
				#echo "PNG: " $BILDER_PNG
    			#alt: BILDER_PNG=( "${DOWNLOAD_DIR}/debug_${DATE}/"*.png )
    			#echo "BILDER_PNG Inhalt: " $BILDER_PNG
                subl $DEBUG_DIR ${SMART_FILES[@]} $SMART_GREP $MDSTAT $DF $IFCONFIG $MESSAGES:100000 "${BILDER_JPG[@]}" "${BILDER_PNG[@]}"
                # ${SMART_FILES[@]} "${BILDER_JPG[@]}" "${BILDER_PNG[@]}"
    		else
    			mkdir $DOWNLOAD_DIR/kapott
    			mv $file $DOWNLOAD_DIR/kapott/debug_$DATE.dat
    			echo $file "konnte nicht entpackt werden."
    			zenity --error --text="Debug konnte nicht entpackt werden\!" --title="Achtung!"
    			
            fi
        fi
    done
    sleep 5
done
IFS="$OIFS"

Welcome to the forum.

... a bit difficult to believe ...

double quote the "command substitution", or replace by just for file in $DOWNLOAD_DIR/*.dat

        if [ -f $file 0 ]

can't possibly work, and never has - it yields an error message like

bash: [: ./file: binary operator expected

The

$DOWNLOAD_DIR/debug_$DATE/$DSM/smart_grep.result

command doesn't exist and leads to an error message.
The alt: command doesn't exist and leads to an error message.

So far for the obvious. Some more comments:

  • your indentation of the script doesn't help in understanding but obfuscates it.
  • Using date by the second for creating temp files / directories you might be pushing your luck as the loop might finish in below a second leading to ambiguities.
  • Above remark on "command subst" is valid for the other for loops in your script as well.
  • appending echo es to a log file umpteen times in a for loop is far less efficient than redirecting the entire loop's output once only at the end of the loop.
  • echo ing four times for four line feeds is less efficient than writing four line feeds in one single echo - or, even better, print all line feeds within the awk script. Did you consider removing the entire for loop in favor of one single awk script?

For this problem you seem to already have found and implemented a solution - what else do you need?

Here you have a Useless Use of ls *, first I've seen in a while.

If you remove the ls, the loop will work just fine.

for file in $DOWNLOAD_DIR/*.dat
do
        echo "file is $file"
done
1 Like

I imagine blend_in was using ls *.dat to get rid of a stray *.dat if there are no .dat files in the directory. Given that you are using bash in this case, adding the following line to the top of the script will not only avert the need for the ls , but help with the problem of stray *.png non-files mentioned later in the original post:

shopt -s nullglob

which would change say

echo *.dat *.png *.gif
abc.dat def.dat *.png fff.gif

into

echo *.dat *.png *.gif
abc.dat def.dat fff.gif

Andrew

Except it's not. He already has an if [ -f file ] to handle that situation. Which he made [ -f file 0 ] in case file happened to be blank.

I'd suggest a plainer / shorter:

for X in file
do
        [ -f "$file" ] || continue

        (rest of code)
done

One less level of nesting is usually a good thing.