problem in variable comparision


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem in variable comparision
# 1  
Old 06-11-2011
Wrench problem in variable comparision

Hi All,

I need to compare the string variable with input value which have more than one word which also should be case insensitive.
Code:
#!/bin/sh
echo "please choose your choice: "
read choice
if [ "$choice" = `echo "CASE TEST" | tr [:upper:] [:lower:]` ]
then echo "correct"
else
echo "wrong"
fi

but i am getting the output for first word only.Smilie
Someone plz help me how to compare if it is more than a word.

Thanks in advance for ur kind help.Smilie

Last edited by Franklin52; 06-11-2011 at 04:16 PM.. Reason: Please indent your code and use code tags
# 2  
Old 06-11-2011
Hello, kalai:

You need to double quote the command substitution to prevent tr's "case test" output from being split into multiple fields (the
= operator expects one operand on each side).

Also, do yourself a favor and get out of the habit of using `cmd args` for command subsitution. It's brittle and error prone (particularly once you start quoting and nesting). Chances are your shell supports the $(cmd arg) syntax, which is much easier to work with.

One last thing. You should quote each of the arguments to tr. The bracketed expressions are special to the shell (they're used in pattern matching) and there's a possibility that file name pattern matching could take place unintentionally.

In summary, my suggestions amount to changing a line in your code to the following:
Code:
if [ "$choice" = "$(echo "CASE TEST" | tr '[:upper:]' '[:lower:]')" ]

Regards and welcome to the forum,
Alister

Last edited by alister; 06-11-2011 at 04:39 PM..
# 3  
Old 06-11-2011
Hi

i don't see the use of the following :

Code:
`echo "CASE TEST" | tr [:upper:] [:lower:]`

since could just be

"case test"

Maybe you could try something like:

Code:
if [ $(echo "$choice" | tr [:upper:] [:lower:]) = "case test" ]

# 4  
Old 06-13-2011
Hi All,
I got the output what i want.
Code:
#!/bin/sh
echo "please choose your choice: "
read choice
b=$(echo "$choice" | tr [:upper:] [:lower:])
if [ "$b" = "case test" ]
then echo "correct"
else
echo "wrong"
fi

Still i dont know whats wrong if i compare directly within IF loop.

@Alister,
Hi

Thanks for your welcome and post Smilie. Initially i used bracketed expressions for that but i got some error.
but when i tried with the command args.. i dint find any error.
Thanks for your valuable suggestions.

@CTSGNB
Hi
Thanks for your valuable suggestions.

Last edited by Scott; 06-13-2011 at 07:03 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

2. UNIX for Advanced & Expert Users

Permission Comparision

Hi, I need to know how to compare the permissions of files which are in two different directories in Unix.. Experts - can any one help on this..!!! (1 Reply)
Discussion started by: yanis
1 Replies

3. Shell Programming and Scripting

Problem with a variable withing a variable

hello there, basically im screwed with a variable that should take the last modification date of a file. my code is fileCreationTime=$(( `ls -l $fileName | tr -s " " | cut -d " " -f6` )) my problem arise coz when the code is executed and stored in a file the return value is 1993 and not... (4 Replies)
Discussion started by: thurft
4 Replies

4. Programming

Problem with date comparision in Pro*C

Hi, I have written the following Pro*C program to get the difference between 2 dates.But when I am printing the value of the date difference,it is always showing 0. #include<stdio.h> #include<sqlca.h> #include<oraca.h> void main() { EXEC SQL BEGIN DECLARE SECTION; int diff; ... (4 Replies)
Discussion started by: sureshbabuc
4 Replies

5. Shell Programming and Scripting

File comparision

Hi All I have to files cat a.txt AAA BBB CCC DDD and cat b.txt AAA CCC EEE i want to compare these two files and o/p should have content of file a.txt which is not in file b.txt c.txt BBB DDD Please help me (1 Reply)
Discussion started by: aaysa123
1 Replies

6. Shell Programming and Scripting

problem in date comparision

#!/bin/ksh var="2009-08-31 12:39:05 UTC" ddate=`echo $var|cut -d' ' -f1` y1=`echo $ddate|cut -d'-' -f1` m1=`echo $ddate|cut -d'-' -f2` d1=`echo $ddate|cut -d'-' -f3` filedate=$y1$m1$d1 currdate="20070814" if ]; then echo "$LINE -> $filedate LOWER THAN $currdate" ... (0 Replies)
Discussion started by: HemaV
0 Replies

7. Shell Programming and Scripting

Problem with Date Comparision

Hi, I have a file which has the date in the last line in the example pasted along with the rates of the countries. -- I want to compare the date in the last line of the file mentioned in the example below with the system date from Monday to Friday. -- If system date is equal to the date... (2 Replies)
Discussion started by: Raji_gadam
2 Replies

8. Shell Programming and Scripting

File comparision

HI, I would like to know how to compare two files and replace non-matching lines with "_" . I can get non-mathing lines with grep -v -f file1 file2 i just want to knw how to display 'file2' with non-matching lines from 'file1' replaced by "_" for exmaple file1: a b c d ... (2 Replies)
Discussion started by: maddy81
2 Replies

9. UNIX for Dummies Questions & Answers

problem in string comparision

Hi All, I've to compare the number of records present in a file against its trailer count. for ex: rec_cnt=$(awk 'END{print NR}' file.txt) trl_cnt=$(tail -1 file.txt| cut -c1-6) problem is trailer is appended with zero's and while comparing it is giving problem. i.e, rec_cnt=9 and... (1 Reply)
Discussion started by: ganapati
1 Replies

10. Shell Programming and Scripting

while - comparision

Hi, Please find the attached scriplet and suggest me to fix the bug in this. ----------------------------------- noofdirs=`ls *.tar | wc -l` if ; then let i=1 while ( $i <= $noofdirs ) ; do echo $i mkdir $i file1=`ls *.tar | head -1` mv $file1 $i i =... (2 Replies)
Discussion started by: sharif
2 Replies
Login or Register to Ask a Question