Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is it possible to format the STDERR in order to have a better looking menu using the select command?

I have a simple select

    select oChoice in $(<tempMenu.menu) ; do  
      case "$oChoice" in
          *) 
           break
          ;;
      esac
   done

I've tried a trick like:

    exec 3>&1
    select ...
    ...
    done 2>&1 1>&3 | sed 's/^/NICE OUTPUT    /'

But I cannot use escape sequences (i.e. colors), for example

   ...
   done 22>&1 1>&3 | sed 's/^/\033[1;33m\033[44mNICE OUTPUT    /'
   or
   done 22>&1 1>&3 | sed 's/^/\\033[1;33m\\033[44mNICE OUTPUT    /'

The escape sequences are not escaped and also the STDOUT is altered because I have customized the PS3 as well.

PS3="$(print \\n\\r)# $(print "\\033[1;33m\\033[44m")"$QUESTION"$(print "\\033[0m\\033[1;1m\\033[44m") `tput sc` $(print "")
#$(print "")
# $(print "")
# Status: $status $(print "")
# $(print "")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  `tput rc` "

As far as I understand (Show only stderr on screen but write both stdout and stderr to file) I cannot separate the STDERR from the STDOUT, so, is there a smarter way to create dynamic text-based menus just using the STDOUT? (or an otherway to workaround my issue)

share|improve this question
1  
consider editing your post to include the contents of tempMenu.menu. Hard to tell what your problem is without seeing what the inputs are. Good luck. –  shellter May 18 '12 at 17:37
    
Thanks @shellter , I've found a solution using dialog instead of select. I'll update the question, or answer myself (not sure what is better to do) as soon as I'll have time to do it. –  tmow May 22 '12 at 14:19
add comment

1 Answer

Another way is avoiding the select and make a menu. Make a shell script for each menu choice (action1.sh, xxx.sh and passwords.sh), configure them in menu.cfg:

1 action1 Do action 1
2 xxx Another nice item for you
3 passwords Very dangerous

And start a menu with a script that reads menu.cfg

#!/bin/ksh
formatmenu()
{
if [ $# -eq 2 ]
then
            printf "%d) %s\n" ${1} "$2" 
    else
            printf "    %s\n" "${1}"
    fi
}
showMenu()
{
    echo "Enter 0 to stop or choose beneath."
    cat menu.cfg|while read option proces description
    do
            formatmenu ${option} ${proces}
            formatmenu "${description}"
    done
}

getAction()
{
    showMenu
    while [ ${CHOICE} -eq -1 ]
    do
            read CHOICE?"Please enter a digit: "
            if [[ ${CHOICE} != +([0-9]) ]]
            then
                    CHOICE=-1
                    echo "Invalid, please enter a digit."
            fi
    done
}

performAction()
{
    if [ ${CHOICE} -eq 0 ]
    then
            return
    fi
    process=$(grep "^${CHOICE} " menu.cfg | cut -d\  -f2)


        echo "===${process}.sh==="
        . ${process}.sh
}

# Start

export CHOICE=-1
getAction
echo choice=${CHOICE} 
performAction
exit 0
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.