0

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?

1
  • python isn't doing the redirect, the shell is, and yes it does that before running the command. I don't know what you were expecting that exec attempt to do exactly. Commented Sep 23, 2014 at 16:33

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

2 Comments

I thought the python will read the file in, process it, print the results to stdout, then terminate at which point the output will be redirected.
@ventsyv, redirection happen before the command is started; in this case, it runs the command with its FD 1 writing to the already-truncated /path/file inode. Since it's already truncated, one obviously can't read input from there.
1

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

2 Comments

tee isn't necessary here; you're just using a different output file, which will work with simple redirection as well.
That was my old answer. As someone already covered it, I mentioned tee

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.