LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Select order (https://www.linuxquestions.org/questions/programming-9/select-order-804213/)

webhope 04-26-2010 03:43 AM

Select order
 
Hi, I have some problem with displaying items of select. All items shoul be on separete line.

Code:

clear; IFS="\n";
    job=('Rename a file ' "move" "copy" "find" "view" "edit"  "next");
    echo -e $AUTO_FINISH$'\E[1;37mJob:\E[1;0m';
    select vyber in "${job[@]}"; do
        case $vyber in
          ${job[0]}) krok="rename"; ;;
          ${job[1]}) krok="move"; ;;
          ${job[2]}) krok="copy"; ;;
          ${job[3]}) krok="find"; ;;
          ${job[4]}) krok="view"; ;;
          ${job[5]}) krok="edit"; ;;
          ${job[6]}) krok="continue";  break; ;;
        esac;
    done;

My output:
Code:

Job:
1) Rename a file  3) copy            5) view            7) next
2) move            4) find            6) edit

I Can find out whats wrong

colucix 04-26-2010 04:05 AM

Take a look at the COLUMNS bash variable. You can try to set it to a low value, before the select construct.

catkin 04-26-2010 04:15 AM

Nothing is wrong! :) The select statement is working as expected (assuming the prompt to choose one of the options was snipped from what you posted).

webhope 04-26-2010 04:21 AM

Quote:

Originally Posted by catkin (Post 3947868)
Nothing is wrong! :) The select statement is working as expected (assuming the prompt to choose one of the options was snipped from what you posted).

But problem is I want every line separate. If I change 'Rename a file ' to 'Rename' everhing is ok. However if spaces are included then it makes trouble.

I try to find something about COLUMNS variable but nothing valuable I have.

colucix 04-26-2010 04:53 AM

Quote:

I try to find something about COLUMNS variable but nothing valuable I have.
Take a look at man bash under the "Shell variables" section.

webhope 04-26-2010 05:02 AM

All I found:

COLUMNS
Used by the select builtin command to determine the terminal width when printing
selection lists. Automatically set upon receipt of a SIGWINCH.

Clever
It works
COLUMNS='\n'
Thanks

catkin 04-26-2010 05:13 AM

The behaviour of select is not defined. Here's a "roll-your-own" alternative
Code:

job=('Rename a file ' 'move' 'copy' 'find' 'view' 'edit'  'next')

while true
do
    echo 'Choose by number...'
    for (( i=0; i<${#job[*]}; i++ ))
    do
        echo "$i ${job[i]}"
    done
    read
    if [[ ! $REPLY =~ ^[0-9]+$ || $REPLY -lt 0 || $REPLY -ge ${#job[*]} ]]; then
        echo "Invalid choice '$REPLY'".  Try again.
    else
        break 2
    fi
done

# $REPLY holds job index


colucix 04-26-2010 06:14 AM

Quote:

Originally Posted by webhope (Post 3947899)
It works
COLUMNS='\n'

Good it works. Anyway, COLUMNS should be set to a number of columns, not a character. The alternative suggested by Catkin should fit your needs, as well.


All times are GMT -5. The time now is 04:18 PM.