0

Both following examples work in modern Bash but don't work in different sh shells (like QNX shell).

$ for i in {1..3}; do echo $i; done
1
2
3

$ for i in $(seq 1 3); do echo $i; done
1
2
3

Is there any alternative method to produce same sequence in QNX shell?

psihodelia
  • 29,566
  • 35
  • 108
  • 157

5 Answers5

1

I found a method which works in QNX shell:

integer i=0
while ((i<4)); do i=i+1; echo $i; done
psihodelia
  • 29,566
  • 35
  • 108
  • 157
1

Here is a simple workaround to simulate the expr hack.

yes |
head -n 3 |
nl |
while read i yes; do
  ....
done

If you don't care about the value of i you can take out the nl line numbering.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

Use seq or jot. You may need to use backticks instead of $().

for i in `seq 1 3`

If the shell in the version of QNX you're using is ksh, then you should be able to use C-style for loops:

for ((i = 1; i <=3; i++ ))

Edit:

I'm now guessing that you have QNX 4 which has a ksh86 clone as its shell. In my opinion, under these circumstances, it is brain dead to not include seq or jot. However, all that aside, here is a hack that should be able to do sequences:

end=3
for n in $(echo "for (i = 1; i <= $end; i++) i" | bc)
do
    echo "$n"
done
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
0

Any posix compliant shell will allow you to iterate:

i=1
while expr $i \< 4 > /dev/null; do
  echo $i
  : $(( i + 1 ))
done

If you have an older shell, you may need to replace the last line inside the loop with i=$( expr $i + 1 ), and in some rare instances you may need to use back ticks.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • Thank you, looks like the right direction. But my shell doesn't have 'expr' tool. Is there any other method to substitute expr'? – psihodelia Jun 27 '12 at 08:14
0

You can get bash for QNX from the NetBSD pkgsrc repository: ftp://ftp.netbsd.org/pub/pkgsrc/packages/QNX/i386/6.5.0_head_20110826/shells/

Whome
  • 507
  • 1
  • 8
  • 24