简体   繁体   中英

bash script does not read lines when called form python script

I have two simple scripts - I am trying to pass some information (date as input into the python script) to the bash script. Here's the python one:

#!/usr/local/bin/python

import os
import sys
import subprocess

year = "2012"
month = "5"
month_name = "may"

file = open('date.tmp','w')
file.write(year + "\n")
file.write(month + "\n")
file.write(month_name + "\n")
file.close

subprocess.call("/home/lukasz/bashdate.sh")

And here's the bash one:

#!/bin/bash

cat /home/lukasz/date.tmp | \
while read CMD; do
    echo -e $CMD
done
rm /home/lukasz/date.tmp

Python script works fine without issues. It calls the bash script but it looks like the while loop just does not run. I know the bash script does run overall because the rm command gets executed and the date.tmp file is removed. However if I comment out the subprocess call in python then run the bash script manually it works fine displaying each line.

Brief explanation of what I am trying to accomplish. I have a python script that exports a very large DB to CSV (almost 300 tables and a few gigs of data) which then calls the bash script to zip the CSVs into one file and move it to another location. I need to pass the month and year supplied to the python script to the bash script.

I believe that you need file.close() instead of file.close . With the latter, you're not actually closing the file since you don't call the method. Since you haven't actually closed the file yet, it might not be flushed and so the entire contents of the file might be buffered rather than written to disk.

As a side note, these things are taken care of automatically if you use a context manager:

with open('foofile','w') as fout:
    fout.write("this data")
    fout.write("that data")

#Sleep well tonight knowing that python guarantees your file is closed properly
do_more_stuff(blah,foo,bar,baz,qux)

Instead of writing a temp file, send the values of year, month, and month-name to the bash script as parameters . Ie, in the Python code remove all the lines with file in them, and replace
subprocess.call("/home/lukasz/bashdate.sh")
with
subprocess.call(['/home/lukasz/bashdate.sh', year, month, month_name])

and in the bash script, replace the cat ... rm lines with (eg)
y=$1; m=$2; mn=$3
which puts the year, month, and month-name into shell variables y, m, and mn.

也许尝试将shell=True添加到调用中:

subprocess.call("/home/lukasz/bashdate.sh", shell=True)

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