simple unpack script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers simple unpack script
# 1  
Old 03-29-2008
simple unpack script

hi all....

I'm a newbie to Shell scripting and am trying to make a simple script that can unpack a variety of .tgz , .zip, .tar or .tar.gz archives to the a current directory

i want the script to create a folder equivalent to the base name of the archive and then move the archive to a subdirectory named archive_store

so far i have done the following:

__________________________________

#!/bin/sh

for I in *.tar.gz;
do
A=`basename $I .tar.gz`
mkdir $A
cd $A
gunzip -c ../$I | tar xf -
cd..
done

_____________________________________

Need some help to expand this script to handle the other formats....

Thank you in advance and any help would be greatly appreciatedSmilie.
# 2  
Old 03-30-2008
Sounds like a case statement. For each file type, assign the commands to use to a set of variables, and then in the main program, simply do what those variables say. Start by refactoring the script you already have, then add more cases. You could start with an easy one like .tgz

Code:
untgz () {
  gunzip -c <"$1" | tar xf -
}

for l in "$@"; do  # invoke this on a bunch of named files
  case $l in *.tar.gz) ext=".tar.gz"; command=untgz;; esac
  :
  A=`basename "$1" "$ext"`
  :
  $command "$1"
  cd ..
done

# 3  
Old 03-30-2008
thanks for your help era... will give it ago and post back...
# 4  
Old 03-30-2008
was able to expand the script using the example as follows... although am unable to make it run... have changed rights of file "chmod 755 filename" and typed "./filename" but there is no result

________________________________
#!/bin/sh

untargz () {
gunzip -c <"$1" | tar xf -
}

untgz () {
gunzip -c <"$1" | tar xf -
}

unzip () {
unzip "$1"
}


for l in "$@";

do

case $l in

*.tar.gz) ext=".tar.gz"; command=untargz

;;

*.tgz) ext=".tgz"; command=untgz
;;

*.zip) ext=".zip"; command=unzip

esac

:
A=`basename "$1" "$ext"`
:
$command "$1"
cd ..

done
________________________________________________

If anyone could suggest how to improve this script in any way and what i might be doing wrong when trying to make it run it would be much appreciated

Thanks once again
# 5  
Old 03-30-2008
The : are where you are supposed to use your imagination and/or the code you already wrote. (Sorry for not making that clear.) As it is now, the script simply does basically nothing. Also note the comment which says you invoke it on the files you want to unpack.

No need to have two identical functions, you can use the same function for tar.gz and tgz; and there is no need to write a function for unzip, since you can just say command=unzip and have it use the unzip command you already have on your computer (oh, and take care not to create a recursive function which invokes itself!)

I seem to have mixed up $1 (dollar one) and $l (dollar ell). Inside the function you want the $1 (one) but in the for loop, the file you are currently processing is $l (ell).

Last edited by era; 03-30-2008 at 12:12 PM.. Reason: one vs ell observation
# 6  
Old 03-30-2008
okay thanks again era will give it another go... kind of going in circles because there is so much that i dont know.... will try recode in simpler form
# 7  
Old 03-30-2008
Okay so i have got it down to this.... now i am completely lost...

i know that i have to add a few more lines of code on how the files will be handled once uncompressed. ie : mkdir, name=basename. Just unsure of where to start

sorry if im being really draining but i dont know how to structure the last part.....



#!/bin/sh

for file in "$@"
do
case $file in

*.zip|*.ZIP)
unzip "$file"
;;

*.tar)
tar xvpf "$file"
;;

*.tgz|*tar.gz|*.tar.Z)
gunzip -c "$file" | tar xvpf -
;;


esac
done
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to unpack files with bunzip2 using while loop

Hi, I have a problem with unzipping some file.xml.bz2 files to file.xml using while loop. all other processing on files is successfull except bunzip2. here is my piece of code while read i do bunzip2 $i done<file.lst; output : No such file or directory.le... (14 Replies)
Discussion started by: maroom
14 Replies

2. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

3. Shell Programming and Scripting

Unpack individual files from tarball

Say you don't want to unpack the whole thing, just individual files or directories within a .tgz. How to do this? (1 Reply)
Discussion started by: stevensw
1 Replies

4. Shell Programming and Scripting

Pack and unpack localtime in perl script

Hi I have a code like this: sub WriteEbcdicHeader { my $Htimestamp=localtime();#i need to pack and unpack this my $eheaderline = $Htimestamp; #packing has to be done here #unpacking has to be done after packing print $EOUTFILE return $eheaderline; } sub WriteEbcdicTrailer { ... (5 Replies)
Discussion started by: rbathena
5 Replies

5. UNIX for Dummies Questions & Answers

perl pack and unpack commands

I am using pack/unpack to encyrpt a file. syntax is below #!/bin/sh encrypt=`perl -e 'print unpack "H*","yourpassword"'` - echo $encrypt >/file/to/store/encrypted/password pass=`cat /file/to/store/encrypted/password` decrypt=`perl -e 'print pack "H*",$pass'` ... (2 Replies)
Discussion started by: erinlomo
2 Replies

6. Shell Programming and Scripting

Unpack .tar-file and get name

Hi there, I wrote the following code: if ($SCENE == *.tar) then echo "tar -xf $SCENE" tar -xf $SCENE > tar.txt set dims = `awk '$0' tar.txt` echo "name of dims is:" echo "$dims" endif My intension is, to write a variable "dims" with the output name of the tar-command. That means,... (6 Replies)
Discussion started by: friend
6 Replies

7. Shell Programming and Scripting

Unpack (extract) EAR / JAR files

i have about 30 .EAR files, every ear file have 1 .JAR file. so i need to extract .EAR files then extract .JAR files, and one important thing is that every archive must bee extracted to separate folder. i try with gzip, but when i extract 30 ear files i cant make separate folders.... (1 Reply)
Discussion started by: waso
1 Replies

8. Red Hat

Method to Unpack cpio files

Hi all, I want to unpack some files .Files and their sizes are: 1. Linux9i_Disk1.cpio -- 500m 2. Linux9i_Disk2.cpio--- 600m 3.Linux9i_Disk3.cpio---- 250m I used cpio -idmv Linux9i_Disk1.cpio command to unpack the files. But Its taking more time to unpack the files.What could be the... (2 Replies)
Discussion started by: William1482
2 Replies

9. UNIX for Dummies Questions & Answers

perl pack and unpack commands

I have a file that contains user id and corresponding password. Lets say password is "help". The below command will create a hex value for string "help". perl -e 'print unpack "H*","help"' So now password is in encoded format. Then I decoded it in the script where am fetching the... (1 Reply)
Discussion started by: max_payne1234
1 Replies

10. UNIX for Dummies Questions & Answers

does any body know how to unpack this file?????

this file i'm trying to unpack is a cloop file (1 Reply)
Discussion started by: amicrawler2000
1 Replies
Login or Register to Ask a Question