maihabunash
maihabunash

Reputation: 1702

how to deliver the standard err immediately to parameter

the following simple script will display dialog GUI with 3 question and need to choose option 1/2/3

the problem is that I want to store the answer 1/2/3 in parameter not in file and then I take the val from the file

what I want is to deliver the standard err immediately to parameter

please advice how to set the standard err ( 2> ) in to parameter?

#!/bin/bash



  dialog    --backtitle "red-hat"  --title "[ computers ]" \
        --menu "Please choose an option:" 15 55 5  \
           1 "erase disk" \
           2 "clean disk" \
           3 "break disk" 2>/temp


       parameter=$(cat /temp )

what I want to get is like that:

    parameter=$( 2> )  .. something like this

Upvotes: 0

Views: 44

Answers (2)

anubhava
anubhava

Reputation: 785856

You can use process substitution to capture stderr in a variable in a subshell:

ls *.exe 2> >(read -r err; echo "ERROR :: $err")
ERROR :: ls: cannot access *.exe: No such file or directory

OR without creating sub shell:

read -u 2 -r err 2< <(dialog    --backtitle "red-hat"  --title "[ computers ]" \
        --menu "Please choose an option:" 15 55 5  \
           1 "erase disk" \
           2 "clean disk" \
           3 "break disk" 2>&1)

echo "ERROR :: $err"

Upvotes: 0

Dark Falcon
Dark Falcon

Reputation: 44201

Use $() and a bit of redirection:

parameter=$(dialog    --backtitle "red-hat"  --title "[ computers ]" \
    --menu "Please choose an option:" 15 55 5  \
       1 "erase disk" \
       2 "clean disk" \
       3 "break disk" 2>&1 >/dev/null)

Note that order is important. 2>&1 dups fd 1 to fd 2. >/dev/null redirects fd 1 so you don't get any of the output written on stdout.


In this case, dialog's stdout still needs to be the tty in order to display to the user. I would recommend you follow this guide and use the following redirection instead:

parameter=$(dialog    --backtitle "red-hat"  --title "[ computers ]" \
    --menu "Please choose an option:" 15 55 5  \
       1 "erase disk" \
       2 "clean disk" \
       3 "break disk" 2>&1 >/dev/tty)

Upvotes: 2

Related Questions