KSH If statement.

How can I search get if to pinpoint 1 word in a line and have it do something for me?

example:

KEY1="<< Response ... Total of 2 >> Sun Jun 19 15:30:18 2011 Tx Power Level is 27.7 Bm ~ "

if [[ $KEY1 = "Tx" ]];
then command;
else error;
fi

Thats just a quick sample. I want my if statement to se the "Tx" in Key1 then issue the command but I can't seem to get it to search key1 just for Tx , I'm switching over to ksh since csh seems to be broken with if statements.

Would this work for you?

if [[ "$KEY1" =~ Tx ]]; then
  echo Do something
else
  echo Do something else
fi

If not, then something like:

if grep -q Tx <(echo $KEY1); then
  ...
else
  ...
fi

Else, perhaps

if [ "$(echo "$KEY1" | grep Tx)" ]; then
  ...
else
  ...
fi

Getting a grep: illegal option -- q when I try to do use the ones with grep. Thanks for helping though :slight_smile: I'm new and still learning. Still having trouble though, couldn't get any of the ones you recommended to work =/

Sounds like you must be using Solaris?

Try with:

/usr/xpg4/bin/grep -q

(assuming neither of the other options work)

Assuming it's Solaris, the safest bet would be the third option.

---
edit: Tested with ksh on Solaris 10:

if /usr/xpg4/bin/grep -q Tx <(echo $KEY1); then
  ...
else
  ...
fi

You edited your post after I submitted mine. It's better if you reply, otherwise the conversation get's all garbled and confusing.

Please state the version of Solaris you are using (or which OS, if not Solaris), and confirm that you are using the Korn shell.

Solaris 10, I'll give what you quoted a try thanks for the assist again :slight_smile:

---------- Post updated at 04:28 PM ---------- Previous update was at 04:24 PM ----------

Awesome that did the job, thanks for the help!