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 [2021/01/09 07:16]
ccstone
action:Execute_a_Shell_Script [2021/10/14 04:45]
peternlewis
Line 16: Line 16:
  
 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.
- 
- 
  
 ==== Using Keyboard Maestro Variables ==== ==== Using Keyboard Maestro Variables ====
Line 23: Line 21:
 === Most Languages like bash, perl, ruby === === Most Languages like bash, perl, ruby ===
  
-In your script, use this format:  `$KMVAR_[variable_name]`+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 (‗).
  
-where //​[variable‗name]//​ is the name of your Keyboard Maestro Variable, but with spaces in the name replaced with underscores (`‗`). +For example: ​ `File Name` would be formatted as $KMVAR‗File‗Name.
- +
-For example: ​ `File Name` would be formatted as `$KMVAR‗File‗Name`.+
  
 If your variable already has underscores,​ you can use it as is. If your variable already has underscores,​ you can use it as is.
Line 33: Line 29:
 {{:​action:​km7-shell-script.png?​nolink|}} {{:​action:​km7-shell-script.png?​nolink|}}
  
-==== Python ====+Keep in mind that $KMVAR‗*VARIABLE‗NAME* is a shell environment variable, and this format is directly accessible to `bash`, `sh`, `zsh`, etc. Other languages may need a different method to read the shell environment variables.
  
-Accessing ​Keyboard Maestro ​Variables in Python is somewhat different Here is an example:+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).
  
-REQUIRES: The following two Keyboard Maestro variables to exist and contain the requisite text:+==== Perl====
  
- local_DataStr +```perl 
- local_CharToCount+my $fileName = $ENV{KMVAR_File_Name};​ 
 +``` 
 + 
 +==== Python ====
  
 ```python ```python
 #​!/​usr/​bin/​env python #​!/​usr/​bin/​env python
-# Python Version: 2.7.x 
  
 import os import os
