LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script failing with tar command. (https://www.linuxquestions.org/questions/linux-newbie-8/script-failing-with-tar-command-4175503150/)

Tadaen 04-27-2014 12:15 PM

Script failing with tar command.
 
Building myself some install scripts. This is giving me issues.

Code:

theming()
{
        if [ -d $1 ] ; then
                ls $1 | while read i ; do
                        tar -xf $i -C $2
                done
        fi
}

Error output

Code:

tar: ilinux.tar.gz: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: simple-2.7.tar.gz: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now

So the script is both detecting the tar files in the folder but saying they don't exist at the same time? The function is invoked as such in a while loop. This is how it is for testing purposes. Under actual use this would be done as root to insert icon packs / themes into the appropriate /usr/share/ directories.

Code:

theming ~/Debian/icons ~/icons
Please advise if have an idea.

shivaa 04-27-2014 12:37 PM

It's not a simple tar file, but a tar then zipped file (see file extension i.e. tar.gz), so in addition to tar extraction options, use -z switch as well.

For instance, to extract a tar.gz file, use command:
Code:

# tar -zxvf ilinux.tar.gz
Accordingly make changes in your script.

Tadaen 04-27-2014 12:48 PM

This is with z switch.
Code:

tar (child): ilinux.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
tar (child): simple-2.7.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now

Code:

tar -xf ??.tar.gz -C /path/
Works from the command line.

*EDIT* I should redo the subject as files exist but don't exist?

Firerat 04-27-2014 12:57 PM

http://mywiki.wooledge.org/BashGuide

For tips


Your problem is the ls $1 , you get a list of files, but not full path

You could do tar -xf ${1}/${i} .....

But using ls for this kind of thing is bad see link above

This should work for you
Code:

for tarball in ${1}/*.tar.gz;do
  tar -xf ${tarball} -C $2
done

Please note, only basic example, you may want/need to build some checks in, and deal with errors gracefully

Tadaen 04-27-2014 05:01 PM

Went ahead and altered my for statements using direct instead of ls. Won't be as recyclable as I liked but it is at least it will work out of the box on any debian based distros.

Thank you for looking and figuring it out.


All times are GMT -5. The time now is 03:42 AM.