Verifique as dependências do script bash / shell

11

Existe um método / comando para verificar as dependências de um script bash? Em outras palavras, uma resposta a essa pergunta: Quais bibliotecas um usuário deve instalar para executar o script?

Eu posso fazer isso manualmente lendo o script e verificando quais outras bibliotecas / comandos ele chama, mas isso não é evidente em scripts longos.

    
por 4m1nh4j1 18.05.2014 / 12:52

2 respostas

6

O incrivelmente arcano sistema autoconf foi usado para

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.

Se isso não for suficientemente assustador, espere até tentar escrever o seu primeiro script autoconf. Os autores do autoconf não têm vergonha de sua dificuldade :

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.

Por outro lado, você sairá do processo sabendo mais sobre as partes complicadas de vários sistemas que você jamais imaginou existir.

Hoje em dia, pessoalmente, eu diria que uma distribuição Debian-ish muito mais padronizada era o meu alvo e não me faria escrever outro script autoconf. Tenho a sorte de ter o luxo de escolher isso; você pode não ter essa latitude.

    
por 18.05.2014 / 15:09
2

Esta não é uma tarefa simples para automatizar, porque um script pode usar construções que anulam a análise estática. Se ele usar eval ou qualquer prefixo como time ou nice , não será tão simples quanto executar algo como egrep -o '^[^ ]+ ? ' para obter comandos e executá-los por meio de which ou type .

No final, a única maneira de ter certeza absoluta é executar o script e descobrir o que falha. Se um script for bem escrito, ele verificará os comandos não padronizados antes da execução. Se não, tentativa e erro é a única maneira de ter certeza.

Dito isto, algo assim poderia ajudar:

#!/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

A saída será parecida com:

$ ./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
    
por 19.05.2014 / 15:50