Operating System - HP-UX
1833768 Members
1880 Online
110063 Solutions
New Discussion

awk and untarring an entire dir.

 
SOLVED
Go to solution
Chris Tijerina_2
Occasional Advisor

awk and untarring an entire dir.

I am at my wits end with this little script.
The goal is to gather all tar mother balls that have a prefix of 'sfib' or 'vins' and untar them (sfib or vins).
I have created and output file and I awk the output file to list column $9 and if it meets a certain criteria then follow the logic that is in the script. What am I doing wrong with my 'while' and 'do' statements? Is there a way for me to untar all tar files within a directory? If anyone can help, I will crown the:-)
Below is a cut and paste of the script.
----------------------------------------------
#!/bin/ksh

cd /home/s1perf/test_dir
ls -l > /home/s1perf/test_dir/mother_tar

awk '{print$9}' /home/s1perf/test_dir/mother_tar | sort | while (read sfib) ; do
if [[ -f $DATA/$sfib.tar ]] ; then

awk '{print $9}' /home/s1perf/test_dir/mother_tar | sort | while read vins ;
if [[ -f $DATA1/$vins.tar ]] ; then

mv -R /home/s1perf/test_dir/*sfib.tar /home/s1perf/test_dir/test_dump ;
mv -R /home/s1perf/test_dir/*vins.tar /home/s1perf/test_dir/test_dump
fi

cd /home/s1perf/test_dir/test_dump
tar -xvf *$9.tar
etc

chmod 777 */*
rm *$9.tar
exit $9.tar
"If you choose not to decide, you still have made a choice."
11 REPLIES 11
Sridhar Bhaskarla
Honored Contributor

Re: awk and untarring an entire dir.

Chris,

I think there is some confusion in your scripts and I hope I understand your "prefix" thing.

When you wrote

awk '{print $9}' /home/s1perf/test_dir/mother_tar|sort |while (read sfib)
do

...

You are not checking for the condition that the prefix should be sfib here.. Right?...

You may want to do this probably...

#!/usr/bin/ksh

cd /home/s1perf/test_dir

for i in "sfib vins"
do
cp -Rp $i*.tar test_dump
done

cd test_dump
tar xvf *.tar

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Joseph C. Denman
Honored Contributor
Solution

Re: awk and untarring an entire dir.

Hi Cris,

This should be faily easy.


dumpdir=cd /where/to/put/them
cd /where/your/files/are
ls sfib* vins* | while read line
do
mv $line $dumpdir/.
done
cd $dumpdir
for x in `ls *.tar`
do
tar xvf $x
done



Hope this helps.

...jcd...
If I had only read the instructions first??
Santosh Nair_1
Honored Contributor

Re: awk and untarring an entire dir.

Chris,

Why not do this:

#/bin/ksh
SRC_DIR=/home/s1perf/test_dir
DEST_DIR=/home/s1perf/test_dir/test_dump

cd $SRC_DIR

for FILE in $SRC_DIR/*sfib.tar $SRC_DIR/*vins.tar
do
if [[ -r $FILE ]]
then
FILENAME=$(basename $FILE)
mv -R $FILE $DEST_DIR
tar xvf $DEST_DIR/$FILENAME
fi
done

Or something like that...


-Santosh
Life is what's happening while you're busy making other plans
Sridhar Bhaskarla
Honored Contributor

Re: awk and untarring an entire dir.

Chris,

If your idea is to just untar all the tar balls that start with sfib or vins, we don't really need to use loops..

cd /home/s1perf/test_dir
cp -Rp sfib*.tar test_dump
cp -Rp vins*.tar test_dump
cd test_dump
tar xvf *.tar
rm *.tar

Is this not what you want?

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Chris Tijerina_2
Occasional Advisor

Re: awk and untarring an entire dir.

Thanks for all the outstanding replies, but I am still working on this issue. Is there a way for me to untar all the tar files within a dir and not have the shell asks if I want to untar a file, you know, the shell asks if I want to untar; reply with y or n. How do I suppress this question. I would like to fully automate this. Could this be done? Again I want to thank Joseph, Santosh, Sridhar
"If you choose not to decide, you still have made a choice."
Alan Riggs
Honored Contributor

Re: awk and untarring an entire dir.

tar doesn't normally prompt before untarring a ball. Has someone in your environment written a wrapper for tar (or are you using a 3rd party tar utility?). If the latter, man tar might show you the appropriate flag to escape the query.

In the former (or if the man page fails) you can always try "brute force".

y | tar xvf *
Alan Riggs
Honored Contributor

Re: awk and untarring an entire dir.

Sorry, that should have been:

yes| tar xvf *
Chris Tijerina_2
Occasional Advisor

Re: awk and untarring an entire dir.

The system is on 11.0 and no third party tar utilis are being used.
Thanks for the response Alan, but I am still getting (untar the follwoing tar file? [y] [n]) prompts when I run the following script:
----------------------------------------------
#!/bin/ksh
dumpdir=/home/s1perf/test_dir/test_dump
cd /home/s1perf/test_dir
ls sfib* vins* | while read line
do
mv $line $dumpdir/.
done
cd $dumpdir
for x in `ls *.tar`
do
yes| tar xvf $x
done
"If you choose not to decide, you still have made a choice."
Sridhar Bhaskarla
Honored Contributor

Re: awk and untarring an entire dir.

Chris,

It is surprizing. I never saw tar asking
yes no questions.

Can you manually try to untar without keeping
it in a script and see if it works?.

#mkdir /tmp/junk;cd /tmp/junk
#tar xvf /home/s1perf/test_dir/somespecific.tar

See if this asks you yes or no?.

Surprizing :-()

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Alan Riggs
Honored Contributor

Re: awk and untarring an entire dir.

Very strange. I untar on 11.00 all the time and have never seen a (y|n) prompt. Please do run a test manually, perhaps with execute trace on. Also, what do the following commands return:
which tar
what $(which tar)

Chris Tijerina_2
Occasional Advisor

Re: awk and untarring an entire dir.

Yahoo!!! It works, thanks Alan,Sri,Jospeh,Santosh!
I will not forget to give points.

-Chris
"If you choose not to decide, you still have made a choice."