Having Trouble with Execute Shell Script

You must have missed how to use variable in a shell script:

image

See Using KM Variables in a Execute a Shell Script action

Thanks, but it doesn't work correctly even without variables. I don't know what I'm doing incorrectly.

You will need to be more specific than "doesn't work correctly" for us to help you.
I'd suggest that you start by uploading your macro that does NOT use KM variables, and then show us what happens where you run it.
A screen capture of the results would be great.

I'm not at all sure what you're up to but it doesn't seem to make sense to use a shebang for python and then try to run python from the command line. I'd expect to see python after the shebang.

Here's a little example of a python script following the shebang that runs reliably on my system. Maybe it will clarify the issue for you:

**Python test Macro (v9.0.6)

Python test.kmmacros (3.4 KB)

The python and python3 commands are not found in that shell action because:

  • you haven't specified a path to them
  • the vanilla shell instance run by KM doesn't inherit any path variables from the instance used by Terminal.app

Depending on your python installation(s), you would need to expand the bare string python3 for example, which is not resolvable by the shell, to something specific like:

/usr/bin/python3

or for 2.7, perhaps:

/usr/bin/python

PS the trick, I find, is always to work incrementally.

Find out how to successfully evaluate and display something atomic, like:

image

and then gradually elaborate, layer upon layer.

1 Like

I did everything you asked for in my earlier comments.

Thank you for the advice. It's brought me closer to having something functional. If I don't use variables, I can get it to work.

Adjusting for my path to Python, @ComplexPoint 's example worked.

@mrpasini 's example also worked, but I can't seem to figure out how to adapt it to my need.

Running this (note: no variables) returns a successful result.

But running this...

...fails with this log entry:

SyntaxError: invalid syntax
2020-09-28 16:54:36 Execute a Shell Script failed with script error: File "text-script", line 6
    /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 "/Users/pariah/Dropbox/_Crucial Files/System - Mac/Mixcloud_Uploader/main.py" + n
    ^
SyntaxError: invalid syntax. Macro “Trying” cancelled (while executing Execute Shell Script).

Similarly these...

Fail with KM engine log entries like this:

SyntaxError: invalid syntax
2020-09-28 17:00:12 Execute a Shell Script failed with script error: File "text-script", line 4
    write /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 "/Users/pariah/Dropbox/_Crucial Files/System - Mac/Mixcloud_Uploader/main.py" $KMVAR_kmVar
                                                                                                                                                    ^
SyntaxError: invalid syntax. Macro “Trying” cancelled (while executing Execute Shell Script).

I tried putting the variable in double quotes per https://wiki.keyboardmaestro.com/action/Execute_a_Shell_Script#Passing_Paths_in_Variables

but I get the error

SyntaxError: invalid syntax
2020-09-28 17:02:23 Execute a Shell Script failed with script error: File "text-script", line 4
    /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 "/Users/pariah/Dropbox/_Crucial Files/System - Mac/Mixcloud_Uploader/main.py" "$KMVAR_kmVar"
    ^
SyntaxError: invalid syntax. Macro “Trying” cancelled (while executing Execute Shell Script).

OK then. I am out of ideas.
I think @mrpasini has given you some great advice and suggestions.
I'll leave it up to you guys to resolve this if needed.

With that hash bang and import os you invoke evaluation of the string as Python source.

Which is fine, but then that filePath string (starting with /Library) is unquoted, so it doesn't constitute well-formed Python, and chokes the interpreter.

(The shell interpreter understands bare paths, as long as they contain no spaces, but the Python interpreter doesn't)

I don't understand, @ComplexPoint . Could you please show me what I need to type to get it to work?

I personally think the simplest and most direct approach is that of the my earlier post:

(Not sure that I can immediately see the advantage, in this case, of additionally wrapping it in a hashbang script, and having to disentangle shell processing from Python processing)

I don't understand the terms "hashbang script" and "disentangle shell processing." I know KM, not scripting. Someone wrote a Python script for me (and I don't know how it works or even how I installed Python). Now I need to execute that script via KM. It works without variables. How would I write the shell script to make it work with the one variable .yaml file path I show in my examples?

