This shows you the differences between two versions of the page.
Both sides previous 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/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: |
+ | |||
+ | 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" | + | |
``` | ``` | ||