Help on "for" loop in bourne shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help on "for" loop in bourne shell
# 1  
Old 11-24-2009
Tools Help on "for" loop in bourne shell

Hello Everyone....

I am trying to print a number sequence in following format using for loop.

Quote:
1
22
333
4444
55555
I am using a bourne shell. I tried following for loop condition but it is bash syntax.

Code:
for (( i=0; i<=5; i++ ))

It is giving syntax error.

Kindly help with the syntax of "for" loop in order to print the above format.

Thanks in advance......
# 2  
Old 11-24-2009
in bourne, you can try the usual
Code:
for i in 1 2 3 4 5

# 3  
Old 11-24-2009
Thank you very mcuh for the reply....

I could use the above mentioned for loop syntax......

But the limit of the loop is unkonown i.e., in
Code:
 for i in 1 2 3 4 5

limit is only 5. What it should be for 'n' numbers ?

Secondly, with the above code is it possible to manipulate the value of i....i.e., if I want a decrement of 3 instead of 1......is it possible...

Thanks in advance.......
# 4  
Old 11-24-2009
Quote:
Originally Posted by EmbedUX
Thank you very mcuh for the reply....

I could use the above mentioned for loop syntax......

But the limit of the loop is unkonown i.e., in
Code:
 for i in 1 2 3 4 5

limit is only 5. What it should be for 'n' numbers ?

Secondly, with the above code is it possible to manipulate the value of i....i.e., if I want a decrement of 3 instead of 1......is it possible...

Thanks in advance.......

Hi, You could have a look at seq, man seq

Quote:
NAME
seq - print a sequence of numbers

SYNOPSIS
seq [OPTION]... LAST
seq [OPTION]... FIRST LAST
seq [OPTION]... FIRST INCREMENT LAST
Example:
Code:
master:~ # for x in $(seq 1 2 10);do for y in $(seq 1 $x);do echo -n $x; done;echo;done
1
333
55555
7777777
999999999
master:~ #


Best regards,
Lakris
# 5  
Old 11-24-2009
Another way:

Code:
i=1
while [ "$i" -le 10 ]
do
  echo "$i"
  i=$(( $i + 1 ))
done

# 6  
Old 11-24-2009
if seq doesn't work you can use a while or until loop and increment an index
Code:
I=1
until [ $I -eq $LIMIT ]
do
     let I+=1 # or let I=I+1 if the other syntax doesn't work
...
done

# 7  
Old 11-24-2009
Thank you for your replies.....

Lakris:
seq is a bash command. I am using a bourne shell......sh

Franklin52:
thank you for the code......
my task is to achieve it using for loop.

Thanks in advance.....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use 'expect' to pass UID & Password to a "for loop" in shell script?

Friends, Need someone's help in helping me with the below requirement for a script: > For a list of servers(over 100+), I need to login into each of them(cannot configure password-less ssh) & grab few configuration details < I know, this is possible through expect programming in a simple... (14 Replies)
Discussion started by: thisissouvik
14 Replies

2. AIX

How to use 'expect' to pass UID & Password to a "for loop" in shell script?

Friends, Need someone's help in helping me with the below requirement for a script: > For a list of servers(over 100+), I need to login into each of them(cannot configure password-less ssh) & grab few configuration details < I know, this is possible through expect programming in a simple... (2 Replies)
Discussion started by: thisissouvik
2 Replies

3. 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

4. Shell Programming and Scripting

How to increment date using "for loop" in format MMDDYY inside the shell script?

Need to increment the date from "currentdate + 90days" inside the for loop (i=1 to i=50) (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

9. Shell Programming and Scripting

read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell

Hallo, i need a Prompting read in my script: read -p "Enter your command: " command But i always get this Error: -p: is not an identifier When I run these in c-shell i get this error /usr/bin/read: read: bad option(s) How can I use a Prompt in the read command? (9 Replies)
Discussion started by: wiseguy
9 Replies

10. UNIX for Dummies Questions & Answers

"if exists" Bourne Shell

hi all! new to the forum and i love how nice alot of you are. and i was hoping to get some help. a friend of mine asked me to make him a script for backtrack which is based on slax. it makes a new changes.lzm for the usb system and merges 2 files to update it. now i got that handled but i cant seem... (2 Replies)
Discussion started by: pcstalljr
2 Replies
Login or Register to Ask a Question