Commands
Rexx can run commands directly from strings, e.g.
Rexx can run commands referred to by variables, e.g.
Use the ADDRESS keyword statement explicitly, e.g.
One can assign the command to a Rexx variable and use that
-- get current operating system
parse source op_sys +1 -- "W"...Windows, everything else is regarded to be Unix
if op_sys="W" then command="dir /s" -- list directory and subdirectories
else command="ls -R" -- list directory and subdirectories
command -- execute command via the operating system (command line)
say "Rexx, RC="rc
Output (Windows), maybe:
Directory of e:\tmp
10.11.2020 13:38 <DIR> .
10.11.2020 13:38 <DIR> ..
10.11.2020 13:40 <DIR> .idea
10.11.2020 13:16 546 example1.rex
2 File(s) 1 287 bytes
Directory of e:\tmp\.idea
…cut…
Rexx, RC=0Excursus
0)": default input via the keyboard1)": default output to the screen (window)2): default output to the screen (window)PATH … determines the directories and the order for seeking programsJAVA_HOME … determines the Java directory to useCLASSPATH … determines the directories and the order for seeking Java classesExcursus
Redirect input from the keyboard to the file "myinput.txt"
Redirect output from the screen to the file "myoutput.txt" (will delete file if it exists)
Redirect error output from the screen to the file "myerrors.txt" (will delete file if it exists)
Excursus
Redirecting standard files when running a program in a process
rexx myprogram.rex 0<myinput.txt 1>myoutput.txt 2>myerrors.txt
rexx myprogram.rex <myinput.txt >myoutput.txt 2>myerrors.txtRedirect output to a file and also redirect error output to the same file
Redirecting and appending to stdout and to stderr
Excursus
Redirect stdout of one program to stdin of the next program
Piping operator: vertical bar ("|")
Example
One can redirect and pipe to/from multiple programs
Excursus
Unix null device, a file named
/dev/null
Windows null device, a pseudo file named
nul
Show errors only (discard stdout output)
rexx myprogram.rex >nul (Windows)
rexx myprogram.rex >/dev/null (Unix)
Show output only (discard stderr output, i.e. do not show error messages)
rexx myprogram.rex 2>nul (Windows)
rexx myprogram.rex 2>/dev/null (Unix)
Excursus
/* myprogram.rex: write input lines (stdin) to output (stdout) */
do until value="" -- if empty input, leave
parse pull value -- read line from stdin, assign to variable "value"
say value -- write to stdout
end
Command (Unix, Windows):
rexx myprogram.rex < myinput.txt
Max
und
Moritz
konnt
der
Lehrer ...Output:
Max
und
Moritz
konnt
der
Lehrer ...
Excursus
/* myprogram.rex: write input lines (stdin) to output (stdout) */
do until value="" -- if empty input, leave
parse pull value -- read line from stdin, assign to variable "value"
say value -- write to stdout
end
Command (Windows):
rexx myprogram.rex < myinput.txt | rexx myfilter.rex >nul
Command (Unix):
rexx myprogram.rex < myinput.txt | rexx myfilter.rex >/dev/null
Max
und
Moritz
konnt
der
Lehrer ...Output:
Max
und
konnt
Excursus
/* myprogram.rex: write input lines (stdin) to output (stdout) */
do until value="" -- if empty input, leave
parse pull value -- read line from stdin, assign to variable "value"
say value -- write to stdout
end
Command (Windows):
rexx myprogram.rex < myinput.txt | rexx myfilter.rex 2>nul
Command (Unix):
rexx myprogram.rex < myinput.txt | rexx myfilter.rex 2>/dev/null
Max
und
Moritz
konnt
der
Lehrer ...Output:
Moritz
der
Lehrer ...
Excursus
/* myprogram.rex: write input lines (stdin) to output (stdout) */
do until value="" -- if empty input, leave
parse pull value -- read line from stdin, assign to variable "value"
say value -- write to stdout
end
Command (Windows):
rexx myprogram.rex < myinput.txt | rexx myfilter.rex 2>nul | sort
Command (Unix):
rexx myprogram.rex < myinput.txt | rexx myfilter.rex 2>/dev/null | sort -f
Max
und
Moritz
konnt
der
Lehrer ...Output:
der
Lehrer ...
Moritz
Excursus
/* myprogram2.rex: write input lines (stdin) to output (stdout) */
signal on notready -- raised, if no input data anymore
loop -- loop eternally, same as: do forever
parse linein value -- get line from stdin, raises notready if no data anymore
say value -- write to stdout
end
notready: -- label to signal, if no input anymore
/* myfilter2.rex: if value contains 'r' then write it to stdout, else to stderr */
signal on notready -- raised, if no input data anymore
do forever -- loop eternally, same as: loop
parse linein value -- get line from stdin, raises notready if no data anymore
if pos('r',value)>0 then .output~say(value) -- write value to stdout
else .error ~say(value) -- write value to stderr
end
notready: -- label to signal, if no input anymore
Command (Windows):
rexx myprogram2.rex < myinput.txt | rexx myfilter2.rex 2>nul | sort
Command (Unix):
rexx myprogram2.rex < myinput.txt | rexx myfilter2.rex 2>/dev/null | sort -f
Max
und
Moritz
konnt
der
Lehrer ...Output:
der
Lehrer ...
Moritz
Commands may include all desired redirections and pipes!
Possible to redirect the command's standard files to and from ooRexx!
Use the ADDRESS … WITH statement for redirections
| INPUT option: | command's stdin provided by an ooRexx collection object |
| OUTPUT option: | command's stdout gets appended to an ooRexx collection object |
| ERROR option: | command's stderr gets appended to an ooRexx collection object |
Example
Use two ooRexx arrays to receive stdout and stderr output from command
-- get current operating system
parse source op_sys +1 -- "W"...Windows, everything else is regarded to be Unix
if op_sys="W" then command="dir /s" -- list directory and subdirectories
else command="ls -R" -- list directory and subdirectories
say "Rexx, command:" quote(command)
say "Rexx, current directory:" quote(directory())
-- redirect standard output to the 'arrOut' array
arrOut=.array~new -- create Rexx array for fetching standard output
-- execute command, redirect output to Rexx array
ADDRESS SYSTEM command WITH OUTPUT USING (arrOut)
say "Rexx, RC="rc
say "Rexx, arrOut~items:" arrOut~items "(lines)"
::routine quote -- enquote argument in double quotes
return '"' || arg(1) || '"'
Output (Windows), maybe:
Rexx, command: "dir /s"
Rexx, current directory: "e:\tmp"
Rexx, RC=0
Rexx, arrOut~items: 26 (lines)
Command, 7
Command, 8
Command, 9
Output:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 15583 100 15583 0 0 423k 0 --:--:-- --:--:-- --:--:-- 434k
Current URL of BACH-WU Directory: http://bach.wu.ac.at/d/directory/
from curl's stderr
Command, 10
command="curl https://bach.wu.ac.at/z/start"
outArr=.array~new -- array for stdout
errArr=.array~new -- array for stderr: curl's information goes here
ADDRESS SYSTEM command WITH OUTPUT USING (outArr) ERROR USING (errArr)
source=outArr~makeString -- turn array into string
parse var source '<h2>WU</h2>' . '<a href="' url '">WU Directory<'
say "Current URL of BACH-WU Directory:" url
Output:
Current URL of BACH-WU Directory: http://bach.wu.ac.at/d/directory/
Command, 11
Output:
Vienna: +15°C →33km/h
%l:
+%t
+%w
Command, 12
url = "https://api-free.deepl.com/v2/translate"
key = "yourAuthorizationKey" -- you need to obtain a valid key from deepl.com!
original_text = "ooRexx die Sprache der unbegrenzten Möglichkeiten."
translate_to = "EN"
command="curl" url "-d auth_key="key "-d text="quote(original_text) "-d target_lang="translate_to
outArr=.array~new -- array for stdout
ADDRESS SYSTEM command WITH OUTPUT USING (outArr) ERROR USING (.array~new)
parse var outArr . ':"' translated_from '","text":"' translated_text '"' -- parse turns array into string
say "Translated by Deepl.com from" translated_from "to" translate_to
say "Translated text:" translated_text
::routine quote -- encloses text in quotes, such that it may contain blanks
return '"' || arg(1) || '"'
Output (if authorization key is correct):
Translated by Deepl.com from DE to EN
Translated text: ooRexx the language of unlimited possibilities.
WU
Prof. Mag. Dr. Rony G. Flatscher
Welthandelsplatz 1
1020 Wien/Vienna
Austria/Europe
www: www.wu.ac.at