Sponsored Content
Top Forums Shell Programming and Scripting Loop Script and not opening files containing spaces Post 303004001 by blend_in on Monday 25th of September 2017 05:08:17 AM
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"

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 03:16 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy