简体   繁体   中英

Redirect output in a bash script

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?

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

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

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