Recent

Author Topic: Can't decrement counter with fpsystem while loop  (Read 752 times)

hakelm

  • Full Member
  • ***
  • Posts: 153
Can't decrement counter with fpsystem while loop
« on: January 28, 2023, 10:23:59 am »
In the little test program below I am trying to run a while loop controlled by a decrementing counter.
The result is, however, not what I had expected, the counter i is not decremented, I get an endless loop.
What is wrong?
H
I am using lazarus-ide-2.2.0 on ubuntu

Code: Pascal  [Select][+][-]
  1. [
  2. program project1;
  3. {$mode objfpc}{$H+}
  4. uses
  5.   {$IFDEF UNIX}
  6.   cthreads,
  7.   {$ENDIF}
  8.   Classes,unix
  9.   { you can add units after this };
  10. //{$R *.res}
  11. var cmd:string;
  12. begin
  13.     //cmd:='i=100; while [ 1 -le "$i" ]; do amixer -c 1 set Master playback "$i"%;   ((i--)); done|grep dB > r';
  14.     cmd:='i=100; while [ 1 -le "$i" ]; do echo $i;   ((i--)); done';
  15.     writeln(cmd);
  16.     fpsystem(cmd);
  17. end.
  18. { output:
  19. i=100; while [ 1 -le "$i" ]; do echo $i;   ((i--)); done
  20. 100
  21. /bin/sh: 1: i--: not found
  22. 100
  23. /bin/sh: 1: i--: not found
  24. 100
  25. /bin/sh: 1: i--: not found
  26. 100
  27. /bin/sh: 1: i--: not found
  28. 100
  29. ......
  30. }/code]

TRon

  • Hero Member
  • *****
  • Posts: 2514
Re: Can't decrement counter with fpsystem while loop
« Reply #1 on: January 28, 2023, 10:41:00 am »
The result is, however, not what I had expected, the counter i is not decremented, I get an endless loop.
What is wrong?
In principle there seems nothing wrong. But i suspect that the decrement of the counter is an internal bash-ishm and therefor fails (you should invoke bash then instead). I get "i-- not found" as if it where a command.

Having said that, this seems to work for me:
Code: [Select]
    cmd:='i=100; while [ 1 -le "$i" ]; do echo $i; i=$((i-1)) ; done';

hakelm

  • Full Member
  • ***
  • Posts: 153
Re: Can't decrement counter with fpsystem while loop
« Reply #2 on: January 28, 2023, 11:38:49 am »
Thanks. i-1 did the trick.
But it is a bit annoying not understanding what is going on.
H

TRon

  • Hero Member
  • *****
  • Posts: 2514
Re: Can't decrement counter with fpsystem while loop
« Reply #3 on: January 28, 2023, 11:44:37 am »
But it is a bit annoying not understanding what is going on.
Some expressions/commands are only available inside bash itself (so called internals). Because you did not invoke bash itself in the fpsystem call these internals are not available/accessible in your script.
see also https://bash.cyberciti.biz/guide/Built-in_commands (click below for the different available shells). I am sure there are websites that have a more comprehensive lists and other gory details available :-)

Other than that, it is always a good idea to use posix compliant scripts.

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Can't decrement counter with fpsystem while loop
« Reply #4 on: January 28, 2023, 04:35:31 pm »
To make things more complicated, fpSystem calls the shell that is registered as your systems /bin/sh, which *can* be a symlink to bash, but also to any other shell such as the Bourne Shell (sh), Z-Shell (zsh), Fish, etc.
So you cannot assume any shell specific functionality when calling system. If you need such functionality, you should be calling the respective shell directly. In this case you also do not need fpsystem but can also just rely on TProcess or RunCommand

TRon

  • Hero Member
  • *****
  • Posts: 2514
Re: Can't decrement counter with fpsystem while loop
« Reply #5 on: January 29, 2023, 12:27:30 pm »
Thank you Warfley for that information as I was not aware of that.

hakelm

  • Full Member
  • ***
  • Posts: 153
Re: Can't decrement counter with fpsystem while loop
« Reply #6 on: January 29, 2023, 12:33:44 pm »
Interesting.
On my system sh points neither to sh nor to bash but to dash and man dash says:

     dash is the standard command interpreter for the system.  The current
     version of dash is in the process of being changed to conform with the
     POSIX 1003.2 and 1003.2a specifications for the shell.  This version has
     many features which make it appear similar in some respects to the Korn
     shell, but it is not a Korn shell clone (see ksh(1)).  Only features des‐
     ignated by POSIX, plus a few Berkeley extensions, are being incorporated
     into this shell.  This man page is not intended to be a tutorial or a
     complete specification of the shell.

 

TinyPortal © 2005-2018