User Tools

Site Tools


action:Execute_a_Shell_Script

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
action:Execute_a_Shell_Script [2019/10/07 04:39]
peternlewis
action:Execute_a_Shell_Script [2019/11/25 16:53]
JMichaelTX
Line 2: Line 2:
  
 The //Execute a Shell Script// action executes a specified shell script using `/bin/sh`, either from a file or from the script embedded in the action. The //Execute a Shell Script// action executes a specified shell script using `/bin/sh`, either from a file or from the script embedded in the action.
 +
 +**Important:​**
 +
 +* It is best to always specify the language using the `#!` at the front of the script.
 +* The script is executed with the `sh` shell, so if you want to use another language, or use any special kinds of shell-specific syntax, then you should specify the language.
 +* For more details and examples, see [[https://​forum.keyboardmaestro.com/​t/​impact-of-macos-catalina-defaulting-to-unix-shell-using-zsh/​15503/​12?​u=jmichaeltx|How to Use Shebang at Top of Shell Script]]
  
 To execute the script from an external file, choose the "​Execute Script File" in the choices for script location. To execute the script from an external file, choose the "​Execute Script File" in the choices for script location.
Line 11: Line 17:
 If the script fails, the action will fail (v9.0+), potentially aborting the macro. If the script fails, the action will fail (v9.0+), potentially aborting the macro.
  
-The script is executed with the `sh` shell — if you want to use another language, or use any special kinds of shell-specific syntax, then you should specify the language using the `#!` at the front of the script.+
  
 ==== Using Keyboard Maestro Variables ==== ==== Using Keyboard Maestro Variables ====
Line 27: Line 33:
 {{:​action:​km7-shell-script.png?​nolink|}} {{:​action:​km7-shell-script.png?​nolink|}}
  
-=== Python ===+==== Python ​====
  
 Accessing Keyboard Maestro Variables in Python is somewhat different. ​ Here is an example: Accessing Keyboard Maestro Variables in Python is somewhat different. ​ Here is an example:
Line 40: Line 46:
  
 Note that you can only read these environment variables. ​ You cannot write to them (or more accurately, you can write to them but that will not change the Keyboard Maestro variables that they were created from). Note that you can only read these environment variables. ​ You cannot write to them (or more accurately, you can write to them but that will not change the Keyboard Maestro variables that they were created from).
 +
 +==== awk ====
 +
 +[Using shell variables in awk](https://​stackoverflow.com/​a/​15787068/​915019) is also different than in most other shell languages. ​ You need to pass the environment variable as parameter to the -v flag.
 +
 +Some examples:
 +
 +```bash
 +# EXAMPLE 1
 +
 +echo | awk -v my_var=4 '​{print "My var is " my_var}'​
 +#-->My var is 4
 +
 +# EXAMPLE 2
 +
 +VAR=3
 +echo | awk -v env_var="​$VAR"​ '​{print "The value of VAR is " env_var}'​
 +#-->The value of VAR is 3
 +
 +# USING KM VARIABLES with awk
 +
 +echo | awk -v env_var="​$KMVAR_My_KM_Variable"​ '​{print "The value of My_KM_Variable is: \""​env_var"​\""​}'​
 +#-->The value of My_KM_Variable is: "Text from KM var"
 +```
 +
 +For a KM Macro that shows this, see:\\
 +**[MACRO: ​  Using KM Variables with Bash awk [Example]](https://​forum.keyboardmaestro.com/​t/​passing-argument-to-awk/​16169/​3)**
  
 ==== Passing Paths in Variables ==== ==== Passing Paths in Variables ====
Line 275: Line 308:
 ==== Forum ==== ==== Forum ====
  
 +- [[https://​forum.keyboardmaestro.com/​t/​impact-of-macos-catalina-defaulting-to-unix-shell-using-zsh/​15503/​12?​u=jmichaeltx|How to Use Shebang at Top of Shell Script]]
 - [[https://​forum.keyboardmaestro.com/​t/​png-metadata-comment/​6375/​29|PNG Metadata "​Comment"​]] - [[https://​forum.keyboardmaestro.com/​t/​png-metadata-comment/​6375/​29|PNG Metadata "​Comment"​]]
 - [[https://​forum.keyboardmaestro.com/​t/​having-trouble-with-quotes-and-shell-variables/​6236/​9|Having trouble with quotes and shell variables]] - [[https://​forum.keyboardmaestro.com/​t/​having-trouble-with-quotes-and-shell-variables/​6236/​9|Having trouble with quotes and shell variables]]
action/Execute_a_Shell_Script.txt · Last modified: 2023/09/22 04:39 by peternlewis