PDA

View Full Version : how to use loop



Taati_Barao
October 30th, 2013, 03:53 AM
Hi Everyone


Could anyone help me on how to use loop to repeat the task for 2 times,

the code as follows



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

The output code



how many shirt you want
2

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

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

Please anyone can tell me where to put loop inside my script, where it can repeat the task for 2 times or more.

Thanks

Taati

cariboo
October 30th, 2013, 04:20 AM
Moved to programming talk.

papibe
October 30th, 2013, 04:31 AM
Hi Taati_Barao.

This should point you in the right direction:

for i in {1..2}
do
echo $i;
done
The output would be:

1
2
Regards.

Taati_Barao
October 30th, 2013, 08:24 AM
Hi Papibe

Thanks for that hint

Taati