LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   problem using gunzip within a script (https://www.linuxquestions.org/questions/linux-newbie-8/problem-using-gunzip-within-a-script-910009/)

danishgambit 10-25-2011 05:05 AM

problem using gunzip within a script
 
Morning,

got a small script to uncompress and untar a bunch of files. The command works when I run it against 1 file on the command line, but when I stick it in a script in a for loop, it doesn't work. I've got


for file in `ls *.bd.tar.gz`
do
gzip -dc $file | tar xvf -
done

It's probably something obvious, but I can't see what it is. Any suggestions would be welcome.

colucix 10-25-2011 05:24 AM

What does "it doesn't work" mean? Any error message? Unexpected behaviour? Your script works fine for me, but I wonder what prevents you from using
Code:

tar xvf $file
instead of the gzip method. GNU tar should automatically take care of the compression (without specifying any additional option). Another note is about parsing the output of ls, which is not advisable for reasons expressed here.

crts 10-25-2011 05:34 AM

Quote:

Originally Posted by danishgambit (Post 4507513)
Morning,

got a small script to uncompress and untar a bunch of files. The command works when I run it against 1 file on the command line, but when I stick it in a script in a for loop, it doesn't work. I've got


for file in `ls *.bd.tar.gz`
do
gzip -dc $file | tar xvf -
done

It's probably something obvious, but I can't see what it is. Any suggestions would be welcome.

Hi,

I can also verify that the script works. So my guess is that your filenames might contain spaces. The for-loop will not process them properly in this case. That is why you should avoid parsing 'ls'. Try this instead:
Code:

for file in *.bd.tar.gz
do
  gzip -dc "$file" | tar xvf -
done


danishgambit 10-25-2011 05:36 AM

Morning,

yep, sorry about the lack of information re: the errors in the original post.
Getting 2 errors for each $file:

e.g. with a filename of jobs.bd.tar.gz

gzip: jobs.bd.tar.gz.gz: No such file or directory
tar: blocksize = 0

The gzip error message looks like it's appending an extra ".gz" to the end of the file name,
and the fact the .gz.gz file doesn't exist probably explains the 2nd message.

Hope this helps.

Regards,

DG.

danishgambit 10-25-2011 05:41 AM

solved - thanks
 
Morning,

as suggested by the 2 responses, parsing the output of the ls seems to have been the cause of my issue.

Tried the code suggested by crts and it works.

Thanks for the help chaps.

DG.


All times are GMT -5. The time now is 01:24 AM.