Terry Rogers
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
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! :)
Scripts are written in Korn Shell.
The following command
lpstat -pTERRYTST
returns the following:-printer TERRYTST disabled since Apr 10 08:36 -
new printer
fence priority : 0
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! :)
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
ASKER
Hi noci,
This seems to work well with one exception.
The line:-
Is there a way to not have this printed to the screen?
Thank you.
This seems to work well with one exception.
The line:-
( echo $LPTSTAT | grep " disabled since" ) && DISABLED=1
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#!/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
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
ASKER
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?
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?
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.