Sign Up

Parameterizing STDIN/STDOUT

The platform currently needs explicit command line parameters for input and output

tool -i input.txt -o output.txt

rather than STDIN/STDOUT

cat input.txt | tool > output.txt

Suppose your tool expects input/output in the latter format and does not have an explicit parameter for input/output.

In that case, you can write a shell one-liner to emulate the existence of a parameter in your Docker image, like this one:

cat "${@: 1:1}" | tool "${@: 2:$#-2}" | tee "${@: $#:1}"

Let's break it down (Don't care? use one of the templates below!):

  • @ is a list of all the input parameters passed to the script

  • ${@: 1:1} follows the format ${@: START:COUNT} which means "starting from the parameter at START (index 1 here), get COUNT (1 in this case) parameters", which is practically equivalent to $1, the first parameter.

  • The first command cat "${@: 1:1}" will output the content of the file passed in the first parameter

  • $# refers to the number of parameter passed to the script

  • Following the same format tool "${@: 2:$#-2}" will pass all the parameters to tool except the first and last one

  • tee "${@: $#:1}" passes the last command to tee

So when it all comes together, this script allows us to use the following command

./run.sh input.txt -p1 v1 -p2 v2 output.txt

and it will be equivalent to

cat input.txt | tool -p1 v1 -p2 v2 | tee output.txt

Templates

STDIN only

If the tool expects input parameter through STDIN but accepts an output parameter, i.e. the original command follows this format

cat input.txt | tool -o output.txt

Use this

cat "${@: 1:1}" | tool "${@: 1:$#-1}"

STDOUT only

If the tool accepts an input parameter but writes the output to STDOUT, i.e. the original command follows this format

tool -i input.txt | tee output.txt

Use this

tool "${@: 1:$#-1}" | tee "${@: $#:1}"

Both STDIN and STDOUT

If the tool expects input parameter through STDIN and writes the output to STDOUT, i.e. the original command follows this format

cat input.txt | tool | tee output.txt

Use this

cat "${@: 1:1}" | tool "${@: 2:$#-2}" | tee "${@: $#:1}"

Where to Save This Script

run.sh

Save the script to a separate file named run.sh and add it to the module's folder (don't forget to add the shebang #!/bin/bash)


#!/bin/bash 
cat "${@: 1:1}" | tool "${@: 2:$#-2}" | tee "${@: $#:1}"

Changes in trickest.yaml

Main command

Use the script in the command parameter

Parameters

The input and output parameters must be adjusted as follows:

  • The output_parameter should be set to >
  • The output_type should be set to file
  • The input parameter's command should be left empty

command: 'cat "${@: 1:1}" | tool "${@: 2:$#-2}" | tee "${@: $#:1}"' 
output_parameter: '>'
# This is important output_type: file
inputs:
- name-of-parameter:
 command:
# Command value is empty  type: file
 description: DESCRIPTION
 order: 0
 visible: true

Dockerfile

The run.sh script should be copied to the docker image on build and used as an ENTRYPOINT


ADD run.sh /app/ 
RUN chmod +x /app/run.sh
RUN apk add bash
ENTRYPOINT ["/app/run.sh"]

Next up: Example Tool Configuration and Import