action:Execute_a_Shell_Script
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| action:Execute_a_Shell_Script [2021/10/18 07:55] – [Path in Shell Scripts] peternlewis | action:Execute_a_Shell_Script [2025/09/12 07:02] (current) – [Quoting Strings] peternlewis | ||
|---|---|---|---|
| Line 7: | Line 7: | ||
| * It is best to always specify the language using the `#!` at the front of the script. | * 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. | * 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. | ||
| + | * The shell is a non-login shell, so configuration files (like .bashrc) will not be run - in particular, this means you will not have much in the PATH (see [[# | ||
| * For more details and examples, see [[https:// | * For more details and examples, see [[https:// | ||
| Line 41: | Line 42: | ||
| ==== Using Keyboard Maestro Variables ==== | ==== Using Keyboard Maestro Variables ==== | ||
| - | === Most Languages like bash, perl, ruby === | + | Keyboard Maestro variables are included in the environment of the script, with the prefix `KMVAR_` and spaces in the name replaced with underscores (‗), |
| + | |||
| + | By default, all variables are included, but you can select No Variables, or specific variables as desired using the popup menu next to the script (v11.0+). | ||
| + | |||
| + | === Most Shell Languages like bash === | ||
| In your script, use this format $KMVAR‗*VARIABLE‗NAME* where *VARIABLE‗NAME* is the name of your Keyboard Maestro Variable, but with spaces in the name replaced with underscores (‗). | In your script, use this format $KMVAR‗*VARIABLE‗NAME* where *VARIABLE‗NAME* is the name of your Keyboard Maestro Variable, but with spaces in the name replaced with underscores (‗). | ||
| Line 131: | Line 136: | ||
| The results of a shell script can be: | The results of a shell script can be: | ||
| - | * Ignored. | + | {{page> |
| - | * Displayed in a floating window. | + | |
| - | * Displayed briefly in a Notification. | + | |
| - | * Displayed large across the screen. | + | |
| - | * Typed in to the current selection. | + | |
| - | * Pasted in to the current selection. | + | |
| - | * Saved to a variable. | + | |
| - | * Appended to a variable (v9.0+). | + | |
| - | * Saved to the system or a Named Clipboard. | + | |
| - | * Asynchronously ignored — the action runs while the macro continues on. | + | |
| - | If you want to return multiple values from a script, then you can either | + | If the output is going to a clipboard or a file, the downloaded data can be an image (v11.0+). |
| + | |||
| + | If you want to return multiple values from a script, then you can use AppleScript (via `osascript`) to [[manual: | ||
| ===== Examples ===== | ===== Examples ===== | ||
| Line 176: | Line 174: | ||
| It is bash that has processed the variable substitution for $KMVAR_witchPref, | It is bash that has processed the variable substitution for $KMVAR_witchPref, | ||
| - | So the problem is that the word-breaking, | + | So the problem is that the word-breaking, |
| So variables that are meant to be a single parameter should be surrounded by double quotes in the line, but not contain quotes in the variable, and generally you do not want to pass multiple parameters within a single parameter. | So variables that are meant to be a single parameter should be surrounded by double quotes in the line, but not contain quotes in the variable, and generally you do not want to pass multiple parameters within a single parameter. | ||
| Line 223: | Line 221: | ||
| The tr command will replace `\r` or `\n` line endings with a nul character, and the `xargs -0` command will read that, split the arguments at the nul character, and pass them to the specified command (in this case `ls -l`. Note that xargs has a limit to the number of arguments it will pass, so for large numbers of arguments it may run the command multiple times with subsets of the arguments - read the `xargs` man page for more details. | The tr command will replace `\r` or `\n` line endings with a nul character, and the `xargs -0` command will read that, split the arguments at the nul character, and pass them to the specified command (in this case `ls -l`. Note that xargs has a limit to the number of arguments it will pass, so for large numbers of arguments it may run the command multiple times with subsets of the arguments - read the `xargs` man page for more details. | ||
| + | |||
| + | Also note that shell globing happens after variable expansion, and so if the variable contains `*` characters they will be expanded by the shell by matching file names in the current directory. To avoid this, you can turn off globing by using the -f option when executing bash - that is, start your script with `# | ||
| ===== Path in Shell Scripts ===== | ===== Path in Shell Scripts ===== | ||
| Line 252: | Line 252: | ||
| For example, if you want to use [[https:// | For example, if you want to use [[https:// | ||
| + | |||
| + | (!) Note that if in the action you exclude the ENV_ variables (including potentially the ENV_PATH) from being included in the environment (eg by selecting //Include No Variables// | ||
| ===== Working Directory ===== | ===== Working Directory ===== | ||
| Line 299: | Line 301: | ||
| === Actions === | === Actions === | ||
| + | * [[action: | ||
| * [[action: | * [[action: | ||
| * [[action: | * [[action: | ||
action/Execute_a_Shell_Script.1634543700.txt.gz · Last modified: by peternlewis
