XSH Examples

Functions definition

XSH source

<?xml version="1.0" encoding="UTF-8"?>
<xsh:functions xmlns:xsh="http://xsd.nore.fr/xsh">
  <xsh:function name="ns_realpath">
    <xsh:parameter name="path" />
    <xsh:body>
    <xsh:local name="cwd">$(pwd)</xsh:local>
    <![CDATA[
[ -d "${path}" ] && cd "${path}" && path="."
while [ -h "${path}" ] ; do path="$(readlink "${path}")"; done

if [ -d "${path}" ]
then
  path="$( cd -P "$( dirname "${path}" )" && pwd )"
else
  path="$( cd -P "$( dirname "${path}" )" && pwd )/$(basename "${path}")"
fi

cd "${cwd}" 1>/dev/null 2>&1
echo "${path}"
    ]]></xsh:body>
  </xsh:function>
</xsh:functions>

Bash output

xsltproc ${NS_XML}/ns/xsl/languages/xsh.xsl examples.xsh
ns_realpath()
{
  local path
  if [ $# -gt 0 ]
  then
    path="${1}"
    shift
  fi
  local cwd="$(pwd)"
  [ -d "${path}" ] && cd "${path}" && path="."
  while [ -h "${path}" ] ; do path="$(readlink "${path}")"; done
 
  if [ -d "${path}" ]
  then
    path="$( cd -P "$( dirname "${path}" )" && pwd )"
  else
    path="
$( cd -P "$( dirname "${path}" )" && pwd )/$(basename "${path}")"
  fi
 
  cd "
${cwd}" 1>/dev/null 2>&1
  echo "
${path}"
}

Ksh output

xsltproc --stringparam xsh.defaultInterpreterType ksh ${NS_XML}/ns/xsl/languages/xsh.xsl examples.xsh
function ns_realpath
{
  typeset var path
  if [ $# -gt 0 ]
  then
    path="${1}"
    shift
  fi
  typeset var cwd="$(pwd)"
  [ -d "${path}" ] && cd "${path}" && path="."
  while [ -h "${path}" ] ; do path="$(readlink "${path}")"; done
 
  if [ -d "${path}" ]
  then
    path="$( cd -P "$( dirname "${path}" )" && pwd )"
  else
    path="
$( cd -P "$( dirname "${path}" )" && pwd )/$(basename "${path}")"
  fi
 
  cd "
${cwd}" 1>/dev/null 2>&1
  echo "
${path}"
}

A simple program

<?xml version="1.0" encoding="UTF-8"?>
<!-- Interpreter type can be forced through the @interpreterType attribute -->
<xsh:program interpreterType="bash" xmlns:xsh="http://xsd.nore.fr/xsh">
  <xsh:functions>
    <!-- This will include ns_realpath function definition -->
    <xi:include href="relativepath/to/filesystem.xsh"
      xpointer="xmlns(xsh=http://xsd.nore.fr/xsh) xpointer(//xsh:function[@name = 'ns_realpath'])" />
  </xsh:functions>
  <!-- The program body -->
  <xsh:code><![CDATA[
scriptFilePath="$(ns_realpath "${0}")"
echo "Hello from ${scriptFilePath}"
  ]]></xsh:code>
</xsh:program>

Adding program interface definition

Here is the content of the XSH file for the <<file apps/build-shellscript>> tool

<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright © 2011-2012 by Renaud Guillard (dev@nore.fr) -->
<!-- Distributed under the terms of the MIT License, see LICENSE -->
<xsh:program interpreterType="bash"
    xmlns:prg="http://xsd.nore.fr/program"
    xmlns:xsh="http://xsd.nore.fr/xsh"
    xmlns:xi="http://www.w3.org/2001/XInclude">
  <xsh:info>
    <!-- The program interface XML definition file -->
    <xi:include href="build-shellscript.xml" />
  </xsh:info>
  <xsh:functions>
    <xi:include href="../lib/filesystem/filesystem.xsh" xpointer="xmlns(xsh=http://xsd.nore.fr/xsh) xpointer(//xsh:function[@name = 'ns_realpath'])" />
    <xi:include href="functions.xsh" xpointer="xmlns(xsh=http://xsd.nore.fr/xsh) xpointer(//xsh:function)" />
  </xsh:functions>
  <xsh:code>
    <!-- We can use XInclude here too -->
    <xi:include href="build-shellscript.body.init.sh" parse="text"/>
    <xi:include href="build-shellscript.body.process.sh" parse="text"/>
  </xsh:code>
</xsh:program>

Note that the build-shellscript.body.process.sh code is also included into a function defined in the build-xulapp utility


XML shell file overview