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/11/25 16:53]
JMichaelTX
action:Execute_a_Shell_Script [2021/01/09 07:16]
ccstone
Line 36: Line 36:
  
 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 56:
 ==== 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:
  
-echo | awk -v my_var=4 '​{print "My var is " ​my_var}' +awk -v myVar=4 'BEGIN {print "myVar is " ​myVar}'
-#-->My var is 4+
  
-EXAMPLE 2+--> My var is 4
  
-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+EXAMPLE 2 – Printing a shell variable ​with awk:
  
-echo | awk -v env_var="​$KMVAR_My_KM_Variable"​ '​{print "The value of My_KM_Variable is: \""​env_var"​\""​}'​ +myShellVariable='​What can I do for you today?'​ 
-#-->The value of My_KM_Variable is: "Text from KM var"+ 
 +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"
 ``` ```
  
Line 210: Line 227:
 * `")` - single quoted string * `")` - single quoted string
  
 +For the reverse problem, where you have a variable that contains multiple parameters, note that you cannot use quotes within a variable - by the time the shell is expanding variables, it has already processed quotes and will not do so again so quotes will just be regular characters passed to the target command and almost certainly result in errors. If possible, use seperate variables for seperate parameters, but if you cannot do that (for example you have a list of paths), store them as seperate lines in a variable (without any quoting or backslashes) and use a command like:
 +
 +```bash
 +echo "​$KMVAR_files"​ | tr '​\r\n'​ '​\0'​ | xargs -0 ls -l
 +```
 +
 +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.
  
 ===== Path in Shell Scripts ===== ===== Path in Shell Scripts =====
action/Execute_a_Shell_Script.txt · Last modified: 2023/09/22 04:39 by peternlewis