-2

I received a advice for do not parse ls, like describes in this website: Don't parse ls.
I was looking for DAILY files in my directory so that's what I did then:

for f in *.DA*; do   
    [[ -e $f ]] || continue 
    for file in $f; do  
        echo "The file that you are working on: "$file
        archiveContent=$( sed -n -e 1p $file )
        echo $archiveContent
    done
done  

Ok, that's works well, I've two files A.DAILY and B.DAILY, with the both archives I can get what is inside it, but when I changed a little bit the loop, it doesn't iterated with all files with .DAILY extension in my directory.

for f in *.DA*; do     
    [[ -e $f ]] || continue     
    for file in $f; do    
        echo "The file that you are working on: "$file    
        archiveContent=$( sed -n -e 1p $file )
        echo $archiveContent  
        COMPRESS $archiveContent;    
    done  
done  

when I called a function inside the loop, the loop just does for the first file, but not to the second.

ranu
  • 622
  • 15
  • 25
  • `$f` is a single file name, so the inner loop only has one iteration, where `$file` has the same value as `$f`. – chepner Oct 24 '13 at 21:23
  • what you suggest to do? I thought that the loop, iterate along the files, because, without the comumand COMPRESS $archiveContent, it shows to me: `The file that you are working on: A.DAILY the content of the A.DAILY The file that you are working on: B.DAILY the content of the B.DAILY` – ranu Oct 24 '13 at 23:57
  • Drop the inner loop? It's not serving any purpose. – chepner Oct 25 '13 at 01:04
  • If your script is outputting `The file that you are working on: B.DAILY`, then the loop is doing the second file. What output are you expecting, and what output are you getting? – Mark Plotnick Oct 25 '13 at 01:58
  • I'm getting the output that i wanted, **IF** i comment the `COMPRESS` function, if not, the loop just works on the first file. – ranu Oct 25 '13 at 03:23
  • There is no question in this question. – ADJenks May 28 '21 at 21:14
  • Well, this questions was asked quite a long time ago and I'm not working with that specific problem anymore. I can presume from the question that the `COMPRESS` function is to blame but I don't have that source code anymore. – ranu May 31 '21 at 18:32

1 Answers1

2

Since the outer loop sets f to each file in turn, your inner loop doesn't seem to serve any purpose.

for f in *.DA*; do
    [[ -e $f ]] || continue     
    echo "The file that you are working on: $f"    
    archiveContent=$( sed -n -e 1p "$f" )
    echo "$archiveContent"  
    COMPRESS "$archiveContent"
done  
chepner
  • 497,756
  • 71
  • 530
  • 681
  • doesn't work, the problem perhaps is related to the function `COMPRESS`, that takes the argument and then search for one archive and compress, when it's done, nothing to do anymore... So, in that loop, with the `#COMPRESS` commented it works like the other one that i've posted... – ranu Oct 25 '13 at 03:15
  • If you think the problem is with `COMPRESS`, perhaps you might share what it does. – chepner Oct 28 '13 at 13:40
  • The compress function does that: `COMPRESS() { #this function make the compress of the each image tar -czf ${imageName-$date}.tar.gz ${imageBasename} 2>>$ERRORLOG if [ $? -eq 0 ] then echo "success compress of the file: ${imageBasename}" else echo "Some errors appears in the script, see the log file for more description" fi` Sorry for how the comment code looks like, i tried to format, and also i cut a bunch of lines of the `COMPRESS` function... – ranu Oct 28 '13 at 16:06
  • Don't put so much code in comments; edit your question to include the code instead. That said, I think you just need to fix your syntax: `tar -czf "${imageName}-$date.tar.gz" "$imageBasename"`. `${imageName-$date}` would expand to the value of `$date` if `$imageName` was the empty string. You have the colon-free version of the Use Default Value parameter expansion operator. – chepner Nov 01 '13 at 22:08
  • The $imageName is never empty @chepner, I have sure of it. And fixing my syntax didn't worked at all, well, I tried something else, i made other script, where it reads the content of the two archives with the `.DAILY` extension, inside this archives, I have a directory, where I take this directory and them I make the script enter on this directory. Before I make the command e.g `cd $dir` where the dir is the directory variable extracted from the file, it doesn't make the loop of the two archives, just of one! So i think that the script just will make something if it inside the same directory. – ranu Nov 05 '13 at 17:56