Search for a string

Hi All,

I have a shell script which accepts a parameter. I want to check whether the parameter value contains a particular string. To the best of my knowledge, grep works on a file, not on a parameter value. How can i do a search of the string and proceed.

Any help would be appreciated.

Thanks,
Sumesh

I think you are trying something like this:

#! /bin/ksh
echo $1
if [[ $1 == "Y" ]]; then
        echo "Yes"
else
        echo "No"
fi

Output:

$./test.sh Y
Y
Yes
$./test.sh N
N
No

Well, it can work on stdin too like most of unix commands

export YOURVALUE="something's going on"
echo $YOURVALUE |grep thing > /dev/null
[ $? -eq 1 ] && echo "No match!"

Hi,

Not really! I've to check the paramater value against two string values. ie, i want to check the parameter value contains either "Loss" or "Profit" in it.

It will be something like this.

#! /bin/sh

$input=$1
if [ <check whether $1 contains the strings specified> ]
then
<statement 1>
else
<statement 2>
fi

#! /bin/sh

input=$1
if [ "$input" = "Loss" ]
then
<statement 1>
elif [ "$input" = "Profit" ]
<statement 2>
fi

Hi,

I should do a pattern matching here against the two strings specified, not a direct comparison.

All solutions satisfy your requirements, you just need to modify them as per your requirements, we can give you a start or hint, not a complete solution. Do whatever you want as per the given hints.

I hope i'll be excused... I'm a Newbee and i'm confused which text processing command viz. sed,awk or any other can do a pattern matching.

I tried awk '{if match($1,"Loss")){print $1}', but it does not work.

Please help me.

you can do verification through sed? Something in the line like this

...
echo $1| sed -n '/Loss/p' > /dev/null 2>&1
if [ $? -eq 0 ];
   echo "Loss is there"
elif ...
....
....

there will be better ways to do this...just an idea for a start..

Thank you for helping me...can u pls tell me what is meant by dev/null.
I'm getting an error like dev/null: cannot create

Thanking you
Sumesh

it means redirection of unwanted output to a /dev/null special file, something like "Recycle bin" in your case most probably permission issue.

Matches a pattern rather an exact match.

[/tmp]$ cat ./try.ksh
#! /bin/ksh

arg="$1"

if [[ "$arg" = @(*[lL]oss*|*[pP]rofit*) ]] ; then
    echo "$arg"
else
    echo "No profit or loss"
fi;
    
[/tmp]$ ./try.ksh "Only Profit and no Loss"
Only Profit and no Loss
[/tmp]$ ./try.ksh "We broke even"
No profit or loss
[/tmp]$ 

Cool!! It is working fine. Can u pls modify the same for bourne shell.

Thanks a lot...
Sumesh

command=`grep "Managed Object" ${INPUT_FILE} | awk -F= '{print $NF}'|awk '{print $1}'`
echo $command

if [ "$command" = "HUAWEI_M2000_SYSTEM" ]
some times the above $command o/p is HUAWEI_M2000_SYSTEM HUAWEI_M2000_SYSTEM..so I want in if cdn to check if $command contains HUAWEI_M2000_SYSTEM it should work for then option...How to do this..
Similarly Iam getting o/p as MAINS FAIL...I need to remove the sapeces in between and convert to MAINSFAIL how to do this too..

Try using the case statement.