D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] standard error handling

 

On 01/09/2019 16:44, Paul Sutton wrote:
> Hi
> 
> 
> I am trying to write a shell script in whiptail ( this is a graphical
> toolkit for bash)
> 
> whiptail --title "Check list example" --checklist \
> "Choose user's permissions" 20 40 4 \
> "Hello" "Print Hello" OFF \
> "Goodbye" "Print Goodbye" OFF \
> "CYA" "Print cya" OFF \
> 
> CHOICEs=$?

$? is the exit status code, a number from 0 to 255.  See `man bash`
section "EXIT STATUS" and "Special Parameters".  Use that to tell if the
command ran OK or not BUT it won't have the choices.

> echo $CHOICEs
> 
>     if $CHOICEs = "Hello" ; then
>         echo "you selected hello"
>     elif $CHOICEs = "Goodbye" ; then
>         echo "You Said Good bye"
>     elif i$CHOICES = "CYA" ; then
>         echo "You said cya"
>     fi

What you need to do is a little redirection dance to swap over stdout
and stderr on the whiptail command.  Then assign a variable to the
(standard) output of whiptail.  So:

CHOICEs=$(whiptail .... 3>&2 2>&1 1>&3)

Then "CHOICEs" will contain space separate list of tags in double
quotes.  You'll have parse that; bash does regex! :)

Another option is output stderr to file and then read that in later.

M.

> This produces a small menu with check boxes (so the first part works), 
> I can select what I want and press Ok.
> 
> I am not really sure how do handle the output and the 2nd part of this
> script doesn't work as expected.  But I don't fully understand stderr
> which is the how whiptail outputs to the console / terminal.
> 
> Using the information at (
> https://en.wikibooks.org/wiki/Bash_Shell_Scripting/Whiptail#Check_list i
> 
> This just tells me how to handle the graphical part of this.   Not how
> to handle what is produced.
> 
> Clearly the example above is meant to echo to the screen something
> relating to the options selected,  however as echo can be replaced with
> other commands I am just keen to get this working on a very basic level
> before I try and do something a little more complex.
> 
> Can anyone help please.
> 
> thanks
> 
> Paul
> 


-- 
FSF member #9429
http://www.fsf.org/register_form?referrer=9429
http://www.fsf.org/about
"The Free Software Foundation (FSF) is a nonprofit with a worldwide
mission to promote computer user freedom and to defend the rights of all
free software users."

Attachment: signature.asc
Description: OpenPGP digital signature

-- 
The Mailing List for the Devon & Cornwall LUG
https://mailman.dclug.org.uk/listinfo/list
FAQ: http://www.dcglug.org.uk/listfaq