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 [2020/07/08 22:43]
peternlewis Handling multiple arguments within a single variable
action:Execute_a_Shell_Script [2021/09/18 16:32]
ccstone [Using Keyboard Maestro Variables]
Line 32: Line 32:
  
 {{:​action:​km7-shell-script.png?​nolink|}} {{:​action:​km7-shell-script.png?​nolink|}}
 +
 +Keep in mind that `$KMVAR_[variable_name]` is a shell environment variable.
 +
 +  * This is format is directly accessible to `bash, sh, zsh, etc.`
 +  * But other languages will probably require you to reach out to the shell environment.
 +    * Perl for instance requires: `my $var = $ENV{KMVAR_VarName};​`
  
 ==== 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:
 +
 +REQUIRES: The following two Keyboard Maestro variables to exist and contain the requisite text:
 +
 + local_DataStr
 + local_CharToCount
  
 ```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_local_DataStr'​].count(os.environ['​KMVAR_local_CharToCount'​])
Line 49: Line 62:
 ==== awk ==== ==== 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.+[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 shell variable as parameter to the -v flag.
  
 Some examples: Some examples:
  
 ```bash ```bash
-# EXAMPLE 1+# EXAMPLE 1 – Using an awk variable in an awk script: 
 + 
 +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?
  
-echo | awk -v my_var=4 '​{print "My var is " my_var}'​ +# ---------------------------------------------------------------------------------------
-#-->My var is 4+
  
-# EXAMPLE ​2+# EXAMPLE ​3 – Using KM Variables with awk
  
-VAR=3 +(You must have a value in the Keyboard Maestro variable My_KM_Variable.)
-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+awk -v awkVariable="​$KMVAR_My_KM_Variable"​ 'BEGIN {print "The value of My_KM_Variable is: \""​awkVariable"​\""​}'​
  
-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: "Contents of KM Variable: My_KM_Variable"
-#-->The value of My_KM_Variable is: "Text from KM var"+
 ``` ```
  
action/Execute_a_Shell_Script.txt · Last modified: 2021/10/18 03:55 by peternlewis