How to make conditional receive a parameter followed by an argument

1

I've been trying to create a if..else structure with a passing parameter. See:

Example

$ opt-get -i pacote.tgz

  

This, I'm creating something similar to the apt-get tool of the Debian Operating System .

I have made the condition to set and verify the parameter:

if [ $1 = "-i" ]; then

Code

#!/bin/sh
#
# (c) 2016 - Diego Henrique Guilherme, <[email protected]>
#
# Programa - opt-get
#
# Aponta para o repositorio
URL=http://mirrors.slackware.com/slackware/slackware-8.1/slackware/tcl/

if [ $1="-i" -a $2="tgz" -o $2="gz" ]
then

     wget -c -qO- $URL$2 | sudo tar zxv -C /

else


     wget -c -P /tmp/ $URL$2

fi

Comment

Note that I use the "-a" (E) parameter to include more than one condition within the same function,

In it the commands are executed only if the variable "$1" is equal to "-i" And just like "$2" is to "tgz" e "gz" .

In another possibility I use "-o" (OU) to make the command run if any of the "tgz", "gz" extension conditions is true.

As you can see, I created the shell script aesthetically similar to apt-get Debian OS , having only the main task to download the package and immediately if the "-i" parameter is passed. If the "-i" parameter is not passed, it simply drops the package in the /tmp folder, does not install.

I've been altering for some time .. I do not know where I might be going wrong.

    
asked by anonymous 03.12.2016 / 23:22

1 answer

0

I put the shell script using the concept of flow control commands, if , which is based on logic "if this happens, I will do this, otherwise I will do that. " See:

#!/bin/sh
#
# (c) 2016 - Diego Henrique Guilherme, <[email protected]>
#
# Programa - opt-get
#

# Apontar para repositório
URL=http://mirrors.slackware.com/slackware/slackware-8.1/slackware/tcl/

# Detectar Formato do pacote
EXT=$(echo $1 $2)

# Verificar extensão do pacote
if [ ${EXT##*.} != "tgz" ]
then
echo "Formato de pacote inválido. Verifique e tente novamente."

# Caso não seja passado parâmetro válido, descontinuar
elif [ $1 != "-i" -a $1 != "-d" ]
then

echo "Ops! opção inválida."

# Mostrar Auto-Ajuda caso nada seja argumentado
elif [ -z $1 ]
then

cat << "EOF"

Modo de Usar: opt-get [-id] <package.tgz>

Opções de Parâmetros válidos

-i  É um acrônimo da palavra - install
-d  É um acrônimo da palavra - download

NOTA! - Não será preciso colocar as duas opções ao mesmo tempo, 
uma vez que cada qual, tem por finalidade baixar o pacote.

A diferença fica por conta de "-i" baixar e auto-instalar
A opção "-d" somente baixa o pacote diretamente para /tmp

EOF

# Baixar e Instalar pacote 
elif [ $1 = "-i" ]
then

wget -c -qO- $URL$2 | sudo tar zxv -C /

# Somente Baixar o pacote
elif [ $1 = "-d" ]
then

wget -c -P /tmp $URL$2

fi
  

This is relatively simple, close to the purpose of a package management system that is a collection of tools that provides a consistent method of installing, updating, and removing software on your system. In the meantime, it's done!

    
04.12.2016 / 07:54