Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

print number pyramid with for loop in unix

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-11-2010
joshilalit2004 joshilalit2004 is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 11 February 2015, 9:07 AM EST
Posts: 29
Thanks: 11
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 09:17 AM.. Reason: code tags, please...
Sponsored Links
    #2  
Old Unix and Linux 05-11-2010
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 23 April 2015, 3:05 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 624 Times in 582 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 Unix and Linux 05-11-2010
joshilalit2004 joshilalit2004 is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 11 February 2015, 9:07 AM EST
Posts: 29
Thanks: 11
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 Unix and Linux 05-11-2010
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 April 2015, 6:26 PM EDT
Location: Saskatchewan
Posts: 20,522
Thanks: 880
Thanked 3,622 Times in 3,386 Posts
Why not a while loop?
Sponsored Links
    #5  
Old Unix and Linux 05-11-2010
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 23 April 2015, 3:05 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 624 Times in 582 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 Unix and Linux 05-12-2010
ygemici ygemici is offline Forum Advisor  
sed_shell@LNU
 
Join Date: Feb 2010
Last Activity: 24 April 2015, 10:01 AM EDT
Location: istanbul
Posts: 1,699
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 Unix and Linux 05-12-2010
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 23 April 2015, 3:05 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 624 Times in 582 Posts
Quote:
Originally Posted by ygemici View Post
Try this Unix or Linux Image

[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 Unix or Linux Image
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 11:17 AM
How print number bisla.yogender Shell Programming and Scripting 4 04-28-2008 06:51 AM
to print number one less than actual number cdfd123 Shell Programming and Scripting 4 09-06-2007 06:56 AM
How to ignore characters and print only number using unix? cdfd123 UNIX for Dummies Questions & Answers 5 08-03-2007 03:47 AM
Print Problem in UNIX. Need to know the option to specify the print paper size ukarthik HP-UX 1 06-07-2007 09:35 AM



All times are GMT -4. The time now is 04:58 PM.