How to extrapolate text from Multi-Line Command Response in Korn Shell Script

Currently writing a management menu interface for managing printers on our HP UX system.

Scripts are written in Korn Shell.

The following command
lpstat -pTERRYTST

Open in new window

returns the following:-

printer TERRYTST disabled since Apr 10 08:36 -
        new printer
        fence priority : 0

Open in new window

I need to be able to search the response for keywords to identify states.

So I need to fill a variable with 1 if the printer is disabled, another variable with 1 if its new.

No idea how to complete this in Korn Shell scripting as am very new to the language.

Help! :)
Terry RogersIT Senior EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You could pass the response to a while, do read line ; evaluate the string, do stuff; done
grep to extract string.

I'm partial to perl for processing text

Using ksh, you can use system commands, including grep assignment, pattern extraction
Test to evaluate....

I've seen multi-line pattern matches, but have not mastered so could not say .....

Going line by line with the match on printer as the start of a line as an indication that the prior printer info is complete.
nociSoftware EngineerCommented:
( echo $LPTSTAT | grep " disabled " ) && DISABLED=1
( echo $LPTSTAT | grep " new  " ) && NEWLPT=1
if [ "$DISABLED" != 0 ]
  echo "Disabled"

Open in new window

Terry RogersIT Senior EngineerAuthor Commented:
Hi noci,

This seems to work well with one exception.

The line:-
( echo $LPTSTAT | grep " disabled since" ) && DISABLED=1

Open in new window

Causes the output to be printed to screen.

Is there a way to not have this printed to the screen?

Thank you.
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

nociSoftware EngineerCommented:
( echo $LPTSTAT | grep " disabled since" >/dev/null ) && DISABLED=1

Open in new window

To also suppress errors:
( echo $LPTSTAT | grep " disabled since" >/dev/null 2>&1 ) && DISABLED=1

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LPTSTAT="printer TERRYTST disabled since Apr 10 08:36 -
   new printer
   fence priority : 0"
[[ "$LPTSTAT" == *disabled* ]] && DISABLED=1
[[ "$LPTSTAT" == *" new "* ]] && NEW=1
echo NEW=$NEW
Terry RogersIT Senior EngineerAuthor Commented:
Hi Ozo,

Thank you for you solution.

What are the differences or benefits your solution offer over noci's ?

noci, Is Ozo's solution a better way to accomplish this?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.