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-18-2016, 04:08 AM   #1
masavini
Member
 
Registered: Jun 2008
Posts: 285

Rep: Reputation: 6
bash function to check if script dependencies are installed...


hi,
all of my bash scripts start with something like this:
Code:
dependencies=('jq' 'wget')
checkScriptDependencies "${dependencies[@]}"
here is the code for checkScriptDependencies:
Code:
checkScriptDependencies () { # "${dependencies[@]}"
	local _packages=( "$@" )
	local _package=
	for _package in ${_packages[@]}; do
		hash $_package
		if (( $? > 0 )); then
			sudo apt-get -y install $_package
		fi
	done
}
it works pretty good, but it will fail if the command name doesn't match the package name.
hxnormalize and hxselect, for example, are installed with html-xml-utils package.

if html-xml-utils is not installed, then, hash hxnormalize will fail but sudo apt-get install hxnormalize will fail as well...

could you suggest how to deal with such a case?

thanks!

Last edited by masavini; 02-18-2016 at 04:28 AM.
 
Old 02-18-2016, 05:46 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,901

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
I do not really understand what do you need, but apt-get install --print-uris <package> will print all the missing/required packages
 
Old 02-18-2016, 06:14 AM   #3
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
well, it's pretty simple...

the aim is to check if a command can be executed. if not, install the required package.

if the command is hxnormalize, sudo apt-get install hxnormalize will not find hxnormalize package as it does not exist: hxnormalize is part of html-xml-utils package.

using hxnormalize as input, how can i install html-xml-utils package?
 
Old 02-18-2016, 06:15 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,901

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
that is: apt-cache search hxnormalize
 
Old 02-18-2016, 06:17 AM   #5
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
why don't check if 'wanted_binary' exists, with whichor test, and only search for it if it's not found,

however,
http://manpages.ubuntu.com/manpages/...g-query.1.html

can also be callled via dpkg

http://manpages.ubuntu.com/manpages/...n1/dpkg.1.html
-S --search option

Quote:
-S, --search filename-search-pattern...
Search for a filename from installed packages.
so it seems this is what you want

Quote:
dpkg -S hxnormalize
 
Old 10-23-2017, 07:30 PM   #6
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
Quote:
apt-cache search hxnormalize
Quote:
dpkg -S hxnormalize
those commands only work if html-xml-utils package is already installed.

in my case, however, html-xml-utils is not installed yet (otherwise hxnormalize command would already be executable!).


i know this thread is pretty old, but i'm resurrecting it just because i finally found a (quite ugly, but working) solution:

Code:
$cat script.bash:
#!/usr/bin/env bash

# this script needs 'hxnormalize' and 'mvn' commands.


# this function checks if its arguments can be executed.
# if they're not, it installs the repository package providing each missing command.
# if a missing command has multiple candidate packages (i.e. mvn, or mysql), ask which should be installed.

function install_if_needed () {

    local needed_packages=()
    local package_name
    local command_name
    for command_name in "$@"; do

      # if this command is already executable, do nothing and continue with the next
      hash "${command_name}" 2>/dev/null \
        && continue

      # if the command is not installed, execute command-not-found builtin and save the stderr to a file
      /usr/lib/command-not-found -- "${command_name}" &> error.txt \
        || true


      # if error.txt contains the string '${command_name}: command not found', it means that
      # the command does not exist in any repository package and can not be installed
      grep -q "${command_name}: command not found" error.txt \
        && echo "command '${command_name}' does not exist in any package. exiting..." > /dev/tty \
        && return 1


      # otherwise error.txt contains the package name:
      # $ cat error.txt
      # The program 'transmission-edit' is currently not installed. You can install it by typing:
      # sudo apt-get install transmission-cli
      package_name=$(awk '/^sudo apt-get/{print $NF}' error.txt)


      # if var $package_name is empty, it means several candidate packages are found:
      # $ cat error.txt
      # The program 'mysql' can be found in the following packages:
      #  * mysql-client-core-5.5
      #  * mariadb-client-core-5.5
      #  * mysql-client-core-5.6
      #  * percona-xtradb-cluster-client-5.5
      # Try: sudo apt-get install <selected package>
      if [[ -z "${package_name}" ]]; then

        local candidate_packages=$(awk '/^ \* /{print $NF}' error.txt)

        echo -e "\nselect a package to install for '${command_name}' command:" > /dev/tty
        select package_name in ${candidate_packages}; do
          break
        done

      fi

      needed_packages+=("${package_name}")

    done


    # if no package is needed, just return
    [[ "${needed_packages[0]:-}" ]] || return 0


    # otherwise update and install
    sudo apt-get -qq update \
      && sudo apt-get --quiet --assume-yes install "${needed_packages[@]}"

    return 0
}


dependencies=('hxnormalize' 'mvn')
install_if_needed "${dependencies[@]}"

exit 0

what do you think about?
 
Old 11-05-2017, 05:43 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
The apt-file program can be used to learn which packages contain a given program/filename.

It would probably not be the cleanest option though. apt-file would need to be installed first and its database kept up-to-date, then the output would need to be parsed to get the name(s) you need.

A package search also came up with auto-apt. It's not clear from the description whether it can be used to supply script dependencies:

Quote:
auto-apt checks the file access of programs running within its environments, and if a program tries to access a file known to belong in an uninstalled package, auto-apt will install that package using apt-get. This feature requires apt and sudo to work.

It also provides simple database to search which package contains a requested file.

Other than that, I say it's probably just better to list out the dependencies the script needs and let the user figure the rest out.
 
  


Reply

Tags
apt-get, hash, ubuntu



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
[SOLVED] Bash function check for valid IP jchambers Programming 7 04-10-2020 04:51 AM
[SOLVED] Bash script to check if file is present or not, check periodically every 30 mins Iyyappan Linux - Server 10 07-03-2013 05:19 AM
[SOLVED] Bash script to check ioCube and suphp is installed waha87 Programming 2 10-25-2011 11:27 PM
How to check linked dependencies of installed prog? Megamieuwsel Linux - General 6 10-16-2003 06:30 AM

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

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