LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-17-2008, 11:14 AM   #1
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Rep: Reputation: 15
How to use lpstat in an If statement?


I have figured out that I can use the lpstat -v <name of printer> (and -a) to see if the printer has been successfully installed. However, I am unsure how to use that information in an if statement.

How would I go about doing this?
 
Old 04-17-2008, 11:47 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
If you don't need output, then you only need the exit value (0, 1, 127, etc, etc). You can achieve that by piping output of the command through 'grep'. So your starting command is: "command 2>&1 | grep -q searchterm". Now you can use the exit value any way you like.

Code:
# With a "case" statement:
lpstat args 2>&1 | grep -m1 -q "searchterm"
case "$?" in
 0) got it;;
 *) echo no;;
esac

# Test:
lpstat args 2>&1 | grep -m1 -q "searchterm"
[ $? -eq 0 ] && echo got it || echo no

# Or incorporate it in your "if" statement:
if [ `lpstat args 2>&1 | grep -m1 -q "searchterm"` -eq 0 ]; then
 echo got it; else echo no
fi

# Or else just:
lpstat args 2>&1 | grep -m1 -q "searchterm" && echo got it || echo no

Last edited by unSpawn; 04-17-2008 at 11:50 AM. Reason: errors
 
Old 04-17-2008, 12:44 PM   #3
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
Quote:
# With a "case" statement:
lpstat args 2>&1 | grep -m1 -q "searchterm"
case "$?" in
0) got it;;
*) echo no;;
esac
this one seems the best solution for what i need, however is this correct:

Code:
lpstat -v $printname 2>&1 | grep -m1 -q "searchterm"
case "$?" in
 0) echo printer installed correctly;;
 *) echo printer was no installed correctly, please try again;;
esac
 
Old 04-22-2008, 06:59 AM   #4
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
anything on if i set this up correctly?
 
Old 04-22-2008, 08:33 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Looks OK to me. Couple of remarks. It could be lpstat isn't available, or doesn't return a valid result due to some error, and beware of 'grep' though, w/o "-i" you need to get case right, so maybe (YMMV, please test):
Code:
__determinePrinterInstall() { 
PRINTER="$1"; CMD="lpstat"; which $CMD >/dev/null 2>&1 || return 127
RESULT=`$CMD -v $printname 2>&1`; [ $? -eq 1 -o -z "${RESULT}" ] && return 127
echo "${RESULT}" | grep -m1 -qi "$PRINTER" >/dev/null 2>&1
}

__determinePrinterInstall "searchterm"
case "$?" in
 0) echo "[SUCCESS]: printer \"$PRINTER\" installed correctly.";;
 1) echo "[FAIL]: printer \"$PRINTER\" was no installed correctly, retry.";;
 127) echo "[FATAL]: \"${CMD}\" not available or ran without output, exiting.";;
 *) echo "[FATAL]: unspecified error, exiting.";;
esac
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
lpstat or something like it paul_mat Linux - Networking 0 05-09-2006 07:12 PM
lpstat and MACs stupendus Linux - Software 2 07-15-2005 02:34 AM
lpstat hang sstong Linux - Networking 0 10-28-2004 07:50 PM
lpstat hang sstong Linux - Software 2 10-28-2004 07:47 PM
lpstat hang sstong Linux - General 0 10-28-2004 03:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration