Suggestions on this script please

i=1
  out=""
  j=`expr 2 * $1`
  while [ $i -le $1 ]
   do
    out="$out"#""
    echo $out
   ((i=i+1))
   done
  while [ $i -lt $j ]
  do
  print ${out%?}
  ((i=i+1))
  done

This script is throwing an error:

gurnish:/home/fnb/gurnish/saurabh/scripts> while1 3
expr: 0402-050 Syntax error.
#
##
###
while1[10]: test: 0403-004 Specify a parameter with this command.
gurnish:/home/fnb/gurnish/saurabh/scripts>

Expected O/P:

#
##
###
##
#

Plz Advise !!!

I dont see how your script would produce what you want since out isn changed in second loop...
The second error refers to while with j... your expr isnt correct: j=`expr 2*$1` so in fact there seem to be just that error...

while1 3
My logic is:
First Loop:
i=1 #
i=2 ##
i=3 ###
Second Loop:
j=2 * 3 =6
for 3<6 Out as it is ###
for 4<6 Out as it is last character trimmed ##
for 5<6 Out as it is last character trimmed #

So the final O/P:

#
##
###
###
##
#

Isn't it?

Rgds,
TS

I do suggest remove both back tics and expr
j=`expr 2 * $1`
and use
j=$((2 * $1))

This ((i=i+1)) can be shorten to ((i++))

Also, you need to reduce the length of out for each iteration of the second loop. Output command should be echo or printf not print:

i=1
  out=""
  ((j=2 * $1))
  while [ $i -le $1 ]
   do
    out="$out"#""
    echo $out
   ((i=i+1))
   done
  while [ $i -lt $j ]
  do
  out=${out%?}
  echo $out
  ((i=i+1))
  done

---------- Post updated at 10:29 PM ---------- Previous update was at 06:55 AM ----------

And here is a script to do a horizontal version:

  ##
 ####
######
i=1
while [ $i -le ${1:-3} ]
do
    out=${out}#
    printf "%${1:-3}s%s\n" $out $out
    ((i=i+1))
done

many thanks for the horizontal version script.However, I would like to ask that why my script is not working???

After making your changes below is the output

#
##
###
##
while1[14]: i++: 0403-053 Expression is not complete; more tokens expected.

Rgds,
TS

Cant answer like that we havent the new script...
About your previous remark and my previous post, to work as you wanted the value in the second loop needed to be decremented which you did not so it would work only once then you would have always the same...

Here is the output and the script...

n12:/home/vbe/test $ sh 9007
9007[5]: test: 0403-004 Specify a parameter with this command.
9007[12]: 2*: 0403-053 Expression is not complete; more tokens expected.
n12:/home/vbe/test $ sh 9007 4
#
##
###
####
###
##
#

# the script 9007:
let i=1
let j=0
  out=""
  j=`expr 2*$1`
  while [ $i -le $1 ]
   do
    out="$out"#""
    echo $out
   ((i=i+1))
  done
  while [ $i -lt $j  ]
  do
     out=${out%?}  # missing in your first script...
     print $out
    #  print ${out%?}     # original print...
     ((i=i+1))
  done
1 Like