> For the complete documentation index, see [llms.txt](https://docs.editran.onesait.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.editran.onesait.com/documentacion-editran/documentacion-editran-editran-sepa-v3-open-en/modo-de-uso/integracion/unix.md).

# UNIX

The Before Issuance script that causes Editran/SEPA to run in an issuance submission is called `PRVEMISEPA.sh`.

To be able to use that script, you must define `./SEPA/PRVEMISEPA.sh` in the "Before issuance" section of the "Application" option of the Editran/G submission that will send SEPA files.

> **IMPORTANT:** It is necessary to define in the issuance directory with the metacharacter `*` at the end of the path, and also relative paths cannot be used (`../`, `./`, etc.)

For example, if the files are in the directory `/opt/ficheros/SEPA/`in the Files section of Editran/G you must define: `/opt/ficheros/SEPA/*`.

A common name can also be defined for the files to be issued and then the asterisk can be defined at the end (`/opt/ficheros/SEPA/file*`)

The script has the following steps:

* It records in the Editran/G output file (`editrang.out`) the general information about the transmission result:

```sh
# -----------------------------------------------------------
#                         Functions
# -----------------------------------------------------------
function escribeCabecera()
{   echo "################################################################################"
    echo PRE-ISSUANCE USER SCRIPT: $0.
    echo "`date`"
}

function escribeParametros()
{   echo "SUBMISSION : $SENTIDO-$SESION ($LOCAL-$REMOTO-$APLICACION)"
    echo "RETURN      : $RETORNO"
    echo "################################################################################"
}

function borrarFichTrabajo()
{
    [ -f "$ARCHIVOS_ORIG" ] && rm "$ARCHIVOS_ORIG"
    [ -f "$FLISTA" ] && rm "$FLISTA"
}

function borrarFichConvertidos()
{ [ ! -f "${ARCHIVOS_ORIG}" ] && return

    while read fichConv
    do
        #Delete converted files
        case "${fichConv}" in
            *.xml|*.XML)
                fsinExtension=${fichConv%????}
                if [ -f "${fsinExtension}" ]; then
                     rm "${fsinExtension}"
                fi;;
            *)
                if [ -f "${fichConv}".xml ]; then
                     rm "${fichConv}".xml
                fi;;
        esac
    done < "${ARCHIVOS_ORIG}"
}

function moverFichOriginales()
{ [ ! -f "${ARCHIVOS_ORIG}" ] && return

    while read fichOrig
    do
        directory=`dirname "${fichOrig}"`/"${DIR_ORIG}"
        if [ ! -d "${directory}" ]; then
            mkdir -p "${directory}"
        fi
        if [ -f "${fichOrig}" ]; then
            mv "${fichOrig}" "${directory}"
        fi
    done < "${ARCHIVOS_ORIG}"
}

function mensajeResultado()
{
    if [ ! -f "${ARCHIVOS_ORIG}" ]; then
        echo "There are no files to convert"
        exit 0
    fi

     case `head -1 "${ARCHIVOS_ORIG}"` in
         *.xml|*.XML)
             echo "SEPA XML to flat conversion successful."
             ;;
         *)
             echo "SEPA flat to XML conversion successful."
             ;;
     esac
}

# -----------------------------------------------------------
#                         MAIN
# -----------------------------------------------------------
SALIDA=0
CLASSPATH=
DIR_ORIG=orig

# Writes the script header ############################
escribeCabecera

# Validate the environment variables
validaEntorno && echo "Error validaEntorno" && exit 1

# Transform the input parameters into Editran parameters
getParametrosEditran "PRV" "$@"
[ $? -ne 0 ] && echo "Error getParametrosEditran" "PRV" "$@" && exit 1

# Write the Editran parameters ############################
escribeParametros
```

Where SESION is the submission name, SENTIDO is the direction of transmission (E (Issuance) or R (Reception)), RETORNO is the result of the transmission, LOCAL is the local code, REMOTO the remote code and APLICACIÓN the application of the submission.

The necessary variables are defined, it is checked that the issuance directory defined in Editran/G has the metacharacter `*` mandatory and Editran/SEPA is executed for each file found in the issuance directory defined in the Editran/G Files section.

If the original file has the extension `.xml` Editran/SEPA will convert it to a flat file; if it does not have an extension `.xml` it will convert it to an XML-formatted file, since by default it uses the command's one-parameter usage mode, as explained in 4.2 Client process.

If none of the conversions performed on the files to be issued ends with an error result, the original files will be moved to a historical subdirectory called `orig`.

If, on the contrary, any of those conversions ends with an error, the successfully converted files are deleted, if any, and the submission sending is stopped, leaving the original output files as they were for later review.

If the issuance directory defined in Editran/G does not have the metacharacter `*` mandatory the process stops. If there were any converted file, it would be deleted:

```sh
# If RETURN is not 0, exit ##################################
if [ "${RETORNO}" != "0000" ]; then
  exit 0
fi

# Working files #######################################
ARCHIVOS_ORIG=$LOCAL-$REMOTO-$APLICACION-$SENTIDO.orig
FLISTA=$LOCAL-$REMOTO-$APLICACION-$SENTIDO.list
borrarFichTrabajo

# SEPA libraries ############################################
for f in ${DIR_LIB_SEPA}/*.jar; do
  CLASSPATH=${f}:${CLASSPATH}
done

for f in ${DIR_LIB_SEPA_SO}/*.jar; do
  CLASSPATH=${f}:${CLASSPATH}
done

# We obtain the files to convert ########################
# The path of the files to be sent is extracted, previously defined in the presentation of EDItran/G
cfgupt -r -i$SESION | grep "NOMBRE FICHERO" | cut -d"=" -f2 | sed 's/^ *//' > $FLISTA

while read fich
do
    case "${fich}" in
        */"*")
            fbuscar="${fich%??}"
            for fichconvert in "${fbuscar}"/*
            do
                if [ -f "${fichconvert}" ] ; then
                    echo "${fichconvert}" >> $ARCHIVOS_ORIG

                    diraux=${PWD}
                    cd "${DIR_SEPA}"
                    java -Dlog4j.configurationFile="conf/log4j2.properties" -cp ${CLASSPATH} \
$EDITRAN_XMLMAIN "${fichconvert}" 2>&1 > ${LOGOUT}
                    error=$?
                    cd "${diraux}"

                             if [ ${error} -ne 0 ]; then
                                 if [ ${error} -ne 36 ]; then
                                     echo "Error converting the file ${fichconvert}"
                                     echo "Error=${error}"
                                     echo "Incorrect SEPA conversion"

                                      SALIDA=1
                                      break
                                  else
                                    echo "SEPA conversion of file ${fichconvert} with warnings"
                                  fi
                             fi
                    fi
                done;;
           *)
                echo "You must define a * wildcard in the path of the EDITRAN/G sending files."

                SALIDA=1
                break
    esac
done < "$FLISTA"

# Conversion with errors ####################################
if [ $SALIDA -ne 0 ]; then
    echo "Error output"
    borrarFichConvertidos
    borrarFichTrabajo
    exit $SALIDA
fi

# Successful conversion #######################################
mensajeResultado
moverFichOriginales
borrarFichTrabajo

# Finish ##################################################
exit $SALIDA
```

The After Issuance script that moves the converted and successfully sent files is called `PSTEMISEPA.sh`.

To be able to use that script, you must define `./SEPA/PSTEMISEPA.sh` in the "After issuance" section of the "Application" option of the Editran/G submission that will send SEPA files.

The script has the following steps:

* It records in the Editran/G output file (`editrang.out`) the general information about the transmission result:

```sh
# -----------------------------------------------------------
#                         Functions
# -----------------------------------------------------------
function escribeCabecera()
{   echo "################################################################################"
    echo USER SCRIPT AFTER SENDING: $0.
    echo "`date`"
}

function escribeParametros()
{   echo "SUBMISSION : $SENTIDO-$SESION ($LOCAL-$REMOTO-$APLICACION)"
    echo "FILES     : $FLIST"
    echo "RETURN      : $RETORNO"
    echo "################################################################################"
}

# -----------------------------------------------------------
#                         MAIN
# -----------------------------------------------------------
# Writes the script header ############################
escribeCabecera

# Validate environment variables ###########################
validaEntorno && echo "Error validaEntorno" && exit 1

# Transform the input parameters into Editran parameters
getParametrosEditran "PST" "$@"
[ $? -ne 0 ] && echo "Error getParametrosEditran" "PST" "$@" && exit 1

# Write the Editran parameters ############################
escribeParametros
```

Where SESION is the submission name, SENTIDO is the direction of transmission (E (Issuance) or R (Reception)), RETORNO is the result of the transmission, LOCAL is the local code, REMOTO the remote code and APLICACIÓN the application of the submission.

* The list of files successfully sent is obtained from the file `.ficheros` from the Editran buffer directory and moved to a directory called "conv".

```sh
# If RETURN is not 0, exit ##################################
if [ "${RETORNO}" != "0000" ]; then
  exit 0
fi

# Working variables ######################################
DIR_CONV=conv
FICH_ANTERIOR=""

# Move converted files ############################
awk -F',' '{if ($1 != "") print $1}' "${FLIST}" | while read fileconv
do
    if [ -f "${fileconv}" ] && [ "${fileconv}" != "${FICH_ANTERIOR}" ]; then
        FICH_ANTERIOR="${fileconv}"

           directory=`dirname "${fileconv}"`/"${DIR_CONV}"

           if [ ! -d "${directory}" ]; then
               mkdir -p "${directory}"
           fi

           mv "${fileconv}" "${directory}"
    fi
done
```

The After Reception script that causes Editran/SEPA to run in a reception submission is called `PSTRECSEPA.sh`.

To be able to use that script, you must define `./SEPA/PSTRECSEPA.sh` in the "After reception" section of the "Application" option of the Editran/G submission that will send SEPA files.

> **IMPORTANT:** It is recommended to define the reception directory with the option `%o` to receive the files with the name under which the remote sends them.
>
> If, when receiving files, the desired conversion is from XML to Flat, it is necessary to define in the reception directory that the file to be downloaded contains extension `.xml`, since the PSTRECSEPA batch by default uses the command's one-parameter usage mode, as explained in [4.2 Client process](#42-proceso-cliente). With the option `%o` the files are received with the name under which the remote sends them, if it sends them with the extension `.xml` no configuration will be necessary; on the contrary, if the remote does not include the extension, the parameter would have to be defined `%o` with the extension `.xml` at the end.

Other variables can also be used, see the Editran/G manual.

For example, to receive the files in the directory `/opt/ficheros/recepción/`in the Files section of Editran/G you must define: `/opt/ficheros/recepción/%o`. If the conversion is from XML to Flat and the remote does not include the extension when issuing, the path `/opt/ficheros/recepción/%o.xml`.

The script has the following steps:

* It records in the Editran/G output file (`editrang.out`) the general information about the transmission result:

```sh
# -----------------------------------------------------------
#                         Functions
# -----------------------------------------------------------
function escribeCabecera()
{   echo "################################################################################"
    echo USER SCRIPT AFTER RECEIVING: $0.
    echo "`date`"
}

function escribeParametros()
{   echo "SUBMISSION : $SENTIDO-$SESION ($LOCAL-$REMOTO-$APLICACION)"
    echo "FILES     : $FLIST"
    echo "RETURN      : $RETORNO"
    echo "################################################################################"
}

function borrarFichTrabajo()
{
    [ -f "$ARCHIVOS_ORIG" ] && rm "$ARCHIVOS_ORIG"
}

function borrarFichConvertidos()
{ [ ! -f "${ARCHIVOS_ORIG}" ] && return

    while read fichConv
    do
        #Delete converted files
        case "${fichConv}" in
            *.xml|*.XML)
                fsinExtension=${fichConv%????}
                if [ -f "${fsinExtension}" ]; then
                     rm "${fsinExtension}"
                fi;;
                *)
                        if [ -f "${fichConv}".xml ]; then
                             rm "${fichConv}".xml
                        fi;;
        esac
    done < "${ARCHIVOS_ORIG}"
}

function moverFichOriginales()
{ [ ! -f "${ARCHIVOS_ORIG}" ] && return

    while read fichOrig
    do
        directory=`dirname "${fichOrig}"`/"${DIR_ORIG}"
        if [ ! -d "${directory}" ]; then
            mkdir -p "${directory}"
        fi
        if [ -f "${fichOrig}" ]; then
            mv "${fichOrig}" "${directory}"
        fi
    done < "${ARCHIVOS_ORIG}"
}

function mensajeResultado()
{
    if [ ! -f "${ARCHIVOS_ORIG}" ]; then
        echo "There are no files to convert"
        exit 0
    fi

      case `head -1 "${ARCHIVOS_ORIG}"` in
          *.xml|*.XML)
              echo "SEPA XML to flat conversion successful."
              ;;
          *)
              echo "SEPA flat to XML conversion successful."
              ;;
      esac
}

# -----------------------------------------------------------
#                         MAIN
# -----------------------------------------------------------
SALIDA=0
CLASSPATH=
DIR_ORIG=orig

# Writes the script header ############################
escribeCabecera

# Validate environment variables ###########################
validaEntorno && echo "Error validaEntorno" && exit 1

# Transform the input parameters into Editran parameters
getParametrosEditran "PST" "$@"
[ $? -ne 0 ] && echo "Error getParametrosEditran" "PST" "$@" && exit 1

# Write the Editran parameters ############################
escribeParametros
```

Where SESION is the submission name, SENTIDO is the direction of transmission (E (Issuance) or R (Reception)), RETORNO is the result of the transmission, LOCAL is the local code, REMOTO the remote code and APLICACIÓN the application of the submission.

The necessary variables are defined and Editran/SEPA is executed for each file received. If the original file has the extension `.xml` Editran/SEPA will convert it to a flat file; if it does not have an extension `.xml` it will convert it to an XML-formatted file.

If none of the conversions performed on the files to be received ends with an error, the original files will be moved to a historical subdirectory called `orig`.

If, on the contrary, any of those conversions ends with an error, the correctly converted files, if any, will be deleted, and the script will return the corresponding result, leaving the original reception files as they were for later review.

```sh
# If RETURN is not 0, exit ##################################
if [ "${RETORNO}" != "0000" ]; then
  exit 0
fi

# Working files #######################################
ARCHIVOS_ORIG=$LOCAL-$REMOTO-$APLICACION-$SENTIDO.orig
FICH_ANTERIOR=""
borrarFichTrabajo

# SEPA libraries ############################################
for f in ${DIR_LIB_SEPA}/*.jar; do
    CLASSPATH=${f}:${CLASSPATH}
done

for f in ${DIR_LIB_SEPA_SO}/*.jar; do
    CLASSPATH=${f}:${CLASSPATH}
done

# We obtain the files to convert ########################
awk -F',' '{if ($1 != "") print $1}' "${FLIST}" | while read fich
do
    if [ -f "${fich}" ] && [ "${fich}" != "${FICH_ANTERIOR}" ]; then
        FICH_ANTERIOR="${fich}"
        echo "${fich}" >> $ARCHIVOS_ORIG

        diraux=${PWD}
        cd "${DIR_SEPA}"
        java -Dlog4j.configurationFile="conf/log4j2.properties" -cp ${CLASSPATH} $EDITRAN_XMLMAIN \
"${fich}" 2>&1 > ${LOGOUT}
        error=$?
        cd "${diraux}"

           if [ ${error} -ne 0 ]; then
               if [ ${error} -ne 36 ]; then
                   echo "Failed to convert file ${fich}"
                   echo "Error=${error}"
                   echo "Incorrect SEPA conversion"

                    # Exits the read loop, not the script
                    exit 1
                else
                    echo "SEPA conversion of file ${fich} with warnings"
                fi
        fi
    fi
done
SALIDA=$?

# Conversion with errors ####################################
if [ $SALIDA -ne 0 ]; then
    echo "Error output"
    borrarFichConvertidos
    borrarFichTrabajo
    exit $SALIDA
fi

# Successful conversion #######################################
mensajeResultado
moverFichOriginales
borrarFichTrabajo

# Finish ##################################################
exit $SALIDA
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.editran.onesait.com/documentacion-editran/documentacion-editran-editran-sepa-v3-open-en/modo-de-uso/integracion/unix.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
