Loop Script and not opening files containing spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop Script and not opening files containing spaces
# 1  
Old 09-25-2017
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
Code:
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 "" ?

Code:
#!/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"

# 2  
Old 09-25-2017
Welcome to the forum.


Quote:
Originally Posted by blend_in
.
.
.
It worked before and i changed nothing in the script,
.
.
.
... a bit difficult to believe ...

Quote:
but since last week i have the problem, that it doesnt extract files containing a space
.
.
.
for file in $(ls $DOWNLOAD_DIR/*.dat)
double quote the "command substitution", or replace by just for file in $DOWNLOAD_DIR/*.dat

Code:
        if [ -f $file 0 ]

can't possibly work, and never has - it yields an error message like
Code:
bash: [: ./file: binary operator expected

The
Code:
$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 echoes 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.
- echoing 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?


Quote:
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 "" ?
For this problem you seem to already have found and implemented a solution - what else do you need?
# 3  
Old 09-25-2017
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.

Code:
for file in $DOWNLOAD_DIR/*.dat
do
        echo "file is $file"
done

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 09-26-2017
Quote:
Originally Posted by Corona688
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.

Code:
for file in $DOWNLOAD_DIR/*.dat
do
        echo "file is $file"
done

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:

Code:
shopt -s nullglob

which would change say
Code:
echo *.dat *.png *.gif
abc.dat def.dat *.png fff.gif

into
Code:
echo *.dat *.png *.gif
abc.dat def.dat fff.gif

Andrew
# 5  
Old 09-26-2017
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:

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

        (rest of code)
done

One less level of nesting is usually a good thing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Process files in loop which have spaces in name

I have a folder with files and I have to process them in a loop. However the filenames have space characters, so the list get split. $ touch "File Number_1" $ touch "File Number_2" $ ls "/tmp/File Number"_* /tmp/File Number_1 /tmp/File Number_2 I tried following (sorry for using the... (3 Replies)
Discussion started by: Wernfried
3 Replies

2. Shell Programming and Scripting

Script to loop through the files

Hi Experts, Need some help regarding a requirement -- I may get any number of zipped files in a month (max 36). each of those wil contain a detail file and a header file. Also when the files arrive, there will already be previous days file lying around. What I am trying to do is count the... (5 Replies)
Discussion started by: ashkul123
5 Replies

3. UNIX for Dummies Questions & Answers

Copying files with spaces in the filename in a for loop

Hi all, I've been tangoing with this one for a couple of days now and I'm still not making any progress. Basically I'm trying to match three numbers in a string from a text file with matching numbers in a jpeg, and then copying the results to another folder. Data looks like this: Model:... (4 Replies)
Discussion started by: faceonline
4 Replies

4. Shell Programming and Scripting

Opening File names with spaces inside it !- PERL

I developed a perl code..And the excerpt from it is given below... open(HANDLE,$cmp_path) ; #reading the xml file from the file path while($file_path = <HANDLE>) I have list of XML files to read from a folder. It has some spaces inside the name of the file...I used "\"... (2 Replies)
Discussion started by: gameboy87
2 Replies

5. Shell Programming and Scripting

for loop ( string having spaces )

Dear All, i facing problem to use string having spaces in for loop.. file used for FOR LOOP command.txt rpm -t -v ttm -D -r RJLL -h YELP rpm -t -v ttm -D -r RJLL -h ERRT rpm -t -v ttm -D -r RJLL -h TYYE rpm -t -v ttm -D -r RJLL -h POOL CODE using for execute above command... (3 Replies)
Discussion started by: arvindng
3 Replies

6. Shell Programming and Scripting

problem with for loop and a variable with spaces...Hi

Hi there, I don't understand the following behavior: toto:~$ for word in un "deux trois"; do echo $word; done un deux trois toto:~$ sentence='un "deux trois"' toto:~$ for word in $sentence; do echo $word; done un "deux trois" toto:~$ sentence="un 'deux trois'" toto:~$ for word in... (10 Replies)
Discussion started by: chebarbudo
10 Replies

7. Shell Programming and Scripting

Script to find folders with spaces and end of files and directories

Hi I need a script that can search through a set of directories and can locate any file or directory that has a space at the end Filename(space) Foldername(space) I then need to remove that space within the script Hope someone can help thanks in advance Treds (8 Replies)
Discussion started by: treds
8 Replies

8. Shell Programming and Scripting

Opening Mulitple files using For loop in Perl

Hi All, I have a total of ten file to open in the Perl script and i am using a for loop to open each file and capture some strings inside each file. Unfortunately, i encounter the below syntax error. I think there should be something wrong with this term reports_${counting}_${_}.txt but i do... (4 Replies)
Discussion started by: Raynon
4 Replies

9. UNIX for Dummies Questions & Answers

Opening files

I am very new to unix. I want to open a file and read one line in at a time. Can anybody help? (3 Replies)
Discussion started by: saarshad001
3 Replies

10. UNIX for Dummies Questions & Answers

Opening Files

I'm a new to UNIX/LINUX. I just put cygwin on my laptop and I can navigate around the directories, but I can't open files (.doc, .ppt, .html or .exe). Is there an explicit command to do this? I know that in Solaris when it does not recognize the file, it brings up the list of available viewing... (4 Replies)
Discussion started by: AJA
4 Replies
Login or Register to Ask a Question