How to Get 'Execute a Shell Script' to Work With Text Input?

Hi,

I got confused, and figured I need some help. I try to get 'Execute a Shell Script' v10+ to work with text input. Couldn't find explanation of the usage of input with text (or input with variable).

I would expect the input arrives as $1 or $1...$n but it doesn't.

image

What do I do wrong? Or what do I miss to get this simple example working? Thanks for the help.

1 Like

You need to use 'read' command to get input from stdin
'Read' command [Bash Read User Input - Javatpoint]
[action:Execute a Shell Script [Keyboard Maestro Wiki]]

image

Hi @tsboh. As @macdevign_mac mentioned, you need to use a read statement.

As an alternative you can reference a macro variable in a shell script. For example, to echo a variable local_A in a shell script, use:

echo "$KMVAR_local_A"

Two points of caution, however:

  1. $KMVAR_ is case sensitive.
  2. If the macro variable name include spaces, the spaces need to be substituted with underscores.

For more information, see: Execute a Shell Script

1 Like

Thank you, for your time and answer! I just found out that the documentation of 'Execute a Shell Script' states: 'You can choose what input (stdin) to send to the script'. Which means no $1...$n :blush:

For future reference for people running by this topic:

Regarding the (bash) read command. There are various processing options for stdin:

all depending on our needs of course.

Cheers

The only problem left is, that if there are several lines in the input AND the last line does not have a newline (at the end), it doesn't get read this way.

Solved too, based on this answer:

while IFS= read -r line || [ "$line" ]; do 
    echo "line: $line"
done
1 Like

Hey Guys,

My preferred method of reading STDIN into a variable in an Execute a Shell Script action:

Execute Shell Action – Read @working.kmmacros (2.0 KB)
Keyboard Maestro Export

When appropriate you can use the redirect alone.

sed -E 's!^!… !' </dev/stdin

-Chris

1 Like

Since related questions might come up now, or when someone later finds this thread, I thought it would be helpful to share this...

DOWNLOAD Macro File:
shell–processing multi-line stdin and KM variable.kmmacros (8.8 KB)


Macro-output