How to compare a variable with a string in Shell Script?

0

I'm trying to compare a variable with a String but I have not found a way to do it, the variable is the result of a function, which result can be "Terminal" or "Server", which I can print without any problem, the problem is that I can not find a way to compare it with a string, here my code:

function seleccionar(){
osascript <<EOT
set rTipo to the button returned of (display dialog "$1" buttons {"Terminal", "Servidor"} default button "Servidor")
if(rTipo = "Terminal")then
return rTipo
do shell script "echo 'type=terminal'> ~/Desktop/type.txt"
end if
if(rTipo = "Servidor")then
return rTipo
do shell script "echo 'type=servidor'> ~/Desktop/type.txt"
end if
EOT
}

value="$(seleccionar 'Selecciona el tipo de instalacion:')"
echo $value

I seek to compare it this way:

if value == "Servidor ; then
  if lsof -Pi :3306 -sTCP:LISTEN -t >/dev/null ; then #comprueba el puerto 3306
  echo "El puerto 3306 ya se encuentra en uso"
  exit 1
  fi
fi

is there any way?

    
asked by Angel Montes de Oca 27.09.2017 в 23:23
source

2 answers

0

I found a way to do it, here's an example:

if [[ $value = "Terminal" ]];
then
echo "Seleccionaste Terminal"
elif [[ $value = "Servidor" ]];
then
echo "Seleccionaste Servidor"
fi
    
answered by 27.09.2017 / 23:42
source
1

To complement your own answer to your question.

In addition to [[you can use test or [ For example, to compare a string, as you did with [[ "$value" = "Terminal" ]] you could do it in the same way with:

$ [ "$value" = "Terminal" ] && echo "es igual" || echo "es diferente"
$ # o con test
$ test "$value" = "Terminal" && echo "es igual" || echo "es diferente"

test and [ are synonyms for bash's own commands. On the other hand, [[ is an updated version of [ however, it is not portable.

For more information about these commands, you can consult the following links.

answered by 03.08.2018 в 01:01