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 revisionPrevious revision
Next revision
Previous revision
action:Execute_a_Shell_Script [2023/09/18 06:31] – [Output of Results] peternlewisaction: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 [[#Path_in_Shell_Scripts|below]] for more information).
 * 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]] * 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]]
  
Line 136: Line 137:
  
 {{page>include:ScriptOutput#}} {{page>include:ScriptOutput#}}
 +
 +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:Scripting#AppleScript|set Keyboard Maestro variables]]. If you want to return multiple values from a script, then you can use AppleScript (via `osascript`) to [[manual:Scripting#AppleScript|set Keyboard Maestro variables]].
Line 171: Line 174:
 It is bash that has processed the variable substitution for $KMVAR_witchPref, split the line in to seven parts, processed the backslash in "Application\ Support" and then executed the defaults tool with the seven parts as arguments (the command itself is the 0th argument, normally mostly ignored by tools). It is bash that has processed the variable substitution for $KMVAR_witchPref, split the line in to seven parts, processed the backslash in "Application\ Support" and then executed the defaults tool with the seven parts as arguments (the command itself is the 0th argument, normally mostly ignored by tools).
  
-So the problem is that the word-breaking, variable substitution, de-quoting and de-backslash is all happening at as the line is processed by bash, after which you are left with quotes from within variable substitution, but they no longer have any meaning, they are just characters.+So the problem is that the word-breaking, variable substitution, de-quoting and de-backslash is all happening as the line is processed by bash, after which you are left with quotes from within variable substitution, but they no longer have any meaning, they are just characters.
  
 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 instead, perhaps set one variable to the setting name, and one variable to the type and one variable to the value, and then use a command like this: 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 instead, perhaps set one variable to the setting name, and one variable to the type and one variable to the value, and then use a command like this:
Line 218: 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 `#!/bin/bash -f`.
  
 ===== Path in Shell Scripts ===== ===== Path in Shell Scripts =====
Line 247: Line 252:
  
 For example, if you want to use [[https://www.perl.org/ | Perl]] with a custom library search path in the Execute a Shell Script action, you can also create a Keyboard Maestro variable `ENV_PERL5LIB` that will set the [[http://perldoc.perl.org/perlrun.html#ENVIRONMENT|PERL5LIB environment variable]]. For example, if you want to use [[https://www.perl.org/ | Perl]] with a custom library search path in the Execute a Shell Script action, you can also create a Keyboard Maestro variable `ENV_PERL5LIB` that will set the [[http://perldoc.perl.org/perlrun.html#ENVIRONMENT|PERL5LIB environment variable]].
 +
 +(!) 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//), this will result it them not being included and thus not setting the corresponding environment variables.
  
 ===== Working Directory ===== ===== Working Directory =====
action/Execute_a_Shell_Script.1695018686.txt.gz · Last modified: by peternlewis

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki