Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: foreach loop to untar multiple files

by fasoli (Beadle)
on Nov 13, 2015 at 17:09 UTC ( [id://1147677]=note: print w/replies, xml ) Need Help??


in reply to foreach loop to untar multiple files

Thank you all, I will be doing the relevant reading (on glob) and modifying the script accordingly.

I've already copied and tried this

my $results = "~/results"; # results coming in. for (glob "$results/*.tar.gz") { system "tar -tvf $_"; }

...and it does what I want.

I've tried the following modification in my script though, and something funny happens again.

foreach (@tar) { `tar -tvf $results\/$_`; }

This gives me the following error:

tar: /home/user/results//home/user/results/results_back.tar.gz: Cannot open: No such file or directory

So the path to the tar.gz file is messed up obviously. But then my hopes build up because I see that it finds the tar.gz file and that's when I stupidly try to be "clever" by removing $results completely from the command and only keeping $_. In that case I'm just getting nothing, it fails quietly. Why is that?

Replies are listed 'Best First'.
Re^2: foreach loop to untar multiple files
by RichardK (Parson) on Nov 13, 2015 at 17:40 UTC

    why don't you print out $_ and see what it really contains?

    Also, see Basic debugging checklist to find out how to debug your code when it goes wrong.

Re^2: foreach loop to untar multiple files
by stevieb (Canon) on Nov 13, 2015 at 17:16 UTC

    Carefully look at your output... the file path is being specified twice... tar: /home/user/results//home/user/results/results_back.tar.gz. Try this:

    for (@tar){ print `tar -tvf $_`; }

    ps. I'd recommend you look at my Archive::Extract example here, to avoid having to shell out.

      for (@tar){ print `tar -tvf $_`; }

      Quite dangerous. Imaginge a file named "foo ; rm -rf * ; bar.tar.gz", injected into @tar by ls or glob. (And yes, that's a legal filename.)

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Thanks for your answer. I noticed that the file path is specified twice, that's why I said that I tried to delete $results and only go for $_. However this was obviously wrong as it hangs for a few seconds and fails quietly and I was wondering if anybody knew why. The same happens with the script you provided :(

      I will look at the Extract example in more detail, it just scared me a bit as it looked a little more complicated.

        My example *should* be good to go, just update the path info in the variables. If you try it and you have issues, reply back and we'll help you through it :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1147677]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-18 06:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found