Wiki source for CommandRedirect
======IO Redirection======
IO can be redirected as per the unix shells (sh) with "> file" , """>>""file" "< file" , "2> file". Redirection to variables and named ports, "Here Documents" and both text and XML documents are supported.
=====Input Redirection=====
Using the syntax
command < file
Input for command is taken from the file "file" (either byte stream or Xml document).
=====Output Redirection=====
Using the syntax
command > file
Output for command is passed to file "file" (either byte stream or Xml document).
Using the syntax
command "">>"" file
Appends the output of command to file, creating file if it does not already exist.
=====Stderr Redirection=====
Using the syntax
command 2> file
Stderr Output for command is passed to file "file" (either byte stream or Xml document).
Using the syntax
command 2"">>"" file
Stderr Output for command is appended to file "file"
=====Here Documents=====
As per the unix shells, "Here Documents" are supported.
A "here Document" is data embedded directly in the script or from the terminal which becomes the standard input of a command.
The syntax is:
%%
command <<EOF
Some Text
Here until the magic "EOF" is found
on a line by its own
EOF
%%
The tag "EOF" can be any string. Here documents work as well for text as xml documents.
For example:
%%
xread doc <<EOF
<foo>
bar
</foo>
EOF
%%
====URL Input====
URL's can be used for input redirection any place a file is used.
For example
%%
$ xcat < http://test.xmlsh.org/data/books.xml
%%
Also any Internal or Builtin commands that take filenames will take URL's for input files.
For example the above can be rewritten without using redirection
%%
$ xcat http://test.xmlsh.org/data/books.xml
%%
In both cases, the Base URI of the resulting document is preserved.
Note that URL redirection can only be used for input, not for output at this time.
====Variable Redirection====
A variable can be used for input or output redirection. The syntax is **{variable}** wherever a file would be used.
Example the following two commands are equivalent and send the result of xls into the variable "doc".
%%
$ xls >{doc}
$ xls | xread doc
%%
And so are the following
%%
$ xcat <{doc}
$ echo $doc | xcat
%%
Variables can be //Appended//. This will turn a non-sequenced variable into a sequenced variable.
The one exception is that if the variable is initially empty, or contains a single atomic value, AND the output of the command is text then the appending works like appending to a file, that is the strings are concatenated.
Example
%%
$ xls >{doc}
$ xls >>{doc}
%%
Now $doc is a sequence of 2 documents. The following would produce equivilent results
%%
$ xls | xread doc
$ doc=<[$doc,$doc]>
%%
Text concatenation works when text mode commands are used.
%%
$ echo foo >{port}
$ echo bar >>{port}
$ echo $port
foobar
%%
====Port Redirection====
Input and output can be explicitly bound to a [[NamedPorts Named Port]]. Named ports are specified by preceding the redirection symbol with (port). There are 3 predefined named ports, "input" , "output" , "error"
For example to bind the standard input port to "file.xml" and the port named "alternate" to "alternate.xml"
%%
$ command (alternate)<alternate.xml <file.xml
%%
The syntax 2< is synonymous with (error)< and 2> is synonymous with (error)>
For example, the following 2 commands are equivalent.
%%
$ command 2>error.txt
$ command (error)>error.txt
%%
----
See Also [[BasicSyntax]]
IO can be redirected as per the unix shells (sh) with "> file" , """>>""file" "< file" , "2> file". Redirection to variables and named ports, "Here Documents" and both text and XML documents are supported.
=====Input Redirection=====
Using the syntax
command < file
Input for command is taken from the file "file" (either byte stream or Xml document).
=====Output Redirection=====
Using the syntax
command > file
Output for command is passed to file "file" (either byte stream or Xml document).
Using the syntax
command "">>"" file
Appends the output of command to file, creating file if it does not already exist.
=====Stderr Redirection=====
Using the syntax
command 2> file
Stderr Output for command is passed to file "file" (either byte stream or Xml document).
Using the syntax
command 2"">>"" file
Stderr Output for command is appended to file "file"
=====Here Documents=====
As per the unix shells, "Here Documents" are supported.
A "here Document" is data embedded directly in the script or from the terminal which becomes the standard input of a command.
The syntax is:
%%
command <<EOF
Some Text
Here until the magic "EOF" is found
on a line by its own
EOF
%%
The tag "EOF" can be any string. Here documents work as well for text as xml documents.
For example:
%%
xread doc <<EOF
<foo>
bar
</foo>
EOF
%%
====URL Input====
URL's can be used for input redirection any place a file is used.
For example
%%
$ xcat < http://test.xmlsh.org/data/books.xml
%%
Also any Internal or Builtin commands that take filenames will take URL's for input files.
For example the above can be rewritten without using redirection
%%
$ xcat http://test.xmlsh.org/data/books.xml
%%
In both cases, the Base URI of the resulting document is preserved.
Note that URL redirection can only be used for input, not for output at this time.
====Variable Redirection====
A variable can be used for input or output redirection. The syntax is **{variable}** wherever a file would be used.
Example the following two commands are equivalent and send the result of xls into the variable "doc".
%%
$ xls >{doc}
$ xls | xread doc
%%
And so are the following
%%
$ xcat <{doc}
$ echo $doc | xcat
%%
Variables can be //Appended//. This will turn a non-sequenced variable into a sequenced variable.
The one exception is that if the variable is initially empty, or contains a single atomic value, AND the output of the command is text then the appending works like appending to a file, that is the strings are concatenated.
Example
%%
$ xls >{doc}
$ xls >>{doc}
%%
Now $doc is a sequence of 2 documents. The following would produce equivilent results
%%
$ xls | xread doc
$ doc=<[$doc,$doc]>
%%
Text concatenation works when text mode commands are used.
%%
$ echo foo >{port}
$ echo bar >>{port}
$ echo $port
foobar
%%
====Port Redirection====
Input and output can be explicitly bound to a [[NamedPorts Named Port]]. Named ports are specified by preceding the redirection symbol with (port). There are 3 predefined named ports, "input" , "output" , "error"
For example to bind the standard input port to "file.xml" and the port named "alternate" to "alternate.xml"
%%
$ command (alternate)<alternate.xml <file.xml
%%
The syntax 2< is synonymous with (error)< and 2> is synonymous with (error)>
For example, the following 2 commands are equivalent.
%%
$ command 2>error.txt
$ command (error)>error.txt
%%
----
See Also [[BasicSyntax]]