Loading a tar file into the shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loading a tar file into the shell script
# 1  
Old 01-18-2012
Loading a tar file into the shell script

Hi All,

I am trying to write a shell script in which the contents of a small tar file is loaded into a variable and the same variable contents is moved into another file and finally untaring of the newly created file is done.

The Shell Script is as follows:
Code:
#----Start Of The Script------
File_Load=`cat -v sample.tar.gz`

echo "$File_Load" > New_Sample.tar.gz
tar -xvf New_Sample.tar.gz
#----End Of The Script--------

When i execute the above scritpt, it creates New_Sample.tar.gz, but untaring is failing is with below error:
Code:
tar: This does not look like a tar archive
tar: Skipping to next header
tar: Read 10063 bytes from Jai1
tar: Error exit delayed from previous errors

And also i observed that the files sample.tar.gz and New_Sample.tar.gz differ
Code:
#diff sample.tar.gz New_Sample.tar.gz
Binary files sample.tar.gz and New_Sample.tar.gz differ

What is the reason for getting this error? Why both files are different? What are the changes needed to fix this issue?
Please guide me.

Thanks & Regards,
Vinay

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 01-18-2012 at 10:18 AM..
# 2  
Old 01-18-2012
Why do you want to use an intermediate variable ?
Simply do :
Code:
cp sample.tar.gz New_Sample.tar.gz

Jean-Pierre.
# 3  
Old 01-18-2012
hi,

the tar files are binary and so you cannot use the command cat directly on these .tar.gz files. If you do try to do that, it will show some binary characters on screen.

There is however a gzcat command that helps us read binary files.
using this all the content will be read in ascii format.

Code:
File_Load=`gzcat sample.tar.gz`
 
echo "$File_Load" > New_Sample.tar.gz
tar -xvf New_Sample.tar.gz

here, the contents of previous tar will be loaded in New_Sample.tar.gz, but that file will be ascii and not binary.

this what you can do -

Code:
File_Load=`gzcat sample.tar.gz`
 
echo "$File_Load" > New_Sample.txt
 
tar -cvf New_Sample.tar New_Sample.txt
gzip New_Sample.tar

Regards,
A!
# 4  
Old 01-18-2012
Hi All,

Thanks for your response.

I tried with "gzcat" command, but i am still facing the same issue.

The modified shell script is as follows:
Code:
#----Start Of The Modified Script------
File_Load=`gzcat sample.tar.gz`

echo "$File_Load" > New_Sample.tar.gz
tar -xvf New_Sample.tar.gz
#----End Of The Modified Script--------

There is no directory/file created after extracting New_Sample.tar.gz. The gzcat command is printing only the ascii contents of the file. Its not converting binary contents to ascii. Is there any other way to load a small binary file(like tar file) contents into the variable of the shell script so that the new variable contents can be directed to a new-file and that new-file acts as previous binary file.

NOTE: Need to be implemented without using cp command.

Thanks & Regards,
vinay

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 01-18-2012 at 10:23 AM..
# 5  
Old 01-18-2012
Quote:
Is there any other way to load a small binary file(like tar file) contents into the variable of the shell script so that the new variable contents can be directed to a new-file and that new-file acts as previous binary file

No.
A Shell Environment variable is not suitable for holding raw binary data. Stick with files.
There is no issue with holding the name of the file in an environment variable rather than the contents of the file.

I suppose you could use "uuencode" to shift the binary data into ascii characters and then read it back with "uudecode". Not tried this because it is pointless.

What are you actually trying to do?
# 6  
Old 01-18-2012
This is going to be heavily shell specific, since almost no shell will give you variables large enough to hold a tarball of any significant size.

You will have to convert it to some format the shell can't possibly mangle. Anything binary at all, and the shell will mangle it.

Code:
# Encoded as ASCII characters
TARBALL=`openssl base64 < file.tar.gz`

# Convert back into binary before extracting
echo "$TARBALL" | openssl base64 -d | tar -zxf -

I don't understand the point of this either, unless you're trying to embed entire tarballs in a here-document or something, because you need the file in the first place to get it in the variable -- why not just use it?
# 7  
Old 01-18-2012
You can also use uuencode/uudecode :
Code:
# Encoded as ASCII characters
TARBALL="$(uuencode file.tar.gz -)"

