Well, first off, it helped me a lot to look at the code with indents in it:
Code:
#!/bin/ksh
count=1
val=$2
op=$1
ans=0
if [ $op = "-e" -o $op = "-o" ]
then
if [ $op = "-e" ]
then
while [ $count -le $val ]
do
ans=`expr $count % 2`
if [ $ans -eq 0 ]
then
echo "$count \c "
count=`expr $count + 1`
fi
done
elif [ $op = "-o" ]
then
while [ $count -le $val ]
do
ans=`expr $count % 2`
if [ $ans -ne 0 ]
then
echo "$count \c "
count=`expr $count + 1`
fi
done
fi
else
while [ $count -le $val ]
do
echo "$count \c "
count=`expr $count + 1`
done
fi
Now, the first thing I usually try when trying to figureout what my script is doing is to add a "set -x" line at the top of the script. That will echo out each step the shell is performing, so you can see what's going on...
So, The way I ran the script, (I'll give an example of what I think I saw trying to use the" -e 4 "option) it got stuck in a loop at:
+ ans=1
+ '[' 1 -eq 0 ']'
+ '[' 1 -le 4 ']'
++ expr 1 % 2
Ok, interesting... let's look at this. So, what the script is doing step by step:
Code:
if [ $op = "-e" ] # This is true in our case
then
while [ $count -le $val ] # while 1 is less than or equal to 4
do
ans=`expr $count % 2` # ans = 1 % 2, or ans = 1
if [ $ans -eq 0 ] # if ans (1) = 0, then continue... nope - can't continue
then
echo "$count \c "
count=`expr $count + 1`
fi # We're done, time to loop while waiting for ans to equal 0
Do you see what you problem is? Hint: $ans will always equal 1 in this case
Here's another (messier, but more info) way of doing it:
Code:
if [ "$op" = "-e" -o "$op" = "-o" ]
echo " if [ $op = -e -o $op = -o ]"
then
echo " then"
if [ "$op" = "-e" ]
echo " if [ $op = -e ] "
then
echo " then"
while [ "$count" -le "$val" ]
echo " while [ $count -le $val ] "
do
echo " do"
ans=`expr $count % 2`
echo " ans=`expr $count % 2` "
if [ "$ans" -eq "0" ]
echo " if [ $ans -eq 0 ] "
then
echo " then"
echo -e "$count \c "
echo " echo -e $count \c"
count=`expr $count + 1`
echo " count=`expr $count + 1`"
This way, you can watch your script in action... It almost looks as if it doesn't know when to exit, right?
You may need a little re-designing, but I think you're pretty much on the right track
(Ooh, and I appologize, I changed some of the "echo"'s to "echo -e"'s, since I was looking at this on a Linux system...
Does this help any?