[SOLVED] bash function to check if script dependencies are installed...
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.
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...
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?
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.