LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell script deletes folder before due (http://www.linuxquestions.org/questions/linux-general-1/shell-script-deletes-folder-before-due-470805/)

OR13 08-04-2006 01:01 PM

Shell script deletes folder before due
 
Hello :),
I was writing a script for a certain need of mine, it goes like this:
Code:

if [ $# -eq 2 ]; then
        mkdir temp
        cp $1 temp/doc.pdf
        cd temp
        *** DO SOME THINGS TO THE PDF FILE ***
        cd ..
        rm -dRf temp
        gedit found.htm
else
        echo "Usage: doit {filename}"
        echo
fi

Now it didn't work, claiming that it cannot create temp/doc.pdf. I had a theory, so I checked it out - I commented the line that deleted the new directory ("rm -dRf temp") and now it worked.

So my questions are:
a) Why does the script delete the directory before it did everything precedes this line?
b) How can I prevent this from happening, AKA make the script delete the directory only AFTER I don't need it anymore?

Thank you,
O.R.

Matir 08-04-2006 01:07 PM

A script will only execute commands in order. Make sure you're not backgrounding commands in the "do something" area.

OR13 08-04-2006 07:07 PM

Here's the full script (it just wasn't finished when I wrote the last post):
Code:

#!/bin/sh

if [ $# -eq 2 ]; then
        mkdir temp
        cp $1 temp/doc.pdf
        cd temp
        pdftohtml -c doc.pdf
        find -name '*.html' -exec grep -i $2 {} \; > ../found.htm
        cd ..
        rm -dRf temp
        gedit found.htm
else
        echo "Usage: doit {filename} {phrase}"
        echo
fi

Any ideas???
Really, if I erase the line "rm -dRf temp" it works fine!
I know that bash should run the commands in order, but maybe I'm doing something wrong here, I don't know...
Help :(

O.R.

haertig 08-04-2006 08:10 PM

My guess would be a permissions problem on creating that temp directory. When it was working, that temp directory might have been previously existing. When it didn't work, the temp directory was not there and failed to be created. When you were commenting/uncommenting that rm command you were affecting the existance of the temp directory, but NOT for the current execution, ... for the NEXT execution. Can be confusing! Something along those lines anyway.

Did you get any error messages on stderr when running this script?

Also, for scripting I never like to assume that a command worked. From your code, you're assuming lots of stuff (mkdir worked, cd worked, etc.) Never assume, especially if you're running as root (I think you are since you're using the -d option for rm, and that is only available for root).

I'd recommend using fully qualified paths and checking for command success, e.g.,
Code:

mkdir -p /full/path/to/temp
cd /full/path/to/temp
if [ "$?" != "0" ]
then
    echo "ERROR: cd failed!"
    exit 1
fi
cp $1 /full/path/to/temp/doc.pdf
if [ ! -s /full/path/to/temp/doc.pdf ]
then
    echo "ERROR: Non-existant or zero length input file!"
    exit 1
fi
pdftohtml -c /full/path/to/temp/doc.pdf
if [ "$?" != "0" ]
then
    echo "ERROR: pdftohtml failed!"
    exit 1
fi

blah, blah, blah ... you get the picture



All times are GMT -5. The time now is 02:53 PM.