...
Shell scripts can be written in any Shell available from the OS such as bash, ksh, zsh, dash etc. It is recommended to add a shebang to the first line of the script that indicates the Shell to be used:
Code Block |
---|
language | jsbash |
---|
title | Example how to invoke a Shell to run script code |
---|
linenumbers | true |
---|
|
#!/bin/bash
# alternative use for bash
#!/usr/bin/env bash
# frequently used shebangs
#!/bin/sh
#!/bin/ksh
#!/bin/zsh
#!/bin/dash |
...
In order to directly run PowerShell® script code from a JS7 shell job script the recommended approach is to use a shebang like this:
Code Block |
---|
language | bashpowershell |
---|
title | Example how run PowerShell® script code with a shebang |
---|
linenumbers | true |
---|
|
#!/usr/bin/env pwsh
Write-Output "Hello"
Write-Output "world" |
...
In order to directly run Python® script code from a JS7 shell job script the recommended approach is to use a shebang replacement like this:
Code Block |
---|
language | bashpy |
---|
title | Example how use Python script code with a shebang |
---|
linenumbers | true |
---|
|
#!/usr/bin/python
print("Hello")
print("world") |
...
Alternatively, a Node.js® script can be executed from a file that is located within reach of the JS7 Agent that runs the job:
Code Block |
---|
language | jsbash |
---|
title | Example how to run Node.js® script from a file |
---|
linenumbers | true |
---|
|
node /some/location/sample_Node.js |
...
Any commands available from the Windows Shell can be used in a job script like this:
Code Block |
---|
language | bashtext |
---|
title | Example how run Shell script code |
---|
linenumbers | true |
---|
|
@echo off
echo hello world
hostname |
...
This includes to call .bat and .cmd command files like this:
Code Block |
---|
language | bashtext |
---|
title | Example how run Shell script code |
---|
linenumbers | true |
---|
|
@echo off
call C:\Documents\hello.bat
call C:\Documents\world.cmd |
...
In order to directly run PowerShell® script code from a JS7 shell job script the recommended approach is to use a shebang replacement like this:
Code Block |
---|
language | bashpowershell |
---|
title | Example how run PowerShell® script code with a shebang replacement |
---|
linenumbers | true |
---|
|
@@findstr/v "^@@f.*&" "%~f0"|powershell.exe -&goto:eof
Write-Output "Hello"
Write-Output "world" |
...
Note: PowerShell 5.1 frequently ships with the Windows OS and makes use of powershell.exe
. Later PowerShell releases use the pwsh.exe
binary.
In addition, a PowerShell® script can be executed from a file that is located in reach of the JS7 Agent:
Code Block |
---|
language | bashtext |
---|
title | Example how to run PowerShell® script code from a file |
---|
linenumbers | true |
---|
|
powershell.exe -NoLogo -NonInteractive -File some_powershell_script.ps1 |
...
Python can be invoked to execute script code like this:
Code Block |
---|
language | bashpy |
---|
title | Example how use Python script code with a shebang |
---|
linenumbers | true |
---|
|
@@findstr/v "^@@f.*&" "%~f0"|python.exe -&goto:eof
print("Hello")
print("world") |
...
Alternatively a Python® script can be executed from a file that has to be located in reach of the JS7 Agent:
Code Block |
---|
language | bashtext |
---|
title | Example how to run Python® script code from a single line |
---|
linenumbers | true |
---|
|
python.exe hello.py |
...
In addition, a Node.js® script can be executed from a file located with the JS7 Agent that implements the job:
Code Block |
---|
language | jstext |
---|
title | Example how to run Node.js® script from a file |
---|
linenumbers | true |
---|
|
node.exe C:\Users\Documents\sample_Node.js |
Accessing Arguments
Shell jobs access arguments from OS environment variables.
- Such variables are declared with the workflow.
- Shell jobs map workflow variables to environment variables for the job.
- Shell job scripts make use of environment variables.
Declaring Workflow Variables
Variables are declared by a workflow like this:
- Variables are specified by a name and data type. Optionally a default value is specified:
- Orders are forced to specify values for workflow variables that are not assigned a default value.
- Orders are free to overrule default values of given workflow variables.
Image Added
Mapping Workflow Variables to Job Environment Variables
From the declared workflow variables jobs choose which variables to map to environment variables like this:
Image Added
Using Environment Variables in Jobs
Jobs make use of environment variables in the job script. Depending on the OS in use the following syntax is used:
- Unix:
${<variable-name>}
or $<variable-name>
- Names of variables are case-sensitive.
- Windows:
%<variable-name>%
- Names of variables are case-insensitive.
Image Added
Examples:
Code Block |
---|
language | bash |
---|
title | Example for use of Environment Variables in a Unix Shell job script |
---|
linenumbers | true |
---|
|
#!/bin/bash
echo "using workflow: $JS7_WORKFLOW_NAME"
echo "running job: $JS7_JOB_NAME"
echo "Business Date: $BUSINESS_DATE"
echo "Flight Number: $FLIGHT_NUMBER" |
Code Block |
---|
language | text |
---|
title | Example for use of Environment Variables in a Windows Shell job script |
---|
linenumbers | true |
---|
|
echo using workflow: %JS7_WORKFLOW_NAME%
echo running job: %JS7_JOB_NAME%
echo Business Date: %BUSINESS_DATE%
echo Flight Number: %FLIGHT_NUMBER% |
Code Block |
---|
language | powershell |
---|
title | Example for use of Environment Variables in a PowerShell job script |
---|
linenumbers | true |
---|
|
#!/usr/bin/env pwsh
echo "using workflow: $env:JS7_WORKFLOW_NAME"
echo "running job: $env:JS7_JOB_NAME"
echo "Business Date: $env:BUSINESS_DATE"
echo "Flight Number: $env:FLIGHT_NUMBER" |
Code Block |
---|
language | py |
---|
title | Example for use of Environment Variables in a Python job script |
---|
linenumbers | true |
---|
|
#!/usr/bin/python
import os
print('using workflow: ', os.environ.get('JS7_WORKFLOW_NAME')
print('running job: ', os.environ.get('JS7_JOB_NAME')
print('Business Date: ', os.environ.get('BUSINESS_DATE')
print('Flight Number: ', os.environ.get('FLIGHT_NUMBER') |
Code Block |
---|
language | js |
---|
title | Example how run Node.js® script code with a shebang |
---|
linenumbers | true |
---|
|
#!/usr/bin/node
console.log('using workflow: ' + process.env.JS7_WORKFLOW_NAME);
console.log('running job: ' + process.env.JS7_JOB_NAME);
console.log('Business Date: ' + process.env.BUSINESS_DATE);
console.log('Flight Number: ' + process.env.FLIGHT_NUMBER); |
Passing Variables
Further Resources
...