ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.