simple unpack script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers simple unpack script
# 8  
Old 03-30-2008
The idea was to put the specific parts in the case statement and then have a general-purpose piece of code just like you had already at the end, except it uses whatever was defined in the case statement above. But okay, here you are.

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;;
    *.tgz) ext=".tgz"; command=untgz;;
    *.tar.Z) ext=".tar.Z"; command=untgz;;
    *.zip) ext=".zip"; command=unzip;;
    *.ZIP) ext=".ZIP"; command=unzip;;
    *) echo "$0: cannot handle $l, skipping" >&2
        continue ;;
  esac

  A=`basename "$l" "$ext"`
  mkdir "$A"
  cd "$A"
  $command "$1"
  cd ..
done

The function is required for tar.gz because we want a command which takes exactly one parameter as its last argument, so we have to rearrange things a bit and create a function which does that. For most formats you will simply need to add a command= which already works as specified (archive name is sole argument to command), so you don't need a separate function for it (witness unzip).

The extension handling is kind of ugly; with associative arrays that would be easy to put into a table, but the shell doesn't have that facility. You could still think about ways to make it a bit less atrocious. Maybe a while loop which reads extensions and commands?

Code:
  # ... code as above, this replaces just the case statement
  ext=
  while read x cmd; do
    case $l in "$x") ext=$x; command=$cmd; break;;
  done <<____HERE
    .zip  unzip
    .ZIP  unzip
    .tar.gz  untgz
    .tar.Z   untgz
    .tgz  untgz
____HERE

  case $ext in '') echo "$0: dunno how to handle $l -- skipping" >&2 ; continue;; esac
  # ... continue as before with A=basename etc

Next you will tell us this was a homework assignment and your lecturer didn't understand how "your" code works, and gave you an F. Oh well.

Actually, your code looks good as well, you just need to put the mkdir etc before the case and the cd .. after the case, but then you will need to figure out a new way to extract just the base name of the archive. It's generally a good idea to put all related information in one place, thus I wanted the case statement to contain all the stuff which is specific to each file type.

Last edited by era; 03-30-2008 at 01:31 PM.. Reason: A bit of rationale for encapsulating file-type specific stuff in one place
# 9  
Old 04-01-2008
hi era.... firstly thanks for your help.....

i know its like you are spoon feeding me but im still unsure how i would handle the basename in a while loop expanding on the first script.

thankyou again
# 10  
Old 04-01-2008
Quote:
Originally Posted by era
then you will need to figure out a new way to extract just the base name of the archive
Can't really help you there, I changed your code precisely to solve this problem.
# 11  
Old 04-01-2008
okay please tell me if i am being annoying.... but i think i have lost track of where to go next.... sorry if im not following your hints but this is a massive learning curve for me. I am truely grateful for your help..

correct me if im wrong but, in the bellow code we have a case statement to handle all the different extension types... then a function "untgz" to handle one parameter at a time... in the main program we have an attempt to handle the basename of the file currently bieng read.

im sorry but could you possibly tell me where to go next. I dont want you to tell me the solution just help me understand what part of my code i need to rewrite, comands to research etc....

If i sound lost it mainly because ive only been using unix and doing shell scripting for three weeks.

thankyou once again

Code:
#!/bin/sh
untgz () {
  gunzip -c <"$1" | tar xf -
}

for l in "$@"; 
  case $l in 
    *.tar.gz) ext=".tar.gz"; command=untgz;;
    *.tgz) ext=".tgz"; command=untgz;;
    *.tar.Z) ext=".tar.Z"; command=untgz;;
    *.zip) ext=".zip"; command=unzip;;
    *.ZIP) ext=".ZIP"; command=unzip;;
    *) echo "$0: cannot handle $l, skipping" >&2
        continue ;;
  esac

  A=`basename "$l" "$ext"` #is this where i should have my while loop?
  mkdir "$A"
  cd "$A"
  $command "$1"
  cd ..
done

# 12  
Old 04-01-2008
I'm not sure what problems remain to be solved. Doesn't it do what you want already?
# 13  
Old 04-01-2008
well when i run it i have various file in the same folder to test i.e - a.tgz , b.tar.gz, c.zip - nothing happens. I have even typed "echo $shell" to see where shell files are located and changed the start of the script to match.

what would be a better way to debug?
# 14  
Old 04-03-2008
Add prints here and there to see what it's doing. And maybe some error checking too.

Code:
A=`basename "$l" "$ext"`
echo file $l, dirname $A, extension $ext
mkdir "$A@ || { echo "$0: Could not mkdir $A -- skipping" >&2; continue; }

You still seem to have "dollar one" in some places where you should have "dollar ell".
 
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