LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 08-04-2006, 01:01 PM   #1
OR13
Member
 
Registered: Jun 2006
Distribution: Fedora 14
Posts: 51

Rep: Reputation: 15
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.
 
Old 08-04-2006, 01:07 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
A script will only execute commands in order. Make sure you're not backgrounding commands in the "do something" area.
 
Old 08-04-2006, 07:07 PM   #3
OR13
Member
 
Registered: Jun 2006
Distribution: Fedora 14
Posts: 51

Original Poster
Rep: Reputation: 15
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.
 
Old 08-04-2006, 08:10 PM   #4
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,004

Rep: Reputation: 304Reputation: 304Reputation: 304Reputation: 304
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

Last edited by haertig; 08-04-2006 at 08:12 PM.
 
  


Reply

Tags
before, directory, remove, script, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH Shell script : copying a file to multiple folder zamri Programming 14 04-29-2008 10:27 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
help with a script that deletes files more than X days old BrianK Linux - General 5 06-14-2004 09:05 PM


All times are GMT -5. The time now is 09:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration