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 .
[[ $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
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
@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.