Skip to content
Advertisement

“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?

Advertisement

Answer

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).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement