LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-07-2008, 04:36 PM   #1
musther
Member
 
Registered: Sep 2007
Posts: 36

Rep: Reputation: 15
Bash - suppress 'Command not found'


I've got a script which needs to test for the presence of certain programs, for example zenity.

Anyway, I'm testing like this:

Code:
echo -n "zenity             "
	zenity --help >> /dev/null
		if [ "$?" != "0" ]; then
			echo -en "\033[1;31m"
			echo "NOT FOUND"
			echo -e "\033[0m"
			ZOK=1
			OK=1
		else
			echo -en "\033[1;32m"
			echo "PRESENT"
			echo -e "\033[0m"
			ZOK=0
		fi
Obviously I don't want any output from zenity, so I'm using >> /dev/null to get rid of that. But if zenity isn't installed I still get the output.

Code:
bash: zenity: command not found
How can I suppress this output, so whether zenity is there or not, nothing is output on the command zenity --help

Thanks
 
Old 02-07-2008, 04:41 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
if you're already redirecting the stdout, just send stderr to stdout too...

Code:
# command_which -doesnt work 2>&1 > /dev/null
#

Last edited by acid_kewpie; 02-07-2008 at 04:42 PM.
 
Old 02-08-2008, 07:05 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
With all due respect, acid_kewpie's answer has a couple of issues.

Here is something that should do better:
Code:
type zenity >/dev/null 2>&1
if [ "$?" != "0" ]; then
...
 
Old 02-08-2008, 07:21 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
i was only looking at the stderr redirection. not the best way to check for a working application. But since when was i due respect?
 
Old 02-08-2008, 07:52 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
But since when was i due respect?
29,000 posts ought to be good for something......
 
Old 02-08-2008, 07:55 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by acid_kewpie View Post
i was only looking at the stderr redirection.
You should have reversed the redirection order for this to work. That was the first issue.
Quote:
not the best way to check for a working application.
Yes, the error wasn't sent by the command but by the shell, defeating stderr redirection goal.
Quote:
But since when was i due respect?
Everyone is due respect, especially mods with such a number of posts
And I wouldn't have liked my own posting to be reported
 
Old 02-08-2008, 08:47 PM   #7
JWPurple
Member
 
Registered: Feb 2008
Posts: 67

Rep: Reputation: 17
Code:
if [[ -x zenity ]]
then
   echo "PRESENT"
else
   echo "MISSING"
fi
is much easier.

Last edited by JWPurple; 02-08-2008 at 08:48 PM. Reason: rm dup comment
 
Old 02-08-2008, 09:48 PM   #8
musther
Member
 
Registered: Sep 2007
Posts: 36

Original Poster
Rep: Reputation: 15
Well it's easier so long as you know the path to zenity, and you know that will be consistent.

Thanks for all the advice, I have it working now (by redirecting stderr).
 
  


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
bash: g++: command not found m_lp_ql_m Linux - Software 8 07-08-2010 01:45 PM
Bash command not found Legolas327 Linux - Newbie 9 06-27-2007 07:44 PM
bash: rpm: command not found && sudo: alien: command not found Java_Code Ubuntu 7 07-27-2006 11:57 PM
bash: pg: command not found Chomper Programming 2 02-05-2005 02:22 AM
bash -command not found- K3V Linux - Newbie 7 09-30-2004 03:57 PM

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

All times are GMT -5. The time now is 08:18 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