String Comparison in Shell script


 
Thread Tools Search this Thread
Operating Systems Solaris String Comparison in Shell script
# 1  
Old 01-29-2010
String Comparison in Shell script

I Have a script which gets the status of oracle database and if the status is READ WRITE ..it should echo "db is up " else "db is down"

Here is the code

Code:
if [ "$status" = "READ WRITE" ]
then
  echo "db up"
else
  echo "db down"
fi
done;


The script is giving me out put "db down" even thoug the value of variable status is READ WRITE.

Last edited by Franklin52; 01-29-2010 at 01:25 PM.. Reason: Correcting code tags
# 2  
Old 01-29-2010
Try "==" and see if the space between READ and WRITE is indeed a space, not a tab or something.
# 3  
Old 01-29-2010
Tried putting "==" no progress. There is a space between "READ WRITE"
# 4  
Old 01-29-2010
before 'if' put:

Code:
echo "x${status}x"
echo "xREAD WRITEx"

and see if they are the same.
# 5  
Old 01-29-2010
Hello Friend,

I think you reply was in the right direction to fix the problem in the code..This is what i am geting after echoing those 2 values


Code:
 
x{
READ WRITE}x
 
xREAD WRITEx

That means my variable $status has READ WRITE with some space on the front..thats why its unable to compare..can you please tell me how can i get rid of those spaces

Thankyou somuch for you help
# 6  
Old 01-29-2010
See:

Code:
echo -e "bla\nbla" | tr -d '\n'

The following:
Code:
tr -d '\n'

will remove any newlines from input.
# 7  
Old 01-29-2010
I tried this..
what happens is if i run it on the command prompt (outside) script it works fine..But in the script its not removing spaces

Code:
 
echo -e "   READ WRITE"|tr -d '\n'
READ WRITE


Code:
 
new_status=`echo -e "$status"|tr -s '\n'`
echo "x{$new_status}x"
echo "xREAD WRITEx"
 
x{-e READ WRITE}x
xREAD WRITEx



---------- Post updated at 12:08 PM ---------- Previous update was at 12:05 PM ----------

Buddy,

I removed the -e and it works wonders...Gr8 help i was struggling with this since morning Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date and Time comparison using shell script

Hi, I'm having two fields in the file F1|F2 20111220|102000 F1 ->YYYYMMDD F2 ->HHMMSS Now, I need to compare this with current date & time and need to return the difference value in hours. Already, I checked with datecalc from the forum. So, need hints from Shell Gurus. Thanks (10 Replies)
Discussion started by: buzzusa
10 Replies

2. Shell Programming and Scripting

Korn shell script comparison

I have a scenario to implement in Korn shell script. Here it is.. I need to compare two values to see whether they are same or not. The issue is that the values coming in for comparison can be a string or an integer which can be determined during run time only. Which korn shell comparison... (1 Reply)
Discussion started by: vani123
1 Replies

3. Shell Programming and Scripting

File comparison in shell script

Hi, I have written one script : #!/bin/bash echo -n -e "\nEnter how many files : " read no for (( j=1; j<=$no; j++ )) do echo -n -e "\nEnter $j File name : " read name done for (( j=1; j<=$no; j++ )) do FILE=`find ./ -type f -name "${name}"` echo "$FILE" (3 Replies)
Discussion started by: kiran_j
3 Replies

4. Shell Programming and Scripting

How to do row comparison in shell script

Hi, I need help on doing the below thing in shell script. I have a file with millions of rows called "abc.txt". i have another file with millions of rows called "xyz.txt". I would like to do the below operation. Open the abc.txt, read the first line, do some operations on the column... (2 Replies)
Discussion started by: informsrini
2 Replies

5. Shell Programming and Scripting

need help in writing a comparison shell script

I have a folder a1 with the following files sample_1.log sample_2.log sample_3.log sample_4.log sample_5.log sample_6.log In another folder there is a file b with the value 5 My script should take the value 5 ( file b), compare it with the files in folder a1, if file name contains... (1 Reply)
Discussion started by: Nagesh1
1 Replies

6. Shell Programming and Scripting

bash shell script string comparison

I want to remove a line that has empty string at second field when I use cut with delimeter , like below $cat demo hello, mum hello, #!/bin/sh while read line do if then # remove the current line command goes here fi done < "demo" i got an error message for above... (4 Replies)
Discussion started by: bonosungho
4 Replies

7. Shell Programming and Scripting

problem in string comparison in shell programming

Hello, was just wondering how to compare strings in unix? I mean as in C there is a function strcmp() in string.h, is there any function in unix for that? I tried using if and all such variations but didn't succeed. Any help would be appreciated. Thanks in advance :) (9 Replies)
Discussion started by: salman4u
9 Replies

8. AIX

Problem in ksh script ( String comparison )

hi , i am trying to compre two strings if ] or if ] when the length of var1 is small (around 300-400 char ) it works fine but when it is large (around 900-1000 chars) it fails is there any limitations for this type of comparison ??? (1 Reply)
Discussion started by: amarnath
1 Replies

9. Shell Programming and Scripting

Problem in ksh script ( String comparison )

hi , i am trying to compre two strings if ] or if ] when the length of var1 is small (around 300-400 char ) it works fine but when it is large (around 900-1000 chars) it fails is there any limitations for this type of comparison ??? (3 Replies)
Discussion started by: amarnath
3 Replies

10. Shell Programming and Scripting

Help with time comparison shell script for HP-UX

I am using Korne Shell in HP-Ux. Can someone give me and idea on how I can write a shellscript on how to do this please:- On our HP-UX server, a batch file is run every evening at about 6:30pm. The first step of this batch file will touch an empty "flag" file to indicate that the batch has... (6 Replies)
Discussion started by: gummysweets
6 Replies
Login or Register to Ask a Question