How to print in the new line?

I have written below script for the output:

1234
123
12
1
" {
 read x
 y=$x
 i=1
 while [ $i -le $x ]
 do
 j=1
 while [ $j -le $y ]
 do
 if [ $i == 1 ]
 then
 print -n $j
 fi
 j=`expr $j + 1`
 done
 i=`expr $i + 1`
 y=`expr $y - 1`
 done
 }"

However, I am getting o/p horizontally: 1234123121

Can anyone please help?

What o/p do you expect? Example

Hi there,

I got O/p from the above script as:

1234123121

I want it to be as shown below:

1234
123
12
1

Plz advise !!!

Using awk

echo "4" | awk '{for (j=$0;j>=1;j--) {for (i=1;i<=j;i++) printf i; print ""}}'
1234
123
12
1
{
   read x
   y=$x
   i=1
   while [ $i -le $x ]
   do
      j=1
      while [ $j -le $y ]
      do
         echo -n $j
         j=`expr $j + 1`
      done
      echo
      i=`expr $i + 1`
      y=`expr $y - 1`
   done
}

I have replaced the prints with echos and indented for reading purposes. YOu had an if construct which I assume you put in for debugging purposes.

Andrew

Shell should take input from the user instead of assuming it to be "4"

---------- Post updated at 08:44 AM ---------- Previous update was at 08:41 AM ----------

I should have quoted that I am working in KORN shell. I don't know how much implications it has on the syntax, but here is the O/P of your amended script.

3
-n 1
-n 2
-n 3

-n 1
-n 2

-n 1

Plz advise !

It works for ksh93 on Linux for me. Try putting the prints back in instead of the echos. What system are you running this on?

Andrew

I am on AIX 5.3 having Korn shell.

Furthermore, if you have noticed my original script contains "print" command only.

Plz advise !

As I said before try replacing my echo commands with print commands:

{
   read x
   y=$x
   i=1
   while [ $i -le $x ]
   do
      j=1
      while [ $j -le $y ]
      do
            print -n $j
         j=`expr $j + 1`
      done
      print
      i=`expr $i + 1`
      y=`expr $y - 1`
   done
}

Andrew

Thanks it worked !!

Can you explain the changes in the scripts and justifications of the same.

I simply used echo because I normally use bash, and print does not exist in bash. Indentation was put in to aid readability. The only real change was the print after the inner loop, to break the line where required.

Andrew

Hi Andrew,

I have the same question as in what is the purpose of the "Second print" that you used in the script.

Rgds,

Basically you wanted

1234
123
12
1

but were getting

1234123121

The inner loop was printing

1234

the first time followed by the shorter strings each iteration. You did this by forcing print to not print the newline character at the end of each print operation. At some point, though, you wanted a newline character to start the next iteration on a new line.

Andrew

Try also (feeling guilty about using the deprecated and dangeraous eval )

read ST
5
for i in $(eval echo {$ST..1}); do eval echo {$i..1}; done
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1