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?
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

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

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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.
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?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.