Then do not use my approach. It assumes you want to work directly with Python code.

You want to run Python from the command line. ComplexPoint has shown you how.

I wish that was the case, but I don't see it. @ComplexPoint presented a sample path for Python that doesn't work on my computer with or without variables. Again, I don't understand the phrases ComplexPoint used.

I have it running from the command line when I type it into Terminal. I also have it running without a variable in KM as a Shell Script action. I just can't get it running with the .yaml file path variable.

Could you please tell me what I need to write to make it work?

ComplexPoint addressed that. Maybe this discussion will help:

How to Escape Spaces Etc In When Passing Variable to Shell

Yes, he probably did, in a foreign language, which is the entirety of that post you referenced as well. I had previously read that post several times, but I don't know how to program Python, so I don't understand the underlying logic of it.

What I do know is that I have tried several methods Google says are "escaping spaces" to no avail. As you can see in my earlier screenshots, I've tried quotations marks, using a set variable kmVar action with the path (with quotes, without quotes), then using "$KMVAR_kmVar" in qutoes and without quotes, /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 in quotes and without quotes, "/Users/pariah/Dropbox/_Crucial Files/System - Mac/Mixcloud_Uploader/main.py" in quotes and without quotes... As per the discussion in How to escape spaces etc. in when passing variable to shell - #10 by Jack_Brannen .

I get that you're trying to teach a man to fish, but at a certain point, when the man doesn't live near the water or know how to bait a hook yet still needs to eat that day, he has to ask someone to catch him a fish while he learns. I'm asking you, @mrpasini , @ComplexPoint , and @JMichaelTX , to please tell me explicitly what to write to make this thing I've been researching and trying unsuccessfully to solve for myself for more than two weeks work. Please.

You've seen this thread and my posts in the forums the last three years: I do my best to help myself and to learn before I ask for assistance. I'm far out of my league here. I don't want to learn to program in Python. I asked someone to help me solve a problem. The solution was a Python script. Now I need help simply executing that Python script from KM. I hope you will tell me how to do that without responding, again, as if I have some knowledge of Python or Shell programming.

Well, here's a little checklist for you:

  1. Run just the Python part of your shell script with one argument (say, "python3 -h) and if that works go to step two.

  2. Run Python with the literal .py and .yaml filenames (no variables), double quoting them. I think you said that works too.

  3. Now try constructing your variable .yaml filename (the one that's apparently causing all the trouble) and just displaying it. Is that what you are after (none of us can tell from over here)? If so, continue.

  4. Substitute the .yaml filename with the variable being sure to double quote it.

If you get this far and it doesn't work, report what version of macOS are you running (it matters). And while you're waiting around for more fish, try removing any underscores from your filenames on Dropbox.

  1. Success: Shell Script:
    Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -h

  2. Success: Shell Script:
    /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 "/Users/pariah/Dropbox/_Crucial Files/System - Mac/Mixcloud_Uploader/main.py" "/Users/pariah/Dropbox/_Projects Files/Hard, Heavy & Hair/Shows - Distros/HHH 272/HHH_272.yaml"

3a. "just displaying it. Is that what you are after (none of us can tell from over here)?" I don't understand. I want to execute the same command as #2 above, but with replacing "/Users/pariah/Dropbox/Projects Files/Hard, Heavy & Hair/Shows - Distros/HHH 272/HHH_272.yaml" with "%PathHHH%/Shows/0%Variable%HHHShowNumber%/OUTPUT/HHH%Variable%HHHShowNumber%.yaml"

Doing that...

3b. FAILURE Shell Script:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 "/Users/pariah/Dropbox/_Crucial Files/System - Mac/Mixcloud_Uploader/main.py" "/Users/pariah/Dropbox/_Projects Files/Hard, Heavy & Hair/Shows - Distros/HHH 272/HHH_272.yaml"

Error reported in KM Engine.log:
FileNotFoundError: [Errno 2] No such file or directory: '%PathHHH%/Shows/0%Variable%HHHShowNumber%/OUTPUT/HHH_%Variable%HHHShowNumber%.yaml'

macOS version 10.15.7
Keyboard Maestro version: 9.0.6