#!/bin/ksh
i=1
while [ $i -le 5 ]
do
j=0
while [ $j -lt $i ]
do
echo "$i"
j =`expr $j + 1`
done
i=`expr $i + 1`
done
There should not be any blank space around assignment operator:
j=`expr $j + 1`
There is a blank between "j" and "=" where it is not supposed to be:
j=`expr $j + 1`
j =`expr $j + 1`
# should be:
j=`expr $j + 1`
Thanks all guys,
I am newbie here and it seems you guyz blasted it within seconds.These spaces are killing in Shell scripting.
And guyz can you tell how to check for an error while executing a shell script as a person who wrote a script is unaware where the error lies.
Rgds,
TS
It's just like debugging in general ... try to simplify your script until it starts to work, then add functionality until the problem shows up. Your last change is probably the cause of the problem.
You can set xtrace / verbose to debug the script:
#!/bin/ksh -xv
So while running the script I need to type SCRIPT.sh -xv.
You meant to say that?
Rgds,
TS
---------- Post updated at 10:55 AM ---------- Previous update was at 10:55 AM ----------
Guys,
Output for the script is :
1
2
2
3
3
3
4
4
4
4
5
5
5
5
5
But I want to have
1
22
333
4444
55555
Can you suggest the amendments?
Rgds,
TS
You can simply modify the shebang in your script:
#!/bin/ksh -xv
i=1
while [ $i -le 5 ]
do
j=0
while [ $j -lt $i ]
do
echo "$i"
j=`expr $j + 1`
done
i=`expr $i + 1`
done
Use the -n
option of the print-builtin
print -n $j
The echo command is not portable in this respect. Some systems use echo -n text
others echo text\c
.
#!/bin/ksh
i=1
while [ $i -le 5 ]
do
j=0
out=""
while [ $j -lt $i ]
do
out=$out$i
j=`expr $j + 1`
done
echo "$out"
i=`expr $i + 1`
done
i=`expr $i + 1`
This can be change to
((i=i+1))
or ((i++))
print -n option gives me below output:
122333444455555
However, I need output
1
22
333
4444
55555
Did you try my suggestion??
Thanks boss !!! This thing worked.
But please if possible could you explain the usage of OUT in the above script?
out=""
out=$out$i
echo "$out"
Rgds,
TS
Nothing...
OUT is a temp variable which will help you to hold all the values of i in second while loop
OUT will be set to "" every time in first while loop
Fine !!!
But how this thing helped in getting i values horizontally?
And can you explain the syntax
out=$out$i
out=$out$i ==> adds existing value of out if any and value of i together and store it in out itself.
In this part,
out=""
while [ $j -lt $i ]
do
out=$out$i
j=`expr $j + 1`
done
echo "$out"
- out is first set to nothing.
- enters while loop with i value as 5 [example]
- j is 0 in the beginning and on entering out=""|5 ==> used | for separating values of OUT and i
- i=5 , j = 1, out=5|5
i=5, j=2, out=55|5
i=5,j=3,out=555|5
i=5,j=4,out=5555|5
i=5,j=5 => while loops exits - print the value of out as 55555
Is this enough??
Sorry to say but I am not a shell expert so still in doubt about the usage of "OUT" variable
Rgds,
TS
---------- Post updated at 05:17 AM ---------- Previous update was at 04:58 AM ----------
Got it "OUT" acts as a buffer and it accumulates the values horizontally and finally prints it !!
Thanks & Rgds
TS
I forgot to mention explicitly, that you have to insert an empty print
statement after the first done
to end a line.
#!/bin/ksh
i=1
while [ $i -le 5 ]
do
j=0
while [ $j -lt $i ]
do
print -n $i
(( j=j+1 ))
done
print
(( i=i+1 ))
done
Output:
1
22
333
4444
55555