Skip to content

Expand create file example to show how to inject expressions into bash scripts #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions _episodes/14-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ teaching: 10
exercises: 0
questions:
- "How do I create required input files from input parameters at runtime?"
- "How do I invoke a script rather than just a simple command line?"
- "How do I make inputs available to my script?"
objectives:
- "Learn how to create files on the fly during runtime."
- "Learn how to use expressions in bash scripts."
keypoints:
- "Use `InitialWorkDirRequirement` to specify input files that need to be
created during tool runtime."
---
Sometimes you need to create a file on the fly from input parameters,
such as tools which expect to read their input configuration from a file
rather than the command line parameters. To do this, use
`InitialWorkDirRequirement`.
rather than the command line parameters, or need a small wrapper shell script.

To generate such files we can use the `InitialWorkDirRequirement`.

*createfile.cwl*

Expand All @@ -22,6 +26,12 @@ rather than the command line parameters. To do this, use
~~~
{: .source}

Any [expressions](../13-expressions/index.html) like `$(inputs.message)` are expanded by the CWL engine before creating the file; here inserting the value at the input `message`.

> **Tip:** The _CWL expressions_ are independent of any _shell variables_ used later during command line tool invocation. That means that any genuine need for the character `$` should be **_escaped** with `\`, for instance `\${PREFIX}` above is expanded to `${PREFIX}` in the generated file to be evaluated by the shell script instead of the CWL engine.

To test the above CWL tool use this job to provide the input value `message`:

*echo-job.yml*

~~~
Expand All @@ -34,22 +44,26 @@ command line:

~~~
$ cwl-runner createfile.cwl echo-job.yml
[job createfile.cwl] /home/example$ cat \
example.conf > /home/example/output.txt
[job createfile.cwl] /private/tmp/docker_tmphrqxxcdl$ sh \
example.sh > /private/tmp/docker_tmphrqxxcdl/output.txt
Could not collect memory usage, job ended before monitoring began.
[job createfile.cwl] completed success
{
"example_out": {
"location": "file:///home/example/output.txt",
"basename": "output.txt",
"class": "File",
"checksum": "sha1$5d3f955d1bb862ec618bc2f7ca4c5fa29fa39e89",
"size": 22,
"checksum": "sha1$9045abe4bd04dd8ccfe50c6ff61820b784b64aa7",
"size": 25,
"path": "/home/example/output.txt"
}
}
Final process status is success
$ cat output.txt
CONFIGVAR=Hello world!
Message is: Hello world!
~~~
{: .output}



{% include links.md %}
12 changes: 6 additions & 6 deletions _includes/cwl/14-runtime/createfile.cwl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/usr/bin/env cwl-runner

class: CommandLineTool
cwlVersion: v1.0
baseCommand: ["cat", "example.conf"]
baseCommand: ["sh", "example.sh"]

requirements:
InitialWorkDirRequirement:
listing:
- entryname: example.conf
entry: |
CONFIGVAR=$(inputs.message)
- entryname: example.sh
entry: |-
PREFIX='Message is:'
MSG="\${PREFIX} $(inputs.message)"
echo \${MSG}

inputs:
message: string
Expand Down
4 changes: 2 additions & 2 deletions _includes/cwl/conformance-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@
output:
example_out:
class: File
checksum: sha1$5509385b089c054ac9f9890dccef666eda7eb79d
checksum: sha1$9045abe4bd04dd8ccfe50c6ff61820b784b64aa7
basename: output.txt
location: Any
size: 23
size: 25

# Section 15
- doc: Test for section 15
Expand Down