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:48]
JMichaelTX ADD awk examples
action:Execute_a_Shell_Script [2020/07/08 22:43]
peternlewis Handling multiple arguments within a single variable
Line 33: Line 33:
 {{:​action:​km7-shell-script.png?​nolink|}} {{:​action:​km7-shell-script.png?​nolink|}}
  
-=== 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:
Line 47: Line 47:
 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). 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 ====
  
 [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 environment variable as parameter to the -v flag.
Line 210: Line 210:
 * `")` - 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