LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-14-2013, 04:01 PM   #1
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Using while loop & select statement - Loop issues


This is a progression from previous threads but they aren't really related anymore so creating a new one here. this is my code:

Code:
while read -r VCENTER DC CLUSTER HOST VMNAME
do
        case "$DC" in
        removed)
                echo -e "Found $VMNAME in removed"
                echo $VMNAME >> $NEWMRLVMS
                MRLVMS=$(($MRLVMS + 1))
                ;;
       removed)
                echo -e "Found $VMNAME in removed"
                PHXVMS=$(($PHXVMS + 1))
                echo $VMNAME >> $NEWPHXVMS
                ;;
        *)
                echo -e "$RED Unable to identify data-center for $VMNAME $NORMAL"
                ;;
        esac


        while [ "$MRLVMS" -ne 0 ]
        do

                echo -e "Please select the proxy below to add $VMNAME to:"
                select MRLPROXY in "${Mrl100Array[@]}" ; do
                        case  $MRLPROXY in
                        *_100_sun)
                        echo -e "Adding $VMNAME to removedon Sunday's schedule"
                        MRLVMS=$(($MRLVMS - 1))
                        ;;
                        *_100_mon)
                        echo -e "Adding $VMNAME to removedon Monday's schedule"
                        ;;
                        esac

                done

        done


done < $VMLIST
Removed a few things for security reasons but the all the code is there. The problem I have is with the second while loop. I want to have a loop that will iterate through and ask the user to select a command with the select builtin, the case statement would then match the pattern chosen from the select menu and then decrease the integer by one and the loop would re-run again until the integer decreases to 0.


When I run the code in its current form it does loop but it does not stop and wait for the user to make a selection. It seems like the while loop is not waiting for response whats teh best way to achieve this sort of looping method with a menu?

Last edited by Kustom42; 05-14-2013 at 04:03 PM.
 
Old 05-14-2013, 05:21 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Original Poster
Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Made some changes and exported my variables and tried creating a second while loop but still having no luck:

Code:
        while read -r VMNAME
        do
                if [ "$MRLVMS" -ne 0 ]
                then
                        select MRLPROXY in "${removed[@]}" ; do
                                case  $MRLPROXY in
                                *_100_sun)
                                echo -e "Adding $VMNAME to removed on Sunday's schedule"
                                MRLVMS=$(($MRLVMS - 1))
                                ;;
                                *_100_mon)
                                echo -e "Adding $VMNAME to removed on Monday's schedule"
                                ;;
                                esac

                        done
                fi
        done < $NEWMRLVMS
It now will only echo the array once but still does not wait for user response on the select statement?? I also tried using a an echo & read combo but same result.

Here is the output from the loop:
Code:
1) 27_103_sun     7) 22_103_sat   13) 26_101_fri   19) 19_102_thur
2) 18_103_mon     8) 29_101_sun   14) 21_101_sat   20) 26_102_fri
3) 18_103_tue     9) 30_101_mon   15) 18_102_sun   21) 21_102_sat
4) 20_103_wed    10) 30_101_tue   16) 30_102_mon
5) 21_103_thur   11) 30_101_wed   17) 18_102_tue
6) 24_103_fri    12) 23_101_thur  18) 21_102_wed
#? #? #? #? #? #? #? #? #? #? #? #? #? #? #? #? #? #? #?
You can see that the select is sent to the terminal 19 times which is the integer value of $MRLVMS so the loop part of the loop works just fine just need to figure out a way to get it to stop on each iteration of the loop and wait for user input so I can execute commands based on that input.
 
Old 05-15-2013, 02:46 AM   #3
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
You can't put a read inside a read loop. You'll need to redirect stdin to a new file descriptor and then read the second loop from the new fd.
 
Old 05-17-2013, 07:36 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by gnashley View Post
You can't put a read inside a read loop. You'll need to redirect stdin to a new file descriptor and then read the second loop from the new fd.
This might be a solution:
Code:
#!/bin/bash

{ while read file; do
  read -p "do you know about $file? " answer 0<&3
  echo "you said '$answer'."
done < <(ls); } 3<&0
Kevin Barry
 
Old 05-17-2013, 08:43 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Or just use read -u:
Code:
while read -u 4 LINE; do
    read ...
done 4< input.file
Of course it's only available in Bash 3.0+.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sleep inside for loop inside while loop causing issues. TheOnlyQ Programming 13 12-19-2012 12:59 PM
if statement in a for loop dsmith8890 Programming 6 05-31-2012 01:29 AM
Need help with for loop + if statement Thaidog Programming 5 05-06-2011 09:54 PM
BASH Need help figuring out how to loop SELECT statement mcdrr Programming 2 05-08-2007 04:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration