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!

Looping CLI commands for X iterations in BASH 1

Status
Not open for further replies.

czarj

Technical User
Apr 22, 2004
130
US
All-

I have a quick BASH scripting questions. Lets say I want to run the following commands X number of times:

ls -la /test
stat /test/file.test
whois bob
cat /test/file.test >> /tmp/testme
ect

How can I loop this is a BASH shell?


--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
bash supports a C-like for loop construct:

Code:
for (( i=1; i<=10; i++ ))
do
    ls -la /test
    stat /test/file.test
    whois bob
    cat /test/file.test >> /tmp/testme
done

Annihilannic.
 
I get syntax errors:

bash-2.04$ more loop
#!/bin/bash
for (( i=1; i<=10; i++ ))
do
ls -la /test
stat /test/file.test
whois bob
cat /test/file.test >> /tmp/testme
done



bash-2.04$ ./loop
./loop: line 2: syntax error near unexpected token `(('
./loop: line 2: `for (( i=1; i<=10; i++ ))'


--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Ah, it's a relatively new feature of bash I believe, perhaps v3 and higher.

Try this instead:

Code:
#!/bin/bash
i=1
while (( i<=10 ))
do
    ls -la /test
    stat /test/file.test
    whois bob
    cat /test/file.test >> /tmp/testme
    (( i=i+1 ))
done

Annihilannic.
 
That works, thanks!

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top