Check if a file type exists repeatedly

Hi,

I have the below script that checks if the .tar file exists in my present directory. If so, extract it and then delete it except if the .tar filename is blent.tar

#!/bin/bash
while [ -f *.tar ] ; do
echo "TAR BALL EXISTS"

   for f in *.tar; do
   echo "File -> $f"
   tar -xvf $f
   rm -ir $f
   done

 done

The above extracts & deletes even the blent.tar which is something I do not want. My requirement is the keep doing untar of the <filename>.tar file till either no .tar file is left or this file [blent.tar] is left behind and the loop completes.

So, when the loop completes all the .tar files should be gone except the blent.tar.

Can you please suggest.

1 Like

Untested.

ls *.tar | while read f
do
    if [ -f "$f" ] && [ "$f" != blent.tar ]
    then
        echo "$file to extract and delete after"
        # uncomment the next line if you like what you see.
        #tar xvf "$f" && rm -i "$f"
    fi
done

No... if the .tar contains .tar files which also contains .tar file this will not work as this will check for all .tars in the first run but it will not be able to check the new .tar generated as a result of tar xvf "$f"

I am basically looking for blent.tar exemption in this statement

while [ -f *.tar ] ;

Of course you haven't told us what operating system or shell you're using, and what you are basically looking for is a syntax error if there is ever more than one tar file in that directory...

Maybe the following (totally untested) code will come close to what you're tying to do:

#!/bin/ksh
found=1
while [ $found -eq 1 ]
do	found=0
	for tf in *.tar
	do	if [ -f "$tf" ] && [ "$tf" != blent.tar ]
		then	found=1
			tar xvf "$tf" && rm -i "$tf"
			ec=$?
			if [ $ec -ne 0 ]
			then	exit $ec
			fi
		fi
	done
done

Note that it will exit if extracting and removing an archive fails instead of going into an infinite loop or attempting to remove archives even if they couldn't be extracted as requested in your specifications. ('m not a big fan of throwing away data nor of infinite loops.

Note that the tar xvf "$tf" && rm -i "$tf" line has been updated to fix typos noted by RudiC and mohtashims.

1 Like

This needs to run on both Linux and Solaris as part of the automation.

And in what way does the code I suggested fail if you run it on a Solaris system in the directory that contains the tar files you want to extract and remove and on a Linux system in the directory that contains the tar files you want to extract and remove?

1 Like

I think you had a typo here tar xvf "$tf" && rm -i "$tf"

Thank you so much Don !!

Yes. Thanks for pointing it out. This correction and the one suggested earlier by RudiC have now both been applied to my earlier post.

It is things like this that made me include the warning that the code I suggested was totally untested. The code I suggest is usually tested against any sample inputs provided. In cases like this where no sample inputs are provided, the testing and debugging burden obviously falls on the thread submitter. I'm glad you were able to use my suggestion as the basis for something that works for you.

1 Like