How to get the result of the gdialog menu selection in the Shell Script variable?

Asked

Viewed 214 times

5

I’m creating a script simple learning where the user should:

  • Enter number that will be saved in the NUM1 variable
  • Enter a second number that will be saved in the NUM2 variable
  • Choose from the menu if it wants to add, subtract, multiply or split these numbers.

After these procedures, it should appear in the terminal "The option chosen was: sum (as for example, if he had chosen subtraction, subtraction would appear there [without accent because it is the variable I want to appear]).

Code I created:

#!/bin/bash

clear

NUM1=$( gdialog --inputbox "Informe o número 1"
gdialog --title 'Aviso' )

NUM2=$( gdialog --inputbox "Informe o número 2"
gdialog --title 'Aviso')

escolha=$( gdialog \
           --menu 'Escolha:' \
           0 0 0 \
           soma '+' \
           subtracao '-' \
           multiplicacao '*' \
           divisao '/' )

echo "A opção escolhida foi: $escolha"

exit

Problem: When I run, I do all the procedures as a user until the choice in the menu, appears in the terminal what was chosen, however, is not what I asked to appear and soon below appears "The chosen option was:" and does not appear the variable in front.

I would like this variable to appear so I see that it is actually being saved in $choice, so I can find a way to use the switch case and finish my script.

NOTE: Use gdialog for visual mode.

2 answers

4

I took the liberty of changing your script to a more streamlined and readable mode, so:

#!/bin/bash

operacao=""

echo "Digite o primeiro valor"
read valor1
sleep 3
echo ""
echo "Digite o segundo valor"
read valor 2
sleep 3
echo ""
echo "Escolha a operação: "
echo "1 - soma"
echo "2 - Subtração"
echo "3 - Multiplicação"
echo "4 - Divisão"
read operacao
case $operacao in
    1) operacao="Soma" ;;
    2) operacao="Subtração" ;;
    3) operacao="Multiplicação" ;;
    4) operacao="Divisão" ;;
    *) echo "Opção inválida" ; exit ;;
esac

echo "A opção foi: $operacao"
  • No problem, rsr... It’s just that I wanted in visual mode, hence the gdialog help in this.

  • 1

    Got it, I don’t know this function yet.

  • All right, thank you... ;)

1


Short answer: gdialog sends output to and STDERR; so you need to redirect STDERR to STOUT or join 2>&1 to the invocations of gdialog:

escolha=$(gdialog .......... 2>&1) 

Eventually the zenity

num1=$(zenity --title "Aviso" --entry --text "numero 1")

As a slightly different philosophy, I suggest the gtkdialog

#! /bin/bash

export MAIN_DIALOG='
 <vbox>
  <hbox>
    <entry> <variable>E1</variable> </entry>
    <entry> <variable>E2</variable> </entry>
  </hbox>
  <hbox>
    <button> <label>+</label> <action> echo $(($E1+$E2))</action> </button>
    <button> <label>-</label> <action> echo $(($E1-$E2))</action> </button>
    <button> <label>*</label> <action> echo $(($E1*$E2))</action> </button>
    <button> <label>/</label> <action> echo $(($E1/$E2))</action> </button>
    <button ok></button>
  </hbox>
 </vbox>'

gtkdialog --program=MAIN_DIALOG

Browser other questions tagged

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