Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If file exists 2

Status
Not open for further replies.

dodge20

MIS
Jan 15, 2003
1,048
US
I have this script

echo "\nEnter the password: \n"
read pword
echo "\nEnter the time to run ex. 1700: \c"
read time
echo "\nEnter the month to run ex. Dec: \c"
read month
echo "\nEnter the Day to run ex 09: \c"
read day

cd /usr/ias/tmp/"$pword"
rm I*;rm M*;rm G*;rm K*;rm H*;rm W*;rm *;
cd /usr/ias/bin
at "$time" nohup ht.make.lists "$pword">/dev/null &


This works fine, if the file exists in the tmp directory. But if there is no file, I get an error message. What I would like to do is create a statement that checks if the file exists, and if it does, then run the rm. If it doesn't, then I would just like to skip over this. I think it can be done with an if statement, but I don't know who to check if the file exists?

I hope I made sense.

Dodge20
 
Try a construct along the lines of:

if [ -f test1 ]
then
echo 'File exists'
echo $?
else
echo $?
fi

The -f checks whether the file exists and the $? is set to 0 (if the file exists) or 1 (if it doesn't). You can then perform the rm by checking the value of $?. Does this help?
 
Alternatively, just redirect the error message to /dev/null using 2> /dev/null as part of your rm command.

Cheers.
 
do a
Code:
 man rm
You will see that what you want is
Code:
 rm -f ...

Hope This Help
PH.
 
Ken Can you explain your answer to me. I am new to Unix and I don't understand what I should do with your scripts that you wrote. The rm -f didn't work.



Dodge20
 
Strange. However, try my second version first:

rm I* 2>/dev/null;rm M* 2>/dev/null;rm G* 2>/dev/null;rm K* 2>/dev/null;rm H* 2>/dev/null;rm W* 2>/dev/null;rm * 2>/dev/null;

Actually, having just edited that, wouldn't the same thing be achieved using just rm * 2>/dev/null rather than all of the preceding rms. rm * removes everything anyway!

HTH.
 
....with the usual caveat - treat rm with the utmost respect, and be sure you're where you want to be when invoking it!
 
OK

The reason for I have several rm is because if I just do rm *. I get an error message that says arguments too long. Thanks for your help, but I think I am going to have to take a different approach to this.

Dodge20
 
I am on SCO 5

Let me try to restate my problem. I was just reading through it, and I didn't say it very clearly. I wrote file, when I meant directory. What happens is, when I enter the pword, if directory exists, everything works well. But when that directory doesn't exist, it quits and there is no prompt for the time,day, or month. So the at command (I know it isn't correct) doesn't get a chance to run.

Now I should explain that the at command, the $pword is a file that does exist in the /usr/ias/bin directory

So would this be better to break into 2 different steps?

Dodge20
 
Try something like this:
Code:
mydir=/usr/ias/tmp/"$pword"
rm -rf "$mydir"
mkdir "$mydir" #maybe add chown and/or chmod
cd "$mydir"
cd /usr/ias/bin
at "$time" nohup ht.make.lists "$pword">/dev/null &


Hope This Help
PH.
 
PHV

I am getting an error message that says:

pword: Undefined variable.

Do you know why?

Dodge20
 
Do you still have this part in your script:

[tt] echo "\nEnter the password: \n"
read pword[/tt]

That's where it defines the variable.

Annihilannic.
 
Yeah it is still in there. That is why I am confused.

Dodge20
 
try removing the & from the "at" line. it may be spawning it as a child process without the pword being exported.
 
I still got the error

pword: Undefined variable

This is the whole script right now

#ht.pm2

echo "\nEnter the password: \c"
read pword
echo "\nEnter the time to run ex. 1700: \c"
read time
echo "\nEnter the month to run ex. Dec: \c"
read month
echo "\nEnter the Day to run ex 09: \c"
read day

mydir=/usr/ias/tmp/"$pword"
rm -rf "$mydir"
mkdir "$mydir" #maybe add chown and/or chmod
cd "$mydir"
cd /usr/ias/bin
at -t "$time" nohup ht.make.lists "$pword">/dev/null


Dodge20
 
OK I just tried running some other scripts that I have that have a variable in them. I also get the undefined variable in those. I know they used to work. Do you know what happened?

Dodge20
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top