simple unpack script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers simple unpack script
# 15  
Old 04-05-2008
thanks for all the help..... have worked out what i have been doing wrong... just got a few more questions.....

currently when i run the script i do so in the following way
i.e ./script foo.tgz # name of file, then file i want to unpack

1) how or what would i have to do to get the script to run without having to state the file i want to unpack?

i.e typing only ---> ./script <----- # the script runs and unpacks all files in the directory i am running it in.

my code looks like this so far....

HTML Code:
#!/bin/sh -x
untgz() {                 #untgz function which takes one parameter
  gunzip -c <"$1" | tar xf -           #as its last argument
}


for I  in "$@";  do
  case $1  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 $1, skipping" >&2
        continue;;
  esac


  A=`basename "$1" "$ext"`
  mkdir "$A"
  mv "$1" "$A"
  cd "$A"
  $command "$1"
  mv "$1" ../
  cd ..
  mkdir store_folder
  mv "$1" store_folder
  cd ..
done
would i have to completely change the structure of my code or maybe change this line
HTML Code:
for I  in "$@";  do
to
HTML Code:
for I  in "*.";  do
or even could there be a way to type ./script .... folder i want to unpack...

Any ideas would be great thanks again
# 16  
Old 04-05-2008
To loop over all files in the current directory, do

Code:
for I in *; do ...

You will want to tweak down the diagnostics so it doesn't complain about each and every file it doesn't know how to handle.

You still have $1 in a lot of places where you should now have $I (dollar capital I). Basically anywhere outside the function, you want $I (ih) not $1 (one).

The temporary move of the packed file into the directory where you extract it seems superfluous; just $command ../"$I" instead. If you change that, you will need to make some changes to the code where it moves the file to the store_folder, too.
# 17  
Old 04-05-2008
tried changing

HTML Code:
for I  in *;  do ...
but getting the following errors

HTML Code:
 ./unpack
+ ...
./unpack: ...: not found
+ continue
+ ...
./unpack: ...: not found
+ continue
+ ...
my code looks like this have changed the $I's but the script is not handling the files in the current directory the way i want to...

what am i going wrong in my main program?

HTML Code:
#!/bin/sh -x
untgz() {                 #untgz function which takes one parameter
  gunzip -c <"$1" | tar xf -           #as its last argument
}


for I  in *;  do ...
  case $1  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:  skipping" >&2
        continue;;
  esac


  A=`basename "$I" "$ext"`
  mkdir "$A"
  mv "$I" "$A"
  cd "$A"
  $command ../ "$I"
#  mkdir store_folder
#  mv "$I" store_folder
#  cd ..
done
thanks for all your help era
# 18  
Old 04-06-2008
No, the ... was just to signal that you'd continue as before from that point on.

Just take out the three dots after "do" and fix the one remaining occurrence of $1 (in the case -- you should have case $I in --) and you should be fine. Oh, and you should either not move the file before unzipping it, and use $command "../$I" (there are no spaces between ../ and $I -- it means unzip the file $I in the parent directory), or move the file to the current directory, and omit the ../

You might want to put in some error checking, too: if the directory you want to create already exists, should it just go there anyway, or perhaps add a suffix to the directory name and try again, for example?

Similarly, if you run it on multiple files, store_folder will already exist at least after the first iteration, but it might also exist after a previous run. If nothing else, you probably want to avoid the warnings from attempting to create a directory which already exists. For that particular case, it's probably sufficient to simply check if it exists already, and only if not, try to create it.

Code:
test -d store_folder || mkdir store_folder

# 19  
Old 04-06-2008
GOT IT FINALLY........... THANKS ERA YOU ARE A LEGEND................
hope i didnt drive you up the wall............. good job Smilie
 
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