Bash Shell loop - Help !


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Shell loop - Help !
# 1  
Old 07-15-2015
Bash Shell loop - Help !

Dear all Linux lover,

I am a new learner to Bash Shell script and I would like to writing a script to to repeat my script.
This mean I would like to have multiple same of result after running the .sh.


Code:
#######

TIMES_NO=0
echo -n "Please enter the number for times to repeat ?"
read TIMES_NO

######

When I start the program, it will prompt for enter the number for the TIMES_NO, after I enter a number, how to writing script to loop all my script inside the sh for the multiple times ?
$TIMES_NO

I have already searching for 10+ hours but failed to do...
Would you kindly help ?Smilie
many many thanks

Rocky

Last edited by Corona688; 07-15-2015 at 03:25 PM..
# 2  
Old 07-15-2015
Please use code tags as required by forum rules!

man bash:
Quote:
for (( expr1 ; expr2 ; expr3 )) ; do list ; done
First, the arithmetic expression expr1 is evaluated according to the rules described below under ARITHMETIC EVALUATION. The arithmetic expression expr2 is then evaluated repeatedly until it evaluates to zero. Each time expr2 evaluates to a non-zero value, list is executed and the arithmetic expression expr3 is evaluated. If any expression is omitted, it behaves as if it evaluates to 1. The return value is the exit status of the last command in list that is executed, or false if any of the expressions is invalid.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-15-2015
Quote:
Originally Posted by RudiC
Please use code tags as required by forum rules!

man bash:
Dear Rudic, thank you for your quick help, but I am not success to write correct script to do this, would you kindly take a look ?
My script in screen capture
www pbasehk com/stephen/temp/screen1
www pbasehk com/stephen/temp/screen2
Result
www pbasehk com/stephen/temp/result

thanks again for your help
I am so sorry the forum rules not allow to post the URL from my account..
# 4  
Old 07-15-2015
Please, don't post screen shots/pictures, as they can't be used for further analysis. Post code or result lines within code tags.

for (( i=W_NO; i>=W_NO; i--)) will execute exactly zero times, as the ending condition is immediatedly met. You may have had i>=0 in mind?
This User Gave Thanks to RudiC For This Post:
# 5  
Old 07-15-2015
I have to writing a script, when run it, prompt for width and height value, enter both value and then used to created a drawing pattern on the screen...

my current script is actually copy from another sample used create a triangle pattern...
I modify the value and hopes it can being a rectangle....

on your mentions script, it will execute 1 time and show one row on the screen, so I hopes to made it become multiple....thanks

---------- Post updated at 11:04 AM ---------- Previous update was at 11:02 AM ----------

or do you have any suggestion I can write a script to do that ?
# 6  
Old 07-15-2015
Are you looking for something basic as:

Code:
printf "Please enter the number of loops: "
read number

C=0
while [ "$C" -lt "$number" ]
do	echo "Loop nr: $C"
	((C++))
done

Place your code inside the loop.

hth
# 7  
Old 07-15-2015
I have tried to enter the code with my code
after I enter the number of loops
result show

Code:
Loop nr: 0

Code:
Loop nr: 1

Code:
Loop nr: 2

Code:
Loop nr: 3

please kindly help me, thank you so much
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

For loop in bash shell

Hi, I am using a for loop to manipulate files data_1.txt through data_100.txt. The for-loop is set up like this: for i in {1..100}; do cut -f1 data_$i.txt > output$i.txt I get the following error message when I run the code: cannot open `data.txt' for reading: No such file or directory... (4 Replies)
Discussion started by: evelibertine
4 Replies

2. Shell Programming and Scripting

"Command not found" doing a while loop in bash/shell

i=0 numberofproducts=${#urls} #gets number of entries in array called "urls" numberofproductsminusone=`expr $numberofproducts - 1` #-subtract by one while do wget ${urls} i=$(( $i + 1 )) sleep 10 done I'm getting an error ./scrape: line 22: [0: command not found that... (3 Replies)
Discussion started by: phpchick
3 Replies

3. Shell Programming and Scripting

Confusion about FOR LOOP syntax between Bourne and BASH shell. Please see.

for (( i=1; i<=3; i++ )); do for (( j=1; j<=3; j++ )); do for (( k=1; k<=3; k++ )); do echo $i$j$k done done done Will the above code work on a BOURNE shell? As far as my understanding is, if I am writing the above code in a file..say lol.sh and then running it through the terminal using... (7 Replies)
Discussion started by: navienavnav
7 Replies

4. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

5. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

6. Shell Programming and Scripting

How to use while loop in bash shell to read a file with 4 lines of gap

Hi , I am currently using the while loop in bash shell, as follows. while read line do echo $line done < file.txt However, i want to use the while loop on file.txt, which will read the file with 4 lines of gap. Ex- if file.txt is a file of 100 lines, then i want to use the loop such... (3 Replies)
Discussion started by: jitendriya.dash
3 Replies

7. UNIX for Dummies Questions & Answers

Anyone know?: How the 'for'-loop could stop working in interactive bash shell?!

It is happening with my sessions already second time: a 'for'-loop for some reason stop to work as expected. That means or it is looping without exitting, or it is not loop even once. Here example of my try when it is not processing even one loop. You can see, I start new subshell and... (14 Replies)
Discussion started by: alex_5161
14 Replies

8. Shell Programming and Scripting

if loop not working in BASH shell

i have this code for a simple if loop: #!/bin/bash array="1 2 3 4 5" array2="5 6 7 8 9" if } -gt ${array} ]; then echo "${array2} is greater than ${array}!!" fi the error is ./script8: line 9: [: too many arguments ./script8: line 9: [: too many arguments ./script8: line 9: [:... (10 Replies)
Discussion started by: npatwardhan
10 Replies

9. Shell Programming and Scripting

problem with while loop in BASH shell

I have file named script1 as follows: #!/bin/bash count="0" echo "hello" echo "$count" while do echo "$count" count=`expr $count + 1` done ----------- when I run it, I get ./script1: line 9: syntax error near unexpected token `done' ./script1: line 9: `done' I... (6 Replies)
Discussion started by: npatwardhan
6 Replies

10. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies
Login or Register to Ask a Question