14

Is there a method/command to check for the dependencies of a bash script? In other words, a response to this question : Which libraries should a user install to run the script?

I can do this manually by reading the script and check what other libraries/commands it calls but this not evident for long scripts.

0

3 Answers 3

8

The incredibly arcane autoconf system has been used to

produce shell scripts to automatically configure software source code packages. These scripts can adapt the packages to many kinds of UNIX-like systems without manual user intervention. Autoconf creates a configuration script for a package from a template file that lists the operating system features that the package can use, in the form of M4 macro calls.

If that is not sufficiently scary, wait until you try to write your first autoconf script. The authors of autoconf are not shy about its difficulty:

Those who do not understand Autoconf are condemned to reinvent it, poorly. The primary goal of Autoconf is making the user's life easier; making the maintainer's life easier is only a secondary goal. Put another way, the primary goal is not to make the generation of configure automatic for package maintainers; rather, the goal is to make configure painless, portable, and predictable for the end user of each autoconfiscated package. And to this degree, Autoconf is highly successful at its goal — most complaints to the Autoconf list are about difficulties in writing Autoconf input, and not in the behavior of the resulting configure.

On the other hand, you will come out of the process knowing more about the fiddly bits of various systems than you'd ever imagined existed.

These days, personally, I would assume a much more standard Debian-ish distribution was my target and not make myself write another autoconf script. I am lucky to have the luxury to choose that; you may not have such latitude.

6

This is not a simple task to automate, because a script may use constructs that defeat static analysis. If it ever uses eval or any prefix like time or nice, it won't be as simple as running something like egrep -o '^[^ ]+ ? ' to get commands and running them through which or type.

In the end, the only way to be absolutely sure is to run the script and find out what fails. If a script is well-written, it will check for non-standard commands before running. If not, trial-and-error is the only way to be certain.

Having said that, something like this could help:

#!/bin/bash
egrep -o -e '^[^ ]+ ? ' -e '[a-zA-Z0-9]+' "$1" | sort -u | {
    while read line
    do
        if type $line &>/dev/null
        then
            echo "$line found"
        else
            echo "Error: $line not found"
        fi
    done
} | sort

The output will look like:

$ ./check i_wonder.sh
cd found
echo found
elif found
else found
Error: abort not found
Error: checkurl not found
Error: cleanup not found
Error: count not found
Error: debug not found
Error: deleteFile not found
Error: die not found
find found
for found
grep found
if found
mv found
readarray found
rm found
shopt found
size found
sleep found
stat found
trap found
unset found
while found
0

You can get a list of .sh files from one folder by:

find \ordnerpfad -type f

If you find a way to get a list of your commands from your .sh files, you can check by follow, the commands are supported by your system:

compgen -ac | grep searchstr

To get a list of by .sh file used commands, perhaps are possible by one of the follow, in this form not working one samples:

Output a list of what commands the script use Perhaps its possible to create a list of used command by about follow:

set -n file_which_need_to_check.sh > found _commands.txt

Or by about the follow:

compgen -c file_which_need_to_check.sh > found_commands.txt

Or by the follow:

shellcheck -x or like this:

https://www.shellcheck.net/
https://github.com/koalaman/shellcheck

Please feel free to improve this.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.