OS/2, eCS & ArcaOS - Technical > Programming

REXX tutorials -- start developing applets under ArcaOS out-of-the-box

<< < (3/6) > >>

Alfredo Fernández Díaz:
Jan-Erik, Gordon, thank you both for your input.

I think code snippets for tutorials should be as simple and straightforward as possible so as to show one way to address the task at hand and get it done right away.

As you point out, however, anything worth doing can probably be done in a number of ways, and different developers can find places in any listing where adding / replacing some code can improve the whole thing, so I'll ammend the above by adding "initially". Just like classes on the same subject are always the same yet always a little bit different if only because of the audience, different people will take away slightly different things from one tutorial so it is probably a good idea to have different code listings to illustrate possible alternatives or whatever. That's the spirit!

Putting everything together, maybe this could be better done in the form of a simple initial listing, plus additions to expand on obvious or non-obvious places. Let's try that with another REXX snippet...

Alfredo Fernández Díaz:
Code snippet #2: timed command

Rationale: one good thing about REXX is that it lets you use system commands in your code, which makes integrating both very easy. It is sometimes interesting to know how long some commands take to complete, so why not use some simple REXX to execute them and find out?

This REXX code will take its execution parameters as a command to be executed as is by the system shell, will report the time elapsed, and pass along its exit (return) code:


--- Code: ---/*
  Timed Command: executes its invoking parameters as an
  external system command, and tells how long it took
*/
parse arg command

/* Depending on command output, this may go off-screen > repeat at the end */
start = time()
say 'Starting "'||command||'" at '||start
call time 'E'  /* start timer */

/* Single literals execute as commands */
'@Echo Off'   
command       
cmdrc = rc     /* save return code */

say 'Command: "'||command||'", '||,
    'started at '||start||', ended at '||time()||'. '||,
    'Elapsed: '||format(time('E'),,2)||'s.'
if cmdrc <> 0 then /* simple warning */
  say 'Non-zero exit code (error?): '||cmdrc

exit cmdrc /* RC is passed along regardless of value */
--- End code ---

Some refinements are possible. For example, time can be formatted if too many seconds are uncomfortable to read:


--- Code: ---[...]
say 'Command: "'||command||'", '||,
    'started at '||start||', ended at '||time()||'. '||,
    'Elapsed: '||t2hms(time('E'))||'.'
if cmdrc <> 0 then /* simple warning */
  say 'Non-zero exit code (error?): '||cmdrc

exit cmdrc /* RC is passed along regardless of value */

t2hms: procedure
  parse arg t
  h = t % 3600
  t = (t // 3600)
  m = t % 60
  s = (t // 60)
  t = format(s,,2)||'s'
  if (m > 0) then do
    if (s < 10) then
      t = '0'||t
    t = m||'m:'||t
   end
  if (h > 0) then do
    if (m < 10) then
      t = '0'||t
    t = h||'h:'||t
   end
return t
--- End code ---

What if commands include redirection?

Some commands are most useful when executed redirecting their output to a file, e.g. "dir > filelist.txt", but executing "TimedCmd dir > filelist.txt" will include TimedCmd output in filelist.txt in the example, which may not be desirable.

To adress such situations, the whole command must be in quotes so it is passed as a single parameter for execution leaving the redirection to be processed later, i.e. TimedCmd "dir > filelist.txt" in the example above. However, the quotes must be removed from both ends so the system shell will break the command again to carry on the redirection, instead of looking for a file called "dir > filelist.txt" to execute:


--- Code: ---[...]
parse arg command
command = strip(command,,'"')
[...]
--- End code ---

Edit: tt -> code

Dave Yeo:
These would be more readable by using the code markup, square bracket code square bracket, same to close except /code.

--- Code: ---/*
  Timed Command: executes its invoking parameters as an
  external system command, and tells how long it took
*/
parse arg command

/* Depending on command output, this may go off-screen > repeat at the end */
start = time()
say 'Starting "'||command||'" at '||start
call time 'E'  /* start timer */

/* Single literals execute as commands */
'@Echo Off'   
command       
cmdrc = rc     /* save return code */

say 'Command: "'||command||'", '||,
    'started at '||start||', ended at '||time()||'. '||,
    'Elapsed: '||format(time('E'),,2)||'s.'
if cmdrc <> 0 then /* simple warning */
  say 'Non-zero exit code (error?): '||cmdrc

exit cmdrc /* RC is passed along regardless of value */

--- End code ---

Thanks for the code.

Alfredo Fernández Díaz:
Wait, there's a proper use for the code tag in forums?! LOL
Thanks for the tip, Dave! : ))

Dave Yeo:

--- Quote from: Alfredo Fernández Díaz on February 26, 2025, 08:10:15 pm ---Code snippet #2: timed command

Rationale: one good thing about REXX is that it lets you use system commands in your code, which makes integrating both very easy. It is sometimes interesting to know how long some commands take to complete, so why not use some simple REXX to execute them and find out?

This REXX code will take its execution parameters as a command to be executed as is by the system shell, will report the time elapsed, and pass along its exit (return) code:


--- Code: ---/*
  Timed Command: executes its invoking parameters as an
  external system command, and tells how long it took
*/
parse arg command

/* Depending on command output, this may go off-screen > repeat at the end */
start = time()
say 'Starting "'||command||'" at '||start
call time 'E'  /* start timer */

/* Single literals execute as commands */
'@Echo Off'   
command       
cmdrc = rc     /* save return code */

say 'Command: "'||command||'", '||,
    'started at '||start||', ended at '||time()||'. '||,
    'Elapsed: '||format(time('E'),,2)||'s.'
if cmdrc <> 0 then /* simple warning */
  say 'Non-zero exit code (error?): '||cmdrc

exit cmdrc /* RC is passed along regardless of value */
--- End code ---

--- End quote ---

Thanks for the better formatting. I tried running the above snippet after saving it as time.cmd and got,

--- Code: ---K:\work\dooble\obj>time.cmd make clean
SYS1044: The system cannot accept the time entered.

Enter the new time:

--- End code ---

Same with quoting the make clean.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version