Reputation: 3532
I have a bash script that runs something along the lines of:
python myscript.py --input=/path/file --someOption=value > /path/file
If I ran it without the redirect, everything works fine. If I run it with the redirect, the file gets truncated. I suspect python is executing the whole line including the redirect when in fact the redirect has to be executed by bash.
I tried:
exec "python myscript.py --input=/path/file --someOption=value"
but I get command not found error.
How do I get python to execute just the python portion, and the redirect to be executed by bash?
Upvotes: 0
Views: 1761
Reputation: 57640
The problem is you are using same file as input and output. Shell first opens the redirected file and it becomes empty. Your python script reads the empty file then.
It can easily be solved by using tee
.
python myscript.py --input=/path/file --someOption=value | tee /path/otherfile
Upvotes: 1
Reputation: 123400
You can't read from and write to the same file at the same time.
Redirect output to a temporary location and then overwrite the input:
if python myscript.py --input=/path/file --someOption=value > /path/file.tmp
then
mv /path/file.tmp /path/file
fi
Upvotes: 6