How to make condition receive a parameter followed by an argument

Asked

Viewed 461 times

1

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

Example

$ opt-get -i pacote.tgz

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

I have made the condition to define and check 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

Commentary

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

On it the commands are executed only if the variable "$1" be equal to "-i" And so is the "$2" this to "tgz" e "gz".

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

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

I’ve been changing it for a long time. I don’t know where I could be going wrong.

1 answer

0


I make available the shell script using the concept of flow control commands, the if, which is based on the logic "if this happens, I will do it, if not, I will do it". 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 something relatively simple, close to the purpose of a package management system which is a collection of tools that provides a consistent method of installing, updating and removing software on your system. Meanwhile, it’s done!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.