简体   繁体   中英

“Syntax error: Unterminated quoted string” error when calling a bash script from Python

I have a situation where, I need to call a bash script inside a python script which is in turn called inside another python script.

download-output-files.py:

#!/usr/bin/env python
import os
import sys

for i in node_path:
    cmd="python watcher.py "+i
    os.system(cmd) ##calling another python script

watcher.py:

#!/usr/bin/env python
import os
import time

        if 'finish' in child:
            print "finish found"
        cmd="./copy_output_file.sh "+node   
        os.system(cmd) ##Calling shell script here

copy_output_file.sh:

#!/bin/bash

filepath=$1
cp ff /home/likewise-open/TALENTICA-ALL/mayankp/kazoo/$filepath

When I run download-output-files.py , it calls watcher.py , which in turn calls copy_output_file.sh and below is the error I face:

mayankp@mayankp:~/kazoo$ python download-output-files.py 

finish found
sh: 1: Syntax error: Unterminated quoted string

When I run the same commands in Python shell, it runs bash script successfully. What am I missing?

It's generally unwise to concatenate strings into shell commands. Try inserting print(cmd) before your os.system(cmd) calls to find out exactly what commands you're trying to run, and I supect you'll notice what the problem is (likely a file name with an apostrophe in it).

Try using subprocess.call(['python', 'watcher.py', i]) instead of os.system("python watcher.py "+i) , and subprocess.call(['/copy_output_file.sh', node]) instead of os.system(cmd="./copy_output_file.sh "+node) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM