doubts on code executing...

June 22, 2011 at 03:47:43
Specs: Windows XP
a=0
while [ "$a" -lt 10 ] # this is loop1
do
b="$a"
while [ "$b" -gt 0 ] # this is loop2
do
printf "$b "
b=`expr $b - 1` ------>how to work this code....or use of this code...
done
echo
a=`expr $a + 1`
done

See More: doubts on code executing...

Report •

#1
June 22, 2011 at 10:00:52
I am not certain I understand your question. This command:

b=`expr $b - 1`

simply subtracts 1 from b. Your script works for me although since a and b are integers, you don't have to surround the variables with double quotes:

#!/bin/ksh

a=0
while [ $a -lt 10 ] # this is loop1
do
  b=$a
  while [ $b -gt 0 ] # this is loop2
  do
    printf "$b "
    b=`expr $b - 1`
  done
  echo
  a=`expr $a + 1`
done
# end script

In closing, the expr method of performing arithmetic is required for the bourne shell. Shells like Korn or Bash support integer arithmetic, so this works:

((a=a + 1))

or even this:

((a+=1))


Let me know if you have any other questions.


Report •
Related Solutions


Ask Question