# Convert back into binary before extracting
echo "$TARBALL" | uudecode -o new_file.tar.gz
tar -xvf new_file.tar.gz

or without using intermediate file :
Code:
# Encoded as ASCII characters
TARBALL="$(uuencode file.tar.gz -)"

# Convert back into binary before extracting
echo "$TARBALL" | uudecode | tar -xvf -



Quote:
NOTE: Need to be implemented without using cp command.
why (by curiosity) ?

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to make Shell script of using tar command?

Hi Guys, I have to make shell script means tar all files from path /home/admin and move to path /dis/wis/ Please help me. (5 Replies)
Discussion started by: aaditya321
5 Replies

2. UNIX for Dummies Questions & Answers

Executing a tar command with the --exclude option in a Debian shell script.

Hi All, I am trying to execute the following tar command with two --exclude options to suppress extract of the two directories specified. Do I need to single quote the directory paths ?? Many thanks for your help. The relevant code excerpt from the script is: cd /var/www/${SITE} ... (7 Replies)
Discussion started by: daveu7
7 Replies

3. Shell Programming and Scripting

background image not loading in newly thrown html page by shell script

I m trying to throw back html page when a file is found.While throwing back html page, the background image is not coming. I am using Apache server.Please suggest how to resolve... #!/bin/ksh echo -e "Content-type: text/html\n\n" echo "<html><head></head><body background=\"/asc/ppp.jpg\">"... (10 Replies)
Discussion started by: ravi18s
10 Replies

4. UNIX for Dummies Questions & Answers

tar -cvf test.tar `find . -mtime -1 -type f` only tar 1 file

Hi all, 4 files are returned when i issue 'find . -mtime -1 -type f -ls'. ./ora_475244.aud ./ora_671958.aud ./ora_934052.aud ./ora_934050.aud However, when I issued the below command: tar -cvf test.tar `find . -mtime -1 -type f`, the tar file only contains the 1st file -... (2 Replies)
Discussion started by: ahSher
2 Replies

5. Shell Programming and Scripting

unix script for loading a data from a file into database

Hi All, I am trying to load a data from a files in a particular directory to a database.. cd $SCC isql metdb >> ${LOGDATA}/LOAD.LOG < ! load from "${LDATA}/${FORM}.ld" insert into $LOADTABLE ! But it's showing the error "syntax error at line 46 : `<<' unmatched" Can u plz help me... (5 Replies)
Discussion started by: grajesh_955
5 Replies

6. Shell Programming and Scripting

Function loading in a shell scripting like class loading in java

Like class loader in java, can we make a function loader in shell script, for this can someone throw some light on how internally bash runs a shell script , what happenes in runtime ... thanks in advance.. (1 Reply)
Discussion started by: mpsc_sela
1 Replies

7. Shell Programming and Scripting

shell script to selectively tar directory based on date

hye everybody :) , i'm new to the scripting world.. hope you guys can help me out with this one.. i'm trying to identify any directory under /tmp/saya that is created more than one day from the current date.. e.g, today is March 14, so any directory that has time stamp March 13 backwards, i... (2 Replies)
Discussion started by: fara_aris
2 Replies

8. Shell Programming and Scripting

hi.. how to attach a tar file using shell script

Hi, How to attach a tar file using shell script or the command liine.. I following command just send the mail to the person with .txt file as body, I want to send it as attachment. /usr/sbin/sendmail -f "user1@daemon.com" user2@daemon.com <hi.txt The contents of the hi.txt will be... (1 Reply)
Discussion started by: madhumathikv
1 Replies

9. Shell Programming and Scripting

Shell Script for Data loading in Oracle

Hi All I am new to unix. I need a shell script to load a datafile in to oracle. I already have a control file, and data file. all I need is if i execute the shell it must load the data using the ctl file to table. Control file : PAY0001.ctl Datafile : mon_grs_det.dat log file :... (3 Replies)
Discussion started by: raghuraja_r
3 Replies

10. Shell Programming and Scripting

Using tar inside a shell script

Hi, I am using tar cvf inside a shell script to archive files. Is there an option to surpress any prompts which come up if the desired archive name already exists ? Thanks in Advance. Kas. (2 Replies)
Discussion started by: kas7225
2 Replies
Login or Register to Ask a Question