Link to home
Start Free TrialLog in
Avatar of Terry Rogers
Terry RogersFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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! :)
Avatar of arnold
arnold
Flag of United States of America image

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.
Avatar of noci
noci

LPTSTAT=` lpstat -pTERRYTST `
DISABLED=0
( echo $LPTSTAT | grep " disabled " ) && DISABLED=1
NEWLPT=0
( echo $LPTSTAT | grep " new  " ) && NEWLPT=1
if [ "$DISABLED" != 0 ]
then
  echo "Disabled"
fi

Open in new window

Avatar of Terry Rogers

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of noci
noci

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
#!/bin/ksh
LPTSTAT="printer TERRYTST disabled since Apr 10 08:36 -
   new printer
   fence priority : 0"
[[ "$LPTSTAT" == *disabled* ]] && DISABLED=1
echo DISABLED=$DISABLED
[[ "$LPTSTAT" == *" new "* ]] && NEW=1
echo NEW=$NEW
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?