Amazon-ssm-agent: Allow $SHELL env variable to override shellCommand in runshellscript.go

Created on 24 Apr 2017  路  3Comments  路  Source: aws/amazon-ssm-agent

Hey,

In runshellscript.go the shellCommand is hard-coded to: var shellCommand = "sh"

So if I run a SSM document with bash specific commands, it will fail.

It would be great if the agent used something like os.Getenv("SHELL") if it was set, or falls back to sh

That way, users could set the SHELL env variable for the agent, in the same way proxies etc are configured.

Example doc:

{
    "schemaVersion": "2.0",
    "description": "Test Shell",
    "parameters": {},
    "mainSteps": [
        {
            "action": "aws:runShellScript",
            "name": "TestShell",
            "inputs": {
                "runCommand": [
                    "    #!/bin/bash",
                    "    IFS=', ' read -r -a array <<< 'item1, item2, item3'",
                    "    for item in \"${array[@]}\"; do echo \"$item\"; done"
                ]
            }
        }
    ]
}

Expected output:

item1
item2
item3

Actual output:

----------ERROR-------
failed to run commands: exit status 2
/var/lib/amazon/ssm//document/orchestration//TestShell/_script.sh: 2:
/var/lib/amazon/ssm//document/orchestration//TestShell/_script.sh: Syntax error: redirection unexpected

To reproduce in the console:

This works:
/bin/bash /var/lib/amazon/ssm/<instanceId>/document/orchestration/<commandId>/TestShell/_script.sh

This fails:
sh /var/lib/amazon/ssm/<instanceId>/document/orchestration/<commandId>/TestShell/_script.sh

Most helpful comment

Hello PaulAtkins,

Thank you for posting here. We will take note of this change and check if it is possible to add to the agent. We will update here with the fix or ETA.

All 3 comments

a possible workaround:

#!/bin/bash
if [ -z "$BASH_VERSION" ]
then
    exec bash "$0" "$@"
fi
# Your script here

https://stackoverflow.com/questions/19538669/run-bash-script-with-sh

Thanks @mariusmni, I've tested that workaround, and confirmed it does work.

Would be great if this got fixed in the Agent :)

Example doc with workaround

{
    "schemaVersion": "2.0",
    "description": "Test Shell",
    "parameters": {},
    "mainSteps": [
        {
            "action": "aws:runShellScript",
            "name": "TestShell",
            "inputs": {
                "runCommand": [
                    "    #!/bin/bash",
                    "    if [ -z \"$BASH_VERSION\" ]; then exec bash \"$0\" \"$@\"; fi",
                    "    IFS=', ' read -r -a array <<< 'item1, item2, item3'",
                    "    for item in \"${array[@]}\"; do echo \"$item\"; done"
                ]
            }
        }
    ]
}

Hello PaulAtkins,

Thank you for posting here. We will take note of this change and check if it is possible to add to the agent. We will update here with the fix or ETA.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pbobov picture pbobov  路  5Comments

thedevopsmachine picture thedevopsmachine  路  4Comments

heydonovan picture heydonovan  路  3Comments

johnnyplaydrums picture johnnyplaydrums  路  5Comments

vane2804 picture vane2804  路  5Comments