grep ONLY a word in ksh

sminfo
sminfo used Ask the Experts™
on
Couple of questions in ksh:

1.How can I grep exactly a word or string  using grep?

Example: I want to grep ONLY FULL 'string1' after lsrole command output, but ONLY grep exactly 'string1'. I want to grep fails if 'string' or 'stri' or 'stgrin' is passed.

lsrole ALL|grep '^string1'

desired output:
string1 blah blah

2. How to do an 'echo' AND 'exit' if lsuser command fails? (NOTE: I'd like to hide stdout or stderr)
Example:

lsuser -a $user >/dev/null 2>&1 || (echo "Usuario no valido en el sistema. Abortando." && exit 1)

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sorry I can help you with your second Q,
as for 1 try:

 lsrole ALL|grep '^string1$'

SnowFlake

Author

Commented:
It doesn't work the way you say::

 lsrole ALL|grep '^string1$'

I can't get my  desired output 'string1'

ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
do you want
'string1'
or
string1 blah blah

What does
lsrole ALL
produce?

if
lsrole ALL
produces a line
string1 blah blah
then the commend you gave in your question
lsrole ALL|grep '^string1'
would produce the output
string1 blah blah

Author

Commented:
let me be more clear, the 'string1' must be a valid role and it's passed as an argument to lsrole cmd. Something like this:

echo "Write role name:"
read role2del

lsrole ALL|grep "^$role2del" || exit 1

BUT, if you passed 'stri' or 'string' grep matches and 'stri' or 'string' when they are not valid roles. What I want?, just grep 'string1' which is the valid role.

understand now?


lsrole ALL output:

opcsia authorizations= rolelist=scropcsia groups= visibility=1 screens=* msgcat= id=51
oraccsia authorizations=csia.sistemas.admin.cmdscripts rolelist= groups= visibility=1 screens=
permcsia authorizations=aix.fs.object.mode,aix.fs.object.owner rolelist= groups= visibility=1 s
rbaccsia authorizations=aix.security rolelist= groups= visibility=1 screens=* msgcat= id=32
restcsia authorizations=csia.sistemas.admin.cmdscripts rolelist= groups= visibility=1 screens=
sa authorizations=aix.system.config.acct,aix.system.config.cron,aix.system.config.src,aix.sys

Most Valuable Expert 2013
Top Expert 2013
Commented:
Hi,
1) try grep -w
This will lead to grep performing a "word search".
2)
lsuser -a $user >/dev/null 2>&1 || (echo "Usuario no valido en el sistema. Abortando." && exit 1)

This will not work because "exit" only leaves the subshell started inside the "(  )"
You will have to go with "if ... then"
lsuser -a $user >/dev/null 2>&1
RC=$?
if [ $RC -eq 2 ] ; then
     echo "Usuario no valido en el sistema. Abortando."
     exit 1
   elif  
      [ $RC -gt 2 ] ; then  
      echo "Command failed. Abortando."
     exit 2
fi  
wmp
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
since there is no line that starts with
string1
in
opcsia authorizations= rolelist=scropcsia groups= visibility=1 screens=* msgcat= id=51
oraccsia authorizations=csia.sistemas.admin.cmdscripts rolelist= groups= visibility=1 screens=
permcsia authorizations=aix.fs.object.mode,aix.fs.object.owner rolelist= groups= visibility=1 s
rbaccsia authorizations=aix.security rolelist= groups= visibility=1 screens=* msgcat= id=32
restcsia authorizations=csia.sistemas.admin.cmdscripts rolelist= groups= visibility=1 screens=
sa authorizations=aix.system.config.acct,aix.system.config.cron,aix.system.config.src,aix.sys


lsrole ALL|grep '^string1'
will match nothing

in any case
lsrole ALL|grep '^string1'
will never match 'string' or 'stri' or 'stgrin'

Author

Commented:
ozo,   sorry.. string1 could be any of:

opcsia
oraccsia
etc

now, asi I said 'string1' is passed to lsrole output,  so, you can passed 'stri' or 'strin' and I only want lsrole ALL|grep '^string1' succeeds only if 'string1' is passed.

I think, as wmp says, with -w works.. let me do some tests... thanks..

Author

Commented:
Hi wmp,

Why do you use exit status of =2 or >2 instead -ne 0? like:

RC=$?
if [ $RC -ne 0 ] ; then
     echo "Usuario no valido en el sistema. Abortando."
     exit 1
fi  
Most Valuable Expert 2013
Top Expert 2013

Commented:
Please don't forget the "^" in front of $string!
grep -w does indeed a word search, but treats any non-alphanumeric character as a delimiter,
so
grep -w ^csia will (desirably) fail,
while
grep -w csia
will succeed (due to "=csia." contained in some lines)
Most Valuable Expert 2013
Top Expert 2013

Commented:
Hi,
our postings crossed, sorry.
An exit status of 2 means that the given userid actually doesn't exist.
On the other hand, an exit status of >2 (here 22) means that the command syntax is wrong, in this case maybe due to $user being empty.
Your version will most probably be OK for most purposes, but the output message is not quite correct in every case.
Sorry for being such a nitpicker!
wmp

Author

Commented:
umm.. I see.. where can I get exit status, in my case, aix/ksh?
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
As you say,
lsrole ALL|grep ^string1
succeeds only if string1 is passed, it does not match stri or strin
it also matches string123 but not if you use
grep -w string1
with the ^ it will only match at the beginning of the line

on your second question, you might use someting like
if lsuser -a $user >/dev/null 2>&1 ;then;else
  echo "Usuario no valido en el sistema. Abortando."
  exit 1
fi
Most Valuable Expert 2013
Top Expert 2013

Commented:
>> where can I get exit status <<
Unfortunately, we don't have an exhaustive documentation of returncodes in AIX, and there isn't a command to get them listed.
Some returncodes can be found in the manpages, and some more are documented here -
http://publib.boulder.ibm.com/infocenter/aix/v6r1/topic/com.ibm.aix.cmds/alphabeticallistofcommands.htm
but in many cases the only way is to check the returncode immediately after a command failed, to then relate the number found to the message the failing command displayed.
wmp
 
Most Valuable Expert 2013
Top Expert 2013

Commented:
... and often the AIX developers use returncodes as specified in the standard (ANSI/XOPEN/POSIX) error code header file /usr/include/sys/errno.h
saying for error number 2
ENOENT  2       /* No such file or directory            */
and for 22
EINVAL  22      /* Invalid argument                     */
which is somewhat "close to the truth" in the case of "lsuser".
wmp
 
 
 

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial