smitter
asked on
sell script
I am new to unix; basically I want t creat an equilateral triangle of numbers. I am able to create a rightangled triangled triangle with the following code:
#!/bin/sh
max_no=0
echo "ENter any number"
read max_no
i=1
s=max_no
while [ $i -le $max_no ]
do
sp=""
while [ $s -ge $i ]
do
sp=$sp""
s=`expr $s - 1`
done
j=1
while [ $j -le $i ]
do
sp=$sp$j
j=`expr $j + 1`
done
echo $sp
i=`expr $i + 1`
done
The output is :
ENter any number
3
1
12
123
I need to know, where exactly I have to modify my code to create an equilateral triangle. However, the problem can be solved in linux, using for loop.
Rgds smitter
#!/bin/sh
max_no=0
echo "ENter any number"
read max_no
i=1
s=max_no
while [ $i -le $max_no ]
do
sp=""
while [ $s -ge $i ]
do
sp=$sp""
s=`expr $s - 1`
done
j=1
while [ $j -le $i ]
do
sp=$sp$j
j=`expr $j + 1`
done
echo $sp
i=`expr $i + 1`
done
The output is :
ENter any number
3
1
12
123
I need to know, where exactly I have to modify my code to create an equilateral triangle. However, the problem can be solved in linux, using for loop.
Rgds smitter
What should the output look like?
ASKER
Hi JustUNIX,
the output should look like
1
1 2
1 2 3
the output should look like
1
1 2
1 2 3
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The problem with sh is that it takes of leading spaces when echoing environment variables with leading spaces.If u use tcsh and printenv ,setenv thing we are able to print leading spaces.
So to print leading spaces we can do the following
echo -n \ ;
-n is to to tell echo not to add newline character at the end of string.
\ and the space after that tell echo to print the space.
Again sh doesnot support the the -n option of echo so the biggest change i did to ur script is to change #!/bin/sh to #!/bin/bash.
#!/bin/bash
max_no=0
echo "ENter any number"
read max_no
i=1
s=max_no
while [ $i -le $max_no ]
do
sp=""
s=$max_no #u have to do this each time so puttin inside the loop.
j=1
while [ $j -le $i ]
do
sp=$sp" "$j #Added a space inbetween
j=`expr $j + 1`
done
#yanked these lines down.
#too lazy to put them back up and test.
while [ $s -ge $i ]
do
#sp=`echo \ $sp`
echo -n \ ;
s=`expr $s - 1`
done
echo $sp
i=`expr $i + 1`
done
ENter any number
10
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
HTH,
Gopu Bhaskar.
So to print leading spaces we can do the following
echo -n \ ;
-n is to to tell echo not to add newline character at the end of string.
\ and the space after that tell echo to print the space.
Again sh doesnot support the the -n option of echo so the biggest change i did to ur script is to change #!/bin/sh to #!/bin/bash.
#!/bin/bash
max_no=0
echo "ENter any number"
read max_no
i=1
s=max_no
while [ $i -le $max_no ]
do
sp=""
s=$max_no #u have to do this each time so puttin inside the loop.
j=1
while [ $j -le $i ]
do
sp=$sp" "$j #Added a space inbetween
j=`expr $j + 1`
done
#yanked these lines down.
#too lazy to put them back up and test.
while [ $s -ge $i ]
do
#sp=`echo \ $sp`
echo -n \ ;
s=`expr $s - 1`
done
echo $sp
i=`expr $i + 1`
done
ENter any number
10
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
HTH,
Gopu Bhaskar.