Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Generic User Avatar

How can I check if a program exists from a Bash script?


  • Please log in to reply
2 replies to this topic

#1 veera004

veera004

  •  Avatar image
  • Members
  • 5 posts
  • OFFLINE
  •  
  • Local time:10:39 AM

Posted 15 June 2023 - 05:55 AM

How would I validate that a program exists, in a way that will either return an error and exit, or continue with the script?

It seems like it should be easy, but it's been stumping me.

 
 

 



BC AdBot (Login to Remove)

 


#2 SleepyDude

SleepyDude

  •  Avatar image
  • Malware Response Team
  • 4,172 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Portugal
  • Local time:06:09 AM

Posted 15 June 2023 - 06:19 AM

Sorry you are looking for Bash not Batch!


Edited by SleepyDude, 17 June 2023 - 09:38 AM.

• Please do not PM me asking for support. Post on the forums instead it will increases the chances of getting help for your problem by one of us.
• Posts in the Malware section that are not replied to within 4 days will be closed. PM me or a moderator to reactivate.
• Please post your final results, good or bad. We like to know! Thank you!

 
Proud graduate of GeekU and member of UNITE
___
Rui

 
 


#3 pSYCHOtRAIL

pSYCHOtRAIL

  •  Avatar image
  • Members
  • 3 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:West-Central Pennsylvania, USA
  • Local time:01:09 AM

Posted 16 June 2023 - 05:48 PM

 

How would I validate that a program exists, in a way that will either return an error and exit, or continue with the script?

It seems like it should be easy, but it's been stumping me.

 
 

 

 

# here are several tests because some packages need different approaches. these are all written for use in BASH scripts. you can see a hodgepodge of tests below so you can pick which works best for several different types of packages.
 ## ## ## ## ## ## ## ## ## ##
# this first section works in well over 90% of cases for me.
hit="insert-package-name-here" # a test to see if $hit is installed and if not, install it with permission in another script, assuming user has sudo rights. "${hit}" retains the package-name for every instance below.
command -v "${hit}"
Status=$?
    {
    if [ "${Status}" -ne "0" ]; then
hit2="" # paste a good package description between the " marks
 x-terminal-emulator -e "${HOME}/Bash_Scripts/install_package.sh" "${hit}" "${hit2} "${hit3}" "${hit4}" # I have a script named install_package.sh that handles the installation. this passes the package name and a good description, may need to pass more variables at times: "${hit}" "${hit2}""${hit3}" "${hit4}" I have the installation script file set up to handle as needed.
    fi
    }
 ## ## ## ## ## ## ## ## ## ##
apt-get install "${hit}" # go ahead and install the package if that suits your case
 ## ## ## ## ## ## ## ## ## ##
# alternate forms without installing:
command -v "${hit}" >/dev/null 2>&1 || { echo "I require ${hit} but it's not installed.  Aborting." >&2; exit 1; }
 ## ## ## ## ## ## ## ## ## ##
dpkg-query -s "${hit}" >/dev/null 2>&1 # returns 0 if installed, 1 if not
 ## ## ## ## ## ## ## ## ## ##
dpkg-query -W -f='${Status}' "${hit}" | grep -q " installed" # returns 0 if installed
 ## ## ## ## ## ## ## ## ## ##
if which "${hit}" >/dev/null; then
    echo "${hit} is installed on your system"
else
    echo "${hit} is not installed on your system"
fi
 ## ## ## ## ## ## ## ## ## ##
if type "${hit}" >/dev/null 2>&1; then
    echo "${hit} is installed on your system"
else
    echo "${hit} is not installed on your system"
fi
 ## ## ## ## ## ## ## ## ## ##
if dpkg -s "${hit}" >/dev/null 2>&1; then
    echo "${hit} is installed on your system"
else
    echo "${hit} is not installed on your system"
fi
 ## ## ## ## ## ## ## ## ## ##
 
I forgot, never run a script or a command until you understand what it is and what it does. Some nasty people will deliberately give you something that could trash your system for meanness.

Edited by pSYCHOtRAIL, 16 June 2023 - 05:51 PM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users