1

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?

7
  • 2
    I wouldn't be surprised if it's caused by the unquoted variables in the shell script. But why are you using a shell script for something that could easily be done with Python?! Commented Apr 11, 2016 at 7:46
  • 3
    This seems like way too much code for this question. Will retract my close vote if you can trim it down to a minimal example. Commented Apr 11, 2016 at 7:51
  • @ChrisMartin, I've edited my code keeping minimal code. Could you please help now? Commented Apr 11, 2016 at 8:51
  • @Biffen, What do you mean when you say unquoted variables in shell script? I want to copy a file to another server, hence I wanted to use scp. In the code above, it's just a sample bash script. I have no clue how to copy a file to another server in Python. Commented Apr 11, 2016 at 8:53
  • 1
    @MayankPorwal filepath=$1 should be filepath="$1", etc. shellcheck.net is a great tool for checking such things. Commented Apr 11, 2016 at 9:16

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

I inserted a print(cmd) before os.system(cmd) and it worked. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.