How to use loop to repeat task?

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions How to use loop to repeat task?
# 1  
Old 10-28-2013
How to use loop to repeat task?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. How can i use loop to repeat task.



2.
Code:
shirt=15
black=13.50


echo "how many shirt you want"
read num
echo
echo "Please enter a choice"
echo "1 ---> normal shirt"
echo "2 ---> black shirt"
read choice
echo


if [ $choice = "1" ]; then
    price=`expr $num1 \* $shirt`
    echo "$num1 * $shirt = $price"


elif [ $choice = "2" ]; then
    answer=$(bc <<< "scale=2;$num*$black")
    echo "$num * $black = $answer"
else 
    echo "Invalid option"
fi


3. How can use loop to repeat the task five times.



4. Complete Name of School (Fiji National University), City (Valelevu), Country (Fiji), Name of Professor (Ronil Chand, and Course Number (CIN 546):


Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
# 2  
Old 10-28-2013
Hi,

Could you please try the following code.


Code:
$ cat ask_user.ksh
one=1
count=0
while [[ $count -lt $one ]]
do
echo "Enter the name of School"
read school_name
echo "Enter the university name:"
read university_name
echo "Enter country name:"
read country_name
echo "Enter name of professor:"
read professor_name
null=""
if [[ "$school_name" == "$null" || "$university_name" == "$null" || "$country_name" == "$null" || "$professor_name" == "$null" ]]
then
echo "Please enter the all entries again as there was a null entry."
else
echo $school_name
echo $university_name
echo $country_name
echo $professor_name
count=1
fi

done


It's output will be like as follows.


Code:
$ ksh ask_user.ksh
Enter the name of School
Enter the university name:
r
Enter country name:
r
Enter name of professor:
r
Please enter the all entries again as there was a null entry.
Enter the name of School


As you can see in Output there was an empty entry so it asked again for enter the values. Also to make a task 5 times repating you can take referance for same code. Hope this helps.




Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 10-28-2013
Many thanks to you... you the man

Thanks again

Taati

---------- Post updated at 09:06 PM ---------- Previous update was at 07:56 AM ----------

Hi R.Singh

Could you help me on this problem to use loop or while to repeat the task for 2 times, the code as follows
Code:
shirt=15
black=13.50


echo "how many shirt you want"
read num
echo
echo "Please enter a choice"
echo "1 ---> normal shirt"
echo "2 ---> black shirt"
read choice
echo


if [ $choice = "1" ]; then
    price=`expr $num1 \* $shirt`
    echo "$num1 * $shirt = $price"


elif [ $choice = "2" ]; then
    answer=$(bc <<< "scale=2;$num*$black")
    echo "$num * $black = $answer"
else
    echo "Invalid option"
fi

Output code as follows

Code:
how many shirt you want
2

Please enter a choice
1 ---> normal shirt
2 ---> black shirt
1

2 * 15 = 30
ictadmin@ubuntu:~/Desktop$

what i want is how to use loop or while to repeat the task to 2 times, so it can allows me to enter again for choice number 2

Thanks
# 4  
Old 10-30-2013
What shell are you using? (Some shells can perform floating point arithmetic without calling bc.)

Look at the man page on your system for the shell you're using. Search it for descriptions of the syntax of its for loop and while loop compound commands.

Consider adding a third choice to your menu to exit the loop when the user has completed calculating order totals.
# 5  
Old 10-30-2013
Moderator's Comments:
Mod Comment Akshay Hegde's earlier response has been hidden. It is not appropriate for members of this forum to complete homework assignments for students. This forum is intended to help students find answers for themselves.
# 6  
Old 10-30-2013
Oh I am so sorry Don Cragun.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

How to use xargs to repeat as a loop to grab date string?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: My goal to find how many requests in 14 days from weblog server. I know to cat a weblog file to wc -l to find the... (8 Replies)
Discussion started by: scopiop
8 Replies

2. Shell Programming and Scripting

Repeat for different variable

Hey, I've created following script: var1=test1 setA=testA if ... touch $setA/$var1 ... fi I would like now the repeat the command touch (in this example) for different variables. So below, the varX should run 3 times (var1, var2, var4). Var3 is skipped in this example... (4 Replies)
Discussion started by: brononius
4 Replies

3. Shell Programming and Scripting

Ping and repeat ?

How do i write a loop ping to see if it get timeout or hang ? it should loop every 30 second to ping a server ? ping -c 5 -t 15 www.google.com if ]; then date '+%Y-%m-%d %H:%M:%S Connection Unavailable' >> /home/sabercats/checkconnection.log else date '+%Y-%m-%d %H:%M:%S Connection... (3 Replies)
Discussion started by: sabercats
3 Replies

4. Shell Programming and Scripting

Repeat using for loop

I have a file like this 2011-10-10 10:46:00,1-1-13-1-1,151510,ALCLA0A84D2C 2011-10-10 10:46:00,1-1-13-1-1,151520,65537 2011-10-10 10:46:00,1-1-13-1-1,151515,46932 2011-10-10 10:46:00,1-1-13-1-1,151521,32769 2011-10-10 10:46:00,1-1-13-1-1,151522,32769 2011-10-10... (4 Replies)
Discussion started by: LavanyaP
4 Replies

5. Shell Programming and Scripting

case loop... repeat on bad input?

I'm trying to get a case statement to start over if an undefined option is selected... But I am ata loss on how to actually do it. Here is a quick example of what I have. Echo "1) do this/n 2) Do that/n 3) Quit/n Make a selection/n" Read answer Case answer in 1) Dothid;; 2) Dothat;;... (3 Replies)
Discussion started by: trey85stang
3 Replies

6. Shell Programming and Scripting

Parse an XML task list to create each task.xml file

I have an task definition listing xml file that contains a list of tasks such as <TASKLIST <TASK definition="Completion date" id="Taskname1" Some other <CODE name="Code12" <Parameter pname="Dog" input="5.6" units="feet" etc /Parameter> <Parameter... (3 Replies)
Discussion started by: MissI
3 Replies

7. UNIX for Dummies Questions & Answers

repeat each record n times

I have: aa01 aa02 aa03 aa04 ab01 ab02 ab03 ab04 I would like each record printed 5 times: aa01 aa01 aa01 aa01 aa01 aa02 aa02 (6 Replies)
Discussion started by: kenneth.mcbride
6 Replies

8. Shell Programming and Scripting

comment and Uncomment single task out of multiple task

I have a file contains TASK gsnmpproxy { CommandLine = $SMCHOME/bin/gsnmpProxy.exe } TASK gsnmpdbgui { CommandLine = $SMCHOME/bin/gsnmpdbgui.exe I would like to comment and than uncomment specific task eg TASK gsnmpproxy Pls suggest how to do in shell script (9 Replies)
Discussion started by: madhusmita
9 Replies

9. Shell Programming and Scripting

to copy and repeat

Hi All, I have done some looking at other threads but haven't found quite what I am looking for. I am a newbie to scripting and haven't got to where I want to you but here is my basic question. I have a script to copy a file and send it to another file with a date and time stamp. What I want to... (4 Replies)
Discussion started by: falcondown01
4 Replies

10. UNIX for Dummies Questions & Answers

Repeat Commands

On my system I use Escape "k" to go back in commands. I read on tutorials that it is ctrl p, but that does not work on my system. Anyone know what the command to go foward is? (6 Replies)
Discussion started by: dereckbc
6 Replies
Login or Register to Ask a Question