How to compare two strings using if

Hi,

Here is my script

#!/bin/ksh
echo $pick_typ

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

when ever i pass CUS as parameter to this script am getting the correct value CUS, however if i pass ORD as parameter it is not coming inside if it is echoing else "Outside if" statement .

I have tried several options but none worked

Please help me

Thanks

Bhargav

Maybe you mean:

#!/bin/ksh
pick_typ="$1"
echo "$pick_typ"

if [ "$pick_typ" = "CUS" -o "$pic_typ" = "ORD" -o "$pic_typ" = "TRR" ]
then
         echo "inside if"
else
         echo "outside if"
fi

I have tried this option as well still am getting the else echo "Outside if "

Please post the current version of your script.

I'm expecting that you run the script as:

./scriptname CUS
./scriptname ORD
./scriptname TRR

Is this right?

Yes I run the script like that by passing the either of the one as Parameter

./test CUS or ./test ORD

Thanks

[[ $a == z* ]] # True if $a starts with an "z" (pattern matching)
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).
[ $a == z* ] # File globbing and word splitting take place.
[ "$a" == "z*" ] # True if $a is equal to z* (literal matching).

try the below:-

#!/bin/ksh
pick_typ="$1"
echo "$pick_typ"

if [ "$pick_typ" == "CUS" -o "$pic_typ" == "ORD" -o "$pic_typ" == "TRR" ]
then
         echo "inside if"
else
         echo "outside if"
fi

k is missing in the variable name

#!/bin/ksh
pick_typ="$1"
echo "$pick_typ"

if [ "$pick_typ" = "CUS" -o "$pick_typ" = "ORD" -o "$pick_typ" = "TRR" ]
then
         echo "inside if"
else
         echo "outside if"
fi

Here's the revised 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

a more flexible way with a loop:

for K in CUS ORD TRR
do	[ "$pick_typ" = $K ] && K=""
done
[ -z "$K" ] && echo "outside if" || echo "inside if"

Hi.

The modern ksh and bash shells allow operator "=~" to do regular expression matching:

#!/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:

#!/usr/bin/env ksh

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

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

exit 0

produces:

% ./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

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.

type scriptname

... and zsh.

1 Like

Thanks a lot it worked fine.