How to compare two strings using if


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare two strings using if
# 8  
Old 02-23-2010
Here's the revised code:

Code:
#!/bin/ksh
 
pick_typ="$1"
echo $pick_typ
 
 
if [ "$pick_typ" == "CUS" ] ||
   [ "$pick_typ" == "ORD" ] ||
   [ "$pick_typ" == "TRR" ]
then
  echo "inside if"
else
  echo "outside if"
fi


Last edited by Scott; 02-23-2010 at 02:38 PM.. Reason: Please use code tags
# 9  
Old 02-23-2010
a more flexible way with a loop:
Code:
for K in CUS ORD TRR
do	[ "$pick_typ" = $K ] && K=""
done
[ -z "$K" ] && echo "outside if" || echo "inside if"

# 10  
Old 02-23-2010
Hi.

The modern ksh and bash shells allow operator "=~" to do regular expression matching:
Code:
#!/usr/bin/env ksh

# @(#) s1	Demonstrate matching.

# Infrastructure details, environment, commands for forum posts. 
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo ; echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p
set -o nounset
echo

echo " Results:"
pick_typ="$1"
echo $pick_typ

# if [[ "$pick_typ" = "CUS" || "$pic_typ" = "ORD" || "$pic_typ" = TRR" ]];then
if [[ "$pick_typ" =~ CUS|ORD|TRR ]]
then
  echo "inside if"
else
  echo "outside if"
fi

exit 0

which, when driven by script s2:
Code:
#!/usr/bin/env ksh

# @(#) s2	Drive working script s1 with parameters.

./s1 CUS
./s1 ORD
./s1 TRR
./s1 junk

exit 0

produces:
Code:
% ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
ksh 93s+

 Results:
CUS
inside if

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
ksh 93s+

 Results:
ORD
inside if

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
ksh 93s+

 Results:
TRR
inside if

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
ksh 93s+

 Results:
junk
outside if

See man ksh, search for "==" to see details ... cheers, drl
# 11  
Old 02-23-2010
Well spotted anbu23 !

@bhargav23 . It is advisible to not call a script the same name as a unix command.
If the following command for a script called "scriptname" finds a program or a shell builtin, try another name.

Code:
type scriptname

# 12  
Old 02-23-2010
Quote:
Originally Posted by drl
The modern ksh and bash shells allow operator "=~" to do regular expression matching:
[...]
... and zsh.
This User Gave Thanks to radoulov For This Post:
# 13  
Old 02-24-2010
Thanks a lot it worked fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Ubuntu

Compare 2 strings

I think there is a way to detect mouse movement. valuator changes if the mouse moves. So I need to compare the two strings. Not sure how to do that. How could I send the valuator string to a file ? I would need to do it twice. andy@7_~/Downloads$ xinput query-state 9 2 classes :... (7 Replies)
Discussion started by: drew77
7 Replies

2. UNIX for Beginners Questions & Answers

If statement to compare two strings

Hi, I am trying to do the following to see if "ip" is already present in a file. if ; then echo "hi" else echo "hello" fi I am seeing errors on the if statement. Can someone please correct the syntax for me? Thanks (2 Replies)
Discussion started by: waince
2 Replies

3. Shell Programming and Scripting

Compare 2 Strings

I have 2 values V_1_4_4_b1 and V_1_5_1_RC_b1. I would need to compare them and determine if the 1st value is greater, less or equal than the 2nd value. The result should need to have a return value. I have below code in bash function but it seems it is not comparing it correctly. Any help will... (8 Replies)
Discussion started by: aderamos12
8 Replies

4. Shell Programming and Scripting

Compare strings with space in if statement

DEV> vi test_if_statement.sh "test_if_statement.sh" 9 lines, 205 characters proc_out="Normal completion" proc_out_comp="Normal completion" echo 'proc_out:'$proc_out echo 'proc_out_comp:'$proc_out_comp if then echo 'match' else echo 'no_match' fi ~ ~ ~ ~ ~ ~ ~ ~ ~ (4 Replies)
Discussion started by: cartrider
4 Replies

5. Shell Programming and Scripting

How to compare two strings in a file

hello guyzz please help me out.. I have two file a.sh and b.sh it contains two string SD109 ,SD108 . I want to compaere these two string . If a.sh>b.sh do rebasing record time. else it shows no rebasing required. Thanks. (2 Replies)
Discussion started by: abhijtr
2 Replies

6. Shell Programming and Scripting

Compare two strings

hi.. i have a problem to compare two string my code is like that if ] then echo "both data are correct" elif ] echo "data is wrong" fi here $username1 is taking value from file.. (7 Replies)
Discussion started by: shubhig15
7 Replies

7. Shell Programming and Scripting

How to Compare 2 Strings ?

Hello , I want to Compare with 2 strings and get if they are True or not please would like some help on this #!bin/ksh echo "Enter Name 1" read Name1 echo "Enter Name 2" read Name2 echo "------------------------" echo "First Name: $Name1" echo "Second Name: $Name2" echo... (25 Replies)
Discussion started by: shatztal
25 Replies

8. Shell Programming and Scripting

Compare text strings.

Hi Im trying to write a script that compare a text string. But it fails, I think it adds a extra line feed to the result and fails beacuse of that. The script. DT=`date +'%Y%m%d%H%M%S'` #ALARM_BIN=/users/alarms/ssa/alarms/bin QUEUE_THR=10 #unset offset #offset="***Server reports data... (3 Replies)
Discussion started by: vettec3
3 Replies

9. Shell Programming and Scripting

How to compare two strings

Hi all, I am trying to compare two strings/dates, but its throwing error::Syntax error at line 5: Please help !! Any alternate way to compare two dates is also fine.... logdate1=`date -u '+%Y.%m.%d %T'` sleep 5 logdate2=`date -u '+%Y.%m.%d %T'` if test... (5 Replies)
Discussion started by: prashant43
5 Replies

10. Shell Programming and Scripting

to compare two strings

hi all, i am new to unix. Actually i need to compare two string and print the result... suppose type='sun' if; then echo good morning else echo good night fi whether the comparison is right r we need to use eq???? help me please.... :confused: thanks in advance.... (1 Reply)
Discussion started by: ithirak17
1 Replies
Login or Register to Ask a Question

Featured Tech Videos