need help using one variable multiple times


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help using one variable multiple times
# 1  
Old 11-21-2011
CPU & Memory need help using one variable multiple times

I apologize for the title but I am not even sure myself what to call this. I am going to use an example of a pizza delivery. I need to make an interactive script that allows users to order a certain number of pizzas, and then choose what they want on each pizza. Here is my code so far....

Code:
#!/bin/bash

#Set variable for number of pizzas
read -p "How many pizzas would you like to order?  " VarPizza


#Loop until user enters vaild tag number
until echo $VarPizza | grep -sq "^[+]*[0-9][0-9]*$" 
do
read -p "Please enter a valid integer " VarPizza
done


#This is where I need help. This needs to loop however many times, based on the amount of pizzas someone orders. So if $VarPizza = 3....it needs to loop 3 times so they can choose what is on the pizza

while true [ $VarPizza ]
do
read -p "What toppings would you like" Toppings
done

This is the part I am unsure of. I need to know how to have a loop that stores each "topping". Would I use an array? Thanks in advance for any help
# 2  
Old 11-22-2011
Use:
Code:
while [[ 0 -lt $VarPizza ]]
do
  read -p "What toppings would you like" Toppings
   (( VarPizza = $VarPizza - 1 ))
done

(Indenting inside of loops is highly recommended)

Also, for validating the input, you can save a call to grep with:
Code:
until [[ "$VarPizza" = [1-9]+([0-9]) ]]

This also prevents someone from entering "0" for the number of pizzas.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert one charactor multiple times in VI

Hi Gurus, I forgot the command which can insert one character multiple times. for example: I need 50 "#" #############... I used the command before, it is very convenient. anybody can help this. thanks in advance. (1 Reply)
Discussion started by: Torhong
1 Replies

2. Shell Programming and Scripting

Copy Column Multiple Times

Hello, I wonder if it my problem can be solved. Inside File1.txt, there are 3 columns. Inside File 2.txt, contain certain variable(in this case, only "3"). So I want to : (copy File 1 x Variable in File 2). Expected result are File 3.txt. Any help are really appreciated. File 1.txt -92.033... (4 Replies)
Discussion started by: guns
4 Replies

3. Shell Programming and Scripting

Running Multiple Times ?

hey mates, I was wondering if someone could assist me with this one, I have couple scripts that I would like to invoke from .sh ( One after another ) Script1 Script2 Script3 Is it possible to run script 2 after script one finished running ? And again start script 3 after script 2... (6 Replies)
Discussion started by: NDxiak
6 Replies

4. Shell Programming and Scripting

Need to run same script multiple times in parallel

Hi all, I have a requirement in which a script invokes a Java program. Lets say script ABC invokes a java program with cfg file a parameter. This script takes 10 minutes to execute . Like this ineed to run the program 10 times meaning 100 minutes if i do it sequentially. If i open... (2 Replies)
Discussion started by: rahman_riyaz
2 Replies

5. Shell Programming and Scripting

Doing one thing multiple times

Hi, I wrote a awk line here: awk 'BEGIN {OFS="\t"} {print $0, int(rand()*($2-$1 + 1) + $1) }' filename Basically what it does is that it takes the start (column 1) and stop (column 2) and generates a random # in between start and stop. I want to take this a step further and have it... (2 Replies)
Discussion started by: phil_heath
2 Replies

6. Shell Programming and Scripting

Running same script multiple times concurrently...

Hi, I was hoping someone would be able to help me out. I've got a Python script that I need to run 60 times concurrently (with the number added as an argument each time) via nightly cron. I figured that this would work: 30 1 * * * for i in $(seq 0 59); do $i \&; done However, it seems to... (4 Replies)
Discussion started by: ckhowe
4 Replies

7. UNIX for Dummies Questions & Answers

Crons executed multiple times.

For some reason my crons are being executed twice. Any suggestion?? I'm currently on 5.8 (2 Replies)
Discussion started by: shorty
2 Replies

8. Shell Programming and Scripting

Trying to read data multiple times

I am developing a script to automate Global Mirroring on IBM DS8100's. Part of the process is to establish a global copy and wait until the paired LUN's Out of Sync tracks goes to zero. I can issue a command to display the ouput and am trying to use AWK to read the appropriate field. I am... (1 Reply)
Discussion started by: coachr
1 Replies

9. Shell Programming and Scripting

Trying to read data multiple times

I am developing a script to automate Global Mirroring on IBM DS8100's. Part of the process is to establish a global copy and wait until the paired LUN's Out of Sync tracks goes to zero. I can issue a command to display the ouput and am trying to use AWK to read the appropriate field. I am... (0 Replies)
Discussion started by: coachr
0 Replies

10. Shell Programming and Scripting

matching multiple times in same line

Hi, I am stuck with pattern matching. I need to match a particular pattern several times in a same line and replace them. for ex., I need to convert (abc XY) (bvf ZY) bla bla to XY ZY bla bla I tried.. s/\(+ (.+)\)/$1/gi and it works (2 Replies)
Discussion started by: oldtrash
2 Replies
Login or Register to Ask a Question