Sponsored Content
Full Discussion: simple unpack script
Top Forums UNIX for Dummies Questions & Answers simple unpack script Post 302180235 by era on Sunday 30th of March 2008 12:19:40 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
RATFOR(1)						      General Commands Manual							 RATFOR(1)

NAME
ratfor - rational Fortran dialect SYNOPSIS
ratfor [ option ... ] [ filename ... ] DESCRIPTION
Ratfor converts a rational dialect of Fortran into ordinary irrational Fortran. Ratfor provides control flow constructs essentially iden- tical to those in C: statement grouping: { statement; statement; statement } decision-making: if (condition) statement [ else statement ] switch (integer value) { case integer: statement ... [ default: ] statement } loops: while (condition) statement for (expression; condition; expression) statement do limits statement repeat statement [ until (condition) ] break next and some syntactic sugar to make programs easier to read and write: free form input: multiple statements/line; automatic continuation comments: # this is a comment translation of relationals: >, >=, etc., become .GT., .GE., etc. return (expression) returns expression to caller from function define: define name replacement include: include filename Ratfor is best used with f77(1). SEE ALSO
f77(1) B. W. Kernighan and P. J. Plauger, Software Tools, Addison-Wesley, 1976. 7th Edition April 29, 1985 RATFOR(1)
All times are GMT -4. The time now is 05:02 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy