Problem to execute a shell command with run()

Asked by Alain Bonnefoy on 2020-07-21

Hello,
The concerned command is a bit particular as it's defined like below where I pipe answers to a script:

    sh -c 'cd /home/my/path && printf "1\ny\ny\ny\ny\ny\n" | ./scripts/prepare_VM_data.sh'

This command perfectly works in bash but I want to execute it from Sikulix script (jython).

My code is:

    cmd = "sh -c 'cd /home/my/path; printf \"1\ny\ny\ny\ny\ny\n\" | ./scripts/prepare_VM_data.sh'"
    print cmd
    print run(cmd)

The output in Sikulix IDE's console is:

    sh -c 'cd /home/my/path; printf "1
    y
    y
    y
    y
    y
    " | ./scripts/prepare_VM_data.sh'
    1
    *****error*****
    /home/my/path;: -c: line 0: unexpected EOF while looking for matching `''
    /home/my/path;: -c: line 1: syntax error: unexpected end of file

I don't really understand the error and who complains about the bad syntax.

Regards,

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
Alain Bonnefoy
Solved:
2020-07-22
Last query:
2020-07-22
Last reply:
2020-07-22
RaiMan (raimund-hocke) said : #1

Your command might be to complex (embedded " and ') for the internal parameter eval of run().

You might try with subprocess.popen.

... but why don't you just use simple file handling:

out = open("/home/my/path /scripts/prepare_VM_data.sh, "w")
out.write("1\ny\ny\ny\ny\ny\n\")
out.close()

Alain Bonnefoy (alcare) said : #2

I don't want to write into the script file but pipe the parameters instead.
I' going to have a look at subprocess....

Alain Bonnefoy (alcare) said : #3

Sorry, not to pipe parameters but pipe answers to questions asked by the script

RaiMan (raimund-hocke) said : #4

LOL, sorry for my stupid suggestion --- to much beer already last night :-))

But what about:

out = open("/home/my/path/scripts/prepare_VM_data_parms.txt, "w")
out.write("1\ny\ny\ny\ny\ny\n\")
out.close()

cmd = ("sh", "-c", "/home/my/path/scripts/prepare_VM_data.sh </home/my/path/scripts/prepare_VM_data_parms.txt")
run(cmd)

Alain Bonnefoy (alcare) said : #5

LOL, certainly @RaiMan, no problem, I giving to try your suggestion. I think it's the solution.
thanks a lot.