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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF-THEN script not working

Status
Not open for further replies.

buzzcasper

Technical User
Jan 14, 2003
7
0
0
US
I don't know why I can't get this but I'm trying to do a comparison of two text strings. The first string is known as "Completed: ALTER DATABASE BACKUP CONTROLFILE TO '/fnsw/local/". The other string is being pulled from a log file and I'm just trying to compare the two. I started it many different ways: having the known string already in a file, running a "tail" of the log file and outputting it into a script generated file and then trying to compare the two files. Ideally, I would like some simple like:

IF [ string1 = "Completed: ALTER DATABASE BACKUP CONTROLFILE TO '/fnsw/local/" ]

but nothing seems to be working for me. I also tried playing with the "test" command within this but maybe I'm trying too hard. My syntax is probably incorrect somewhere but it just doesn't want to work. Anybody have any advice??
 
Pal!!!

tray

if [ $string1 = "Completed: ALTER DATABASE BACKUP CONTROLFILE TO '/fnsw/local/" ]

$string1 is the right form to use variables
 
I was kind of guessing that part for the IF statement but then what's the command to set the string1 variable. Here's my script:

echo "***************************************************\n"
echo "************ Oracle Backup Status *****************\n"
string1='tail -n1 /home/fnsw/alert_MINN.log'
if [ $string1 = "Completed: ALTER DATABASE BACKUP CONTROLFILE TO '/fnsw/local/" ]
then
echo "The Oracle Backup PASSED."
else
echo "The Oracle Backup Failed."
fi

One other thing, I'm thinking of inputting a command in this to see if the script that writes to the alert_MINN.log is running. Is there a command to check to see if a script is running that could be put into an IF-THEN statement??

 
Try something simple like:

echo $string1 >> /tmp/string.log

between the string1= and the if.

For seeing if somethings running you might need to do something similar to your string1= line like:

script1=`ps -ef | grep scriptname`

Then see how many lines it comes back with.
 
also make sure that when assigning your variable

string1=`tail -n1 /home/fnsw/alert_MINN.log`

that you are using a back tic ` (see tilde character key) and not a single quote ' .


 
echo "***************************************************\n"
echo "******** Oracle Backup Status *********************\n"
string1=`tail -n1 /home/fnsw/alert_MINN.log`
echo $string1
if [ $string1 = "Completed: ALTER DATABASE BACKUP CONTROLFILE TO '/fnsw/local/"]
then
echo "The last Oracle Backup FAILED!"
echo "\nThe error output from the backup script can be found in:"
echo "/$ORACLE_HOME/rdbms/log/alert_MINN.log"

else
echo "The last Oracle Backup PASSED."
fi
echo "\n***************************************************"

The output I am getting is:
************************************************************

***************** Oracle Backup Status *********************

Completed: ALTER DATABASE BACKUP CONTROLFILE TO '/fnsw/local/
checkbackup[7]: [Completed:: not found.
The last Oracle Backup PASSED.

So setting the variable is working fine but I'm still missing why the if-then statement is not comparing the variable to the text string.
 
Hi,

Because you are doing a character comparison, try enclosing $string1 in quotes:

if [ "$string1" = "Completed: ALTER DATABASE BACKUP CONTROLFILE TO '/fnsw/local/" ]

I believe that will help you.
 
The problem is in the condition test with the square brackets. You are using [ ] or test syntax; the korn shell [[ ]] construct has many more options. Before you had to surround both sides with double quotes or use hacks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top