Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Number Input arguments inside infinite while loop 1

Status
Not open for further replies.

FedoEx

Technical User
Oct 7, 2008
49
US
Code:
#!/bin/bash
while :   #infinite loop
do
echo " what is your choice .."  
read ch                        #expects to read only one input
echo " ok your choice is " $ch #execute the simple function with the ch arg
done
Is there a way to do the code above for a random number of input arguments inside the while loop.
If I were to supply the input arguments on the command line I could have used.

Code:
 #!/bin/bash
 for  ch in $@ ;do
 echo " ok your choice is " $ch
 done
I guess I need to define the for loop above as a function and then call that function inside the while loop.
 
Hi

Not sure what you are trying to accomplish, but sounds like the case for a [tt]while[/tt] - [tt]read[/tt] loop :
Code:
[gray]#!/bin/bash[/gray]

echo [green][i]" what is your choice .."[/i][/green]  
[b]while[/b] [COLOR=chocolate]read[/color] ch[teal];[/teal] [b]do[/b]
  echo [green][i]" ok your choice is "[/i][/green] [navy]$ch[/navy]
  echo [green][i]" what is your choice .."[/i][/green]  
[b]done[/b]


Feherke.
 
I need to process each supplied argument separately in the while loop.When you run the while code above...
Code:
what is your choice ..
a b c
 ok your choice is  a b c
 what is your choice ..
b c
 ok your choice is  b c
 what is your choice ..
it takes a b c as one argument.
I need to threat them as separate arguments.
The desired output is

Code:
what is your choice ..
a b c
 ok your choice is  a
 ok your choice is  b
 ok your choice is  c
 what is your choice ..
b c
 ok your choice is  b 
 ok your choice is  c
 what is your choice ..

 
So you need a for loop inside the while loop.

Something like this:
Code:
#!/bin/bash

echo " what are your choices .."  
while read ch; do
  for c in $ch; do
    echo " ok your choice is " $c
  done
  echo " what are your choices .."  
done

HTH,

p5wizard
 
Yes that is exactly what I need.
It is easier than I thought.
I was trying via using separate function like.
Code:
 for  ch in $@ ;do
 echo " ok your choice is " $ch done
Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top