print number pyramid with for loop in unix | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

print number pyramid with for loop in unix

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-11-2010
joshilalit2004 joshilalit2004 is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 28 January 2014, 3:01 PM EST
Posts: 18
Thanks: 2
Thanked 1 Time in 1 Post
print number pyramid with for loop in unix

How can I print number pyramid with for loop(not while only for) in unix like:

1
22
333
4444
55555

---------- Post updated at 09:09 AM ---------- Previous update was at 09:07 AM ----------

I forgot it is in ksh...I wrote a script in bash but it is nt wrkng in ksh...

bash script was:

Code:
#!/bin/sh
for ((i=1;i<=5;i++))
do
for ((j=1;j<=i;j++))
do
   echo -e  "$i \c"
done
echo ""
done


SO I need script which will work in ksh please

Last edited by pludi; 05-11-2010 at 10:17 AM.. Reason: code tags, please...
Sponsored Links
    #2  
Old 05-11-2010
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 26 November 2014, 8:26 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,675
Thanks: 184
Thanked 620 Times in 578 Posts

Code:
i=1 j=1
while ((i <= 5)); do
  while ((j <= i)); do
    print -n $i\ 
	j=$((j + 1))
  done
  print
  i=$((i + 1)) j=1 
done

There is a space after the \ character on the fourth line.
Sponsored Links
    #3  
Old 05-11-2010
joshilalit2004 joshilalit2004 is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 28 January 2014, 3:01 PM EST
Posts: 18
Thanks: 2
Thanked 1 Time in 1 Post
Thanks for the reply...

I am looking for the same programe with the for loop....I cannot use while loop......and I have to run the programe in ksh.....

Thanks in advance..
    #4  
Old 05-11-2010
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 27 November 2014, 11:46 AM EST
Location: Saskatchewan
Posts: 19,943
Thanks: 848
Thanked 3,420 Times in 3,205 Posts
Why not a while loop?
Sponsored Links
    #5  
Old 05-11-2010
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 26 November 2014, 8:26 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,675
Thanks: 184
Thanked 620 Times in 578 Posts
Quote:
Originally Posted by joshilalit2004 View Post
Thanks for the reply...

I am looking for the same programe with the for loop....I cannot use while loop......and I have to run the programe in ksh.....
[...]
If you get errors with the original code, you should be using a version of ksh different from ksh93. As far as I know ksh88 and pdksh do not support the following syntax:


Code:
for ((expr;expr;expr)) ...

As far as I know, all ksh implementations support the while loop ...
Sponsored Links
    #6  
Old 05-12-2010
ygemici ygemici is offline Forum Advisor  
sed_shell@LNU
 
Join Date: Feb 2010
Last Activity: 26 November 2014, 9:43 AM EST
Location: istanbul
Posts: 1,688
Thanks: 3
Thanked 289 Times in 281 Posts
PHP


Code:
#!/bin/ksh
 
for i in {1..5} ; do
j=$i
  while [[ $(( j -= 1 )) -gt -1 ]] ; do
       a=$a$i
    done
echo "$a"
a=""
done


Code:
# ./pyramid
1
22
333
4444
55555

Sponsored Links
    #7  
Old 05-12-2010
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 26 November 2014, 8:26 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,675
Thanks: 184
Thanked 620 Times in 578 Posts
Quote:
Originally Posted by ygemici View Post
Try this

[CODE]
for i in {1..5} ; do
[...]
The only ksh implementation that supports the above brace expansion is ksh93 ...

It also supports the for loop syntax mentioned in the original post.

So I suppose that this code will not work for the original poster
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Please help. How to print number with 'awk'? liuzhencc Shell Programming and Scripting 3 04-28-2010 12:17 PM
How print number bisla.yogender Shell Programming and Scripting 4 04-28-2008 07:51 AM
to print number one less than actual number cdfd123 Shell Programming and Scripting 4 09-06-2007 07:56 AM
How to ignore characters and print only number using unix? cdfd123 UNIX for Dummies Questions & Answers 5 08-03-2007 04:47 AM
Print Problem in UNIX. Need to know the option to specify the print paper size ukarthik HP-UX 1 06-07-2007 10:35 AM



All times are GMT -4. The time now is 01:02 PM.