-print os.environ['​KMVAR_local_DataStr'].count(os.environ['​KMVAR_local_CharToCount'​])+print os.environ['​KMVAR_File_Name']
 ``` ```
  
 See the [[#​Forum|Forum]] section below for more Python examples. See the [[#​Forum|Forum]] section below for more Python examples.
- 
-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 ==== ==== awk ====
Line 61: Line 57:
  
 ```bash ```bash
-# EXAMPLE 1 – Using an awk variable in an awk script: +awk -v awkVariable="​$KMVAR_File_Name" 'BEGIN {print "The value of VAR is: " awkVariable}'​
- +
-awk -v myVar=4 'BEGIN {print "myVar is " myVar}'​ +
- +
-# --> My var is 4 +
- +
-# --------------------------------------------------------------------------------------- +
- +
-# EXAMPLE 2 – Printing a shell variable with awk: +
- +
-myShellVariable='​What can I do for you today?'​ +
- +
-awk -v awkVariable="​$myShellVariable" 'BEGIN {print "The value of VAR is: " awkVariable}'​ +
- +
-# --> The value of VAR is: What can I do for you today? +
- +
-# --------------------------------------------------------------------------------------- +
- +
-# EXAMPLE 3 – Using KM Variables with awk +
- +
-(You must have a value in the Keyboard Maestro variable My_KM_Variable.) +
- +
-awk -v awkVariable="​$KMVAR_My_KM_Variable"​ 'BEGIN {print "The value of My_KM_Variable is: \""​awkVariable"​\""​}'​ +
- +
-# --> The value of My_KM_Variable is: "​Contents of KM Variable: My_KM_Variable"​+
 ``` ```
- 
-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 ====
  
 If you are passing a file/folder path in a Keyboard Maestro Variable, then it is best to put the Variable reference in double quotes so that spaces in the path will work, like this: If you are passing a file/folder path in a Keyboard Maestro Variable, then it is best to put the Variable reference in double quotes so that spaces in the path will work, like this:
 +
 ```bash ```bash
 cat "​$KMVAR_File_Path"​ cat "​$KMVAR_File_Path"​
Line 100: Line 69:
 **Consider These Guidelines** **Consider These Guidelines**
  
-  * This must be a full path.  **If the path starts with a tilde (~)**, then you must first convert it to a full path using the [[action:​Filter | Filter, Expand tilde (~) paths]] ​Action BEFORE ​the Execute Shell Script ​Action.+  * This must be a full path.  **If the path starts with a tilde (~)**, then you must first convert it to a full path using the [[action:​Filter | Filter, Expand tilde (~) paths]] ​action **before** ​the Execute Shell Script ​action.
   * Like all Bash variables, the Keyboard Maestro Variable must _not_ be between single quotes (`'`) in order for the variable to be expanded to / replaced by its value. ​ So, for example, **this will _not_ work**:   * Like all Bash variables, the Keyboard Maestro Variable must _not_ be between single quotes (`'`) in order for the variable to be expanded to / replaced by its value. ​ So, for example, **this will _not_ work**:
  
 ```bash ```bash
 # 🚫 Does NOT Work # 🚫 Does NOT Work
-/​usr/​local/​bin/​emacsclient -e '(w3m-browse-url ​"$KMVAR_SafariURL")'+echo 'The variable is "$KMVAR_File_Name"'​
 ``` ```
 +
 because although the Variable is between double-quotes,​ it is in a string that is between single-quotes. because although the Variable is between double-quotes,​ it is in a string that is between single-quotes.
  
Line 113: Line 83:
 ```bash ```bash
 # ✅ This WORKS # ✅ This WORKS
-/​usr/​local/​bin/​emacsclient -e '(w3m-browse-url ​"'"​$KMVAR_SafariURL"'"​)'+echo 'The variable is "'"​$KMVAR_File_Name"'"'​
 ``` ```
 +
 One key Bash feature that makes this work is that when two quoted strings are adjacent, they will be concatenated. ​ So after the Variable is expanded, the command string might look like this: One key Bash feature that makes this work is that when two quoted strings are adjacent, they will be concatenated. ​ So after the Variable is expanded, the command string might look like this:
  
 ```bash ```bash
-/​usr/​local/​bin/​emacsclient -e '(w3m-browse-url ​"https://www.apple.com")'+echo 'The variable is "Test.txt"'​
 ``` ```
- 
-For more info, see these discussions in the Keyboard Maestro Forum: 
-  * [[https://​forum.keyboardmaestro.com/​t/​png-metadata-comment/​6375/​29| How To Quote Paths by @ccstone]]. 
-  * [[https://​forum.keyboardmaestro.com/​t/​trouble-expanding-variable-in-shell-script/​13664/​2?​u=jmichaeltx | Trouble Expanding Variable in Shell Script by @PeterNLewis]] 
  
 ==== UTF-8 and Non-ASCII Characters ==== ==== UTF-8 and Non-ASCII Characters ====
Line 137: Line 104:
 * Trim Results — removes white space from the start and end of the scripts results. * Trim Results — removes white space from the start and end of the scripts results.
 * Include Errors — include stdout and stderr results from the script. * Include Errors — include stdout and stderr results from the script.
- 
  
 ==== Output of Results ==== ==== Output of Results ====
Line 155: Line 121:
  
 If you want to return multiple values from a script, then you can either 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 either use AppleScript (via `osascript`) to [[manual:​Scripting#​AppleScript|set Keyboard Maestro variables]].
 +
 ===== Examples ===== ===== Examples =====
  
Line 195: Line 162:
 ``` ```
  
-Now a further complication happens when you want to sent a string containing double quotes and variables to a parameter. ​ For example, perhaps you want a command like this:+Now a further complication happens when you want to send a string containing double quotes and variables to a parameter. ​ For example, perhaps you want a command like this:
  
 ```bash ```bash
Line 237: Line 204:
 ===== Path in Shell Scripts ===== ===== Path in Shell Scripts =====
  
-**In essence, the _default_ path in a Keyboard Maestro Execute Shell Script is the base path for the system:**+In essence, the _default_ path in a Keyboard Maestro Execute Shell Script is the base path for the system:
  
     /​usr/​bin:/​bin:/​usr/​sbin:/​sbin     /​usr/​bin:/​bin:/​usr/​sbin:/​sbin
  
-that is the script will search for tools in the `/usr/bin` directory, then in the `/bin` directory, then `/usr/sbin` and finally `/​sbin`. ​ Only tools installed by the system will be in these directories - any tools you have installed will almost certainly be elsewhere and so not found by default because:+That is the script will search for tools in the `/usr/bin` directory, then in the `/bin` directory, then `/usr/sbin` and finally `/​sbin`. ​ Only tools installed by the system will be in these directories - any tools you have installed will almost certainly be elsewhere and so not found by default because:
  
-  * **shell ​scripts are executed in non-interactive mode** (see the INVOCATION section of the sh man page), the **only** path preset by default is the system path. +  * Shell scripts are executed in non-interactive mode (see the INVOCATION section of the sh man page), the **only** path preset by default is the system path. 
-  ​* **Thus, your tools or files may not be automatically accessible.**+  * Thus, your tools or files may not be automatically accessible.
   * The `$PATH` environment variable you may have set in Terminal is **not** used.   * The `$PATH` environment variable you may have set in Terminal is **not** used.
-  * None of your profile scripts (like *~/.profile*) will be executed.+  * None of your profile scripts (like `~/.profile`) will be executed.
   * Custom environment variable settings will not be applied, including any settings for tool-specific environment variables like PERL5LIB.   * Custom environment variable settings will not be applied, including any settings for tool-specific environment variables like PERL5LIB.
  
-**How To Set a Path** +Generally, ​the best thing to do is to use full paths when referring ​to tools, such as `/usr/​local/​bin/perl`however some tools may access sub-tools ​that they cannot find without ​the PATH (or other environment ​variables) being set.
-  * If you need access to tools (executables) or files that you installed (not part of the system installation),​\\ **You will need to do one of the following:​**\\ ​   +
-    * **Use an explicit, full path** to the tool or file.\\ See [[#​Passing_Paths_in_Variables|Passing Paths in Variables]] in the above section.\\ OR +
-    * **Set the `$PATH` environment variable within each Execute Shell Script action**.\\ OR +
-    * **Set `ENV_PATH`** (the Keyboard Maestro Path Variable) **prior to the Execute Shell Script Action.** +
-      * The path defined in this Variable will automatically apply to every Execute Shell Script, without need for further reference. ​ It will work just like your `$PATH` works in the Terminal app. +
-      * You can set this Variable manually in the [[manual:​Windows#​Example|Variable Preferences Panel]] of the _Keyboard Maestro App Preferences_. +
-      * Once the `ENV_PATH` Variable ​is set, it will remain in your Keyboard Maestro Variable set (until/​unless you delete it), available ​to every Execute Shell Script Action that you might use in the future. ​ So you don't need to set it in every Macro. +
-      * If your tool requires other environment variables to be set you can set them as well by creating an appropriate Keyboard Maestro variable with `ENV_` at the front. +
-        * For example, if you want to use [[https://www.perl.org/ | Perl]] with a custom library search path in a Execute Shell Script actionyou can also create a Keyboard Maestro Variable `ENV_PERL5LIB` ​that will set the the [[http://​perldoc.perl.org/​perlrun.html#​ENVIRONMENT|PERL5LIB ​environment ​variable]] for each Execute Shell Script.+
  
-**Setting ​the ENV‗PATH Keyboard Maestro Variable**+If you need access to tools (executables) or files that you installed (not part of the system installation), ​**you will need to do one of the following:​** 
 +    * Use an explicit, full path to the tool or file. See [[#​Passing_Paths_in_Variables|Passing Paths in Variables]] in the above section. **or** 
 +    * Set the `$PATH` environment variable within each Execute Shell Script action. ​**or** 
 +    * Set `ENV_PATH` Keyboard Maestro variable prior to the Execute Shell Script Action.
  
-A shell script path that will work for many users is:+Any Keyboard Maestro variable ​that starts with `ENV_` will automatically be set as the as the corresponding environment variable (without the usual `KMVAR_` prefix). So if you set your path in the `ENV_PATH` Keyboard Maestro variable, the `PATH` environment variable ​will be set from it.
  
-    /​usr/​local/​bin:/usr/​bin:/​bin:/​usr/​sbin:​/sbin+You can set this Variable manually in the [[manual:manual/Preferences#​Variables_Preferences|Variable Preferences Panel]] in Keyboard Maestro. Once the `ENV_PATH` variable is set, it will remain in your Keyboard Maestro variable until/unless you delete it, and remain available to every Execute a Shell Script action that you might use in the future. ​ So you don’t need to set it in every Macro.
  
-Of course, if you have tools or files you installed elsewhere, then you would need to include ​the path to those in this path statement, IF you plan to use any of those tools/files in a Execute Shell Script Action.+If your tool requires other environment variables to be set you can set them as well by creating an appropriate Keyboard Maestro variable with `ENV_` at the front.
  
-You can create the `ENV_PATH` Variable one time using the [[manual:Windows#​Example|Variable Preferences Pane]] of the _Keyboard Maestro Preferences_. +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 ​Keyboard Maestro ​variable `ENV_PERL5LIBthat will set the [[http://perldoc.perl.org/perlrun.html#​ENVIRONMENT|PERL5LIB environment variable]].
-  * Add a variable ​named `ENV_PATHand set its value. +
-  * Once set, it will remain unchanged until you change or delete it. +
- +
-{{:​action:​env_path-var-preferences-km-7.3.png?​nolink|}} +
- +
-For a good discussion about this see:   +
-  * [KM can"t find shell commands ](https://forum.keyboardmaestro.com/t/​km-cant-find-shell-commands/​5430) +
-  * [[https://​forum.keyboardmaestro.com/​t/​png-metadata-comment/​6375/​16Post by @ccstone]]+
  
 ===== Execute Script From Other Apps ===== ===== Execute Script From Other Apps =====
  
-When you execute a shell script from other apps, like Terminal.app,​ the shell does NOT have access to the Keyboard Maestro Engine environment,​ and thus it does NOT know anything about Keyboard Maestro Variables.+When you execute a shell script from other apps, like Terminal.app,​ the shell does **not** ​have access to the Keyboard Maestro Engine environment,​ and thus it does **not** ​know anything about Keyboard Maestro Variables.
  
 In order to access Keyboard Maestro Variables in these scripts, you must use a tool like [[https://​developer.apple.com/​library/​mac/​documentation/​Darwin/​Reference/​ManPages/​man1/​osascript.1.html|osascript]]. In order to access Keyboard Maestro Variables in these scripts, you must use a tool like [[https://​developer.apple.com/​library/​mac/​documentation/​Darwin/​Reference/​ManPages/​man1/​osascript.1.html|osascript]].
  
-(!)  Note that you do NOT need to replace spaces with underscores in your Variable name in this case, since you are ultimately using AppleScript in the Shell Script. +Here is a simple example to **get** a variable:
- +
-Here is a simple example ​that works with Keyboard Maestro Ver 6+ to **get** a variable:+
 <​code>​ <​code>​
-osascript -e 'tell application "​Keyboard Maestro Engine"​ to get value of variable ​"My KM Var"'​+osascript -e 'tell application "​Keyboard Maestro Engine"​ to getvariable ​"My KM Var"'​
 </​code>​ </​code>​
  
-If you are using Keyboard Maestro 7.1+ you can use the new simpler command to **get** variable+(!)  Note that you do **not** replace spaces with underscores in your variable ​name in this case, since you are ultimately using AppleScript in the Shell Script.
-<​code>​ +
-osascript -e 'tell application "​Keyboard Maestro Engine"​ to getvariable "My KM Var"'​ +
-</​code>​+
  
-To **set** a variable (and create if needed) ​with Ver 7.1+ you can use this:+To **set** a variable (and create if needed) you can use this:
 <​code>​ <​code>​
 osascript -e 'tell application "​Keyboard Maestro Engine"​ to setvariable "My KM Var" to "Some new value"'​ osascript -e 'tell application "​Keyboard Maestro Engine"​ to setvariable "My KM Var" to "Some new value"'​
Line 351: Line 299:
 - [[https://​forum.keyboardmaestro.com/​t/​using-installed-shell-tools-from-km/​4341/​10|Using installed shell tools from KM]] - [[https://​forum.keyboardmaestro.com/​t/​using-installed-shell-tools-from-km/​4341/​10|Using installed shell tools from KM]]
 - [[https://​forum.keyboardmaestro.com/​t/​feature-request-execute-python-script/​4927/​6|[feature request] execute python script]] - [[https://​forum.keyboardmaestro.com/​t/​feature-request-execute-python-script/​4927/​6|[feature request] execute python script]]
 +For more info, see these discussions in the Keyboard Maestro Forum:
 +- [[https://​forum.keyboardmaestro.com/​t/​png-metadata-comment/​6375/​29| How To Quote Paths by @ccstone]].
 +- [[https://​forum.keyboardmaestro.com/​t/​trouble-expanding-variable-in-shell-script/​13664/​2?​u=jmichaeltx | Trouble Expanding Variable in Shell Script by @PeterNLewis]]
 +- [[https://​forum.keyboardmaestro.com/​t/​km-cant-find-shell-commands/​5430| KM can’t find shell commands]]
 +- [[https://​forum.keyboardmaestro.com/​t/​png-metadata-comment/​6375/​16| Post by @ccstone]]
  
 - [[https://​forum.keyboardmaestro.com/​tags/​shellscript|Keyboard Maestro Forum topics about Shell Script]] - [[https://​forum.keyboardmaestro.com/​tags/​shellscript|Keyboard Maestro Forum topics about Shell Script]]
  
 **Keywords:​** Quote String, Bash, Shell Scripting **Keywords:​** Quote String, Bash, Shell Scripting
action/Execute_a_Shell_Script.txt · Last modified: 2023/09/22 04:39 by peternlewis