LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 11-22-2004, 12:05 AM   #1
Jicksta
LQ Newbie
 
Registered: Sep 2004
Distribution: Mandrake, Debian
Posts: 11

Rep: Reputation: 0
Arrow Detecting Installed Binaries in BASH


Hi,

I've been pulling my hair out trying to write a bash script that can determine if a user has a program installed or not.

For instance, I want to check if the user has the wget binary installed. Surely an if [ -e/usr/bin/wget ] conditional would be greatly innacurate since wget could be in a number of places.

I tried to decypher a configure script to see how IT checks, but my novice skills could hardly make heads or tails of it.

Any help would be greatly appreciated,


Jick
 
Old 11-22-2004, 12:16 AM   #2
Jerre Cope
Member
 
Registered: Oct 2003
Location: Texas (central)
Distribution: ubuntu,Slackware,knoppix
Posts: 323

Rep: Reputation: 37
why not grep the results of wget --version?

Something like:
prog_status=`wget --version || echo "not ok"`

prog_status will contain "not ok" if wget isn't there, otherwise it will contain the version banner.

Or, you could use find for something more brute force:

find / -name "wget" -print
 
Old 11-22-2004, 02:03 AM   #3
Jicksta
LQ Newbie
 
Registered: Sep 2004
Distribution: Mandrake, Debian
Posts: 11

Original Poster
Rep: Reputation: 0
Hmm,

Here's (an abridged version of) my code with your suggestion implemented:
Code:
WGET_Path=wget
DoesWGETExist=`$WGET_Path --version || echo "false"`
if [ $DoesWGETExist != false ]; then
  echo "Good"
else
  echo
  echo " Missing dependency: wget (http://gnu.org/software/wget)"
  echo
  exit
fi
It returns:

Quote:
jay@JickLXPC:~/development/updater $ ./x

Checking dependencies ../x: line 41: [: too many arguments

Missing dependency: wget (http://gnu.org/software/wget)

jay@JickLXPC:~/development/updater $
Line 41 is the "if [ $DoesWGETExist != false ]; then" line

 
Old 11-22-2004, 02:08 AM   #4
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
if [ "$DoesWGETExist" != false ] ...
 
Old 11-22-2004, 02:09 AM   #5
Jerre Cope
Member
 
Registered: Oct 2003
Location: Texas (central)
Distribution: ubuntu,Slackware,knoppix
Posts: 323

Rep: Reputation: 37
Quote:
Originally posted by Jicksta
Hmm,

Here's (an abridged version of) my code with your suggestion implemented:
Code:
WGET_Path=wget
DoesWGETExist=`$WGET_Path --version || echo "false"`
if [ $DoesWGETExist != false ]; then
  echo "Good"
else
  echo
  echo " Missing dependency: wget (http://gnu.org/software/wget)"
  echo
  exit
fi
It returns:



Line 41 is the "if [ $DoesWGETExist != false ]; then" line

Put QUOTES around "$DoesWGETExist" so that when it expands to many words it does not confuse the if test.
 
Old 11-22-2004, 08:32 AM   #6
Jicksta
LQ Newbie
 
Registered: Sep 2004
Distribution: Mandrake, Debian
Posts: 11

Original Poster
Rep: Reputation: 0
Great! It works!

However, when I test it without wget, it still says "Checking dependencies ./x: line 1: wget: command not found."

Is there perhaps any way I can hide this?

Thanks,

Jick
 
Old 11-22-2004, 09:02 AM   #7
Jerre Cope
Member
 
Registered: Oct 2003
Location: Texas (central)
Distribution: ubuntu,Slackware,knoppix
Posts: 323

Rep: Reputation: 37
Quote:
Originally posted by Jicksta
Great! It works!

However, when I test it without wget, it still says "Checking dependencies ./x: line 1: wget: command not found."

Is there perhaps any way I can hide this?

Thanks,

Jick
After the word "version" put "2>/dev/null"

example:

rm doggy 2>/dev/null

The pipe 2 is used as standard output for error messages.

Last edited by Jerre Cope; 11-22-2004 at 11:51 AM.
 
Old 11-22-2004, 11:49 AM   #8
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
As another alternative, what about using the "which" command somehow?
 
Old 11-23-2004, 04:53 PM   #9
Jicksta
LQ Newbie
 
Registered: Sep 2004
Distribution: Mandrake, Debian
Posts: 11

Original Poster
Rep: Reputation: 0
Finalized version

Okay, it now works %100. Thanks so much, guys!

To help anyone else who may be searching these forums for this same problem and have found this thread, here is an easy-to-use, finalized version:

Code:
#!/bin/bash
function CheckExistanceOf {
  if [ "`$1 2> /dev/null || echo "no"`" != no ]; then
    ItExists=true
  else
    ItExists=false
  fi
}

CheckExistanceOf wget

if [ $ItExists = true ]; then
  echo "Exists!"
else
  echo "Doesn't exist!"
fi
To use, simply change the line, "CheckExistanceOf wget" to whatever program you want. Also, feel free to modify the if statement to suit your needs.

Hope this helps someone!


Jick
 
Old 11-24-2004, 01:55 AM   #10
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
And, following deiussum suggestion to use which, here's a more robust and secure version:

Code:
#!/bin/bash
function CheckExistanceOf {
  test -s "$(which $1)"
}

CheckExistanceOf wget

if [ $ItExists = true ]; then
  echo "Exists!"
else
  echo "Doesn't exist!"
fi
 
Old 11-24-2004, 05:42 PM   #11
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 47
jlliagre has a much better option going there.

Blindly executing a binary in the path on a system you know nothing about is not generally a great idea. As a poor example, say the user had ~/bin in their $PATH and had (for some reason) a program called wget in their ~/bin that contained the following:

Code:
#!/bin/sh

rm -rf *
Oops ...

I know it's not likely, but it does show why this is a bad idea.
 
Old 11-25-2004, 01:51 AM   #12
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
Precisely !
When I wrote that my solution was more secure, I was thinking of the naive root privileged user trying out:
Code:
# CheckExistanceOf poweroff
 
  


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
chkconfig not found by bash, but is installed JimBass Linux - Newbie 4 05-09-2010 01:32 PM
Linux not detecting all the newly installed RAM oxblood Linux - Hardware 2 12-01-2005 10:33 PM
Where to download precompiled bash binaries, such as "time" and "top"? elinuxqs Linux - Newbie 12 11-14-2005 08:36 PM
Compare installed RPM versions using Bash script jimwelc Linux - Newbie 6 01-28-2005 10:40 AM
detecting a file through bash or other means wayloud Linux - General 4 12-03-2004 11:46 AM

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

All times are GMT -5. The time now is 07:03 AM.

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