I wrote a python file which prints a number every 5 sec and I runned it by using the command python3 filename &gt;&gt; a.txt  But the output comes to the file after the completion of the  program. How to make it redirect instantaneously??
1353
Is output buffering enabled by default in Python's interpreter for sys.stdout?  If the answer is positive, what are all the ways to disable it?  Suggestions so far:   Use the -u command line switch Wrap sys.stdout in an object that flushes after every write Set PYTHONUNBUFFERED env var sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)   Is there any other way to set some global flag in sys/sys.stdout programmatically during execution?
96825
Say I have the following script:  #!/bin/bash for i in $(seq 1000) do     cp /etc/passwd tmp     cat tmp | head -1 | head -1 | head -1 &gt; tmp  #this is the key line     cat tmp done   On the key line, I read and write the same file tmp which sometimes fails.  (I read it is because of race conditions because the processes in the pipeline are executed in parallel, which I do not understand why - each head needs to take the data from the previous one, doesn't it? This is NOT my main question, but you can answer it as well.)  When I run the script, it outputs about 200 lines. Is there any way I can force this script to output always 0 lines (so the I/O redirection to tmp is always prepared first and so the data is always destroyed)? To be clear, I mean changing the system settings, not this script.  Thanks for your ideas.
44814
So I'm trying to run foo.exe, but I don't want the output to the terminal but into a file. Running foo.exe &gt; foo.txt should accomplish this for me, but it's not. When I'm running the exe-file, I get the output. The exe is working fine in other words. However, when I try to send the output to a file, the only thing I get is this:  'c:/Program' is not recognized as an internal or external command, operable program or batch file.   This only shows up when I try to send it to a file. Thinking that it could be the path (which is c:\Program Files (x86)\ and so on) which is misinterpreted, I tried specifying the output file like so: foo.exe &gt; c:\test.txt, but still no joy.  So, aside from stating that the binary that I am trying to run is poorly written, is there anything I can do to remedy this? Keep in mind that I do get valid output when simply running the exe, it just won't print nicely to a file. Obviously the output is there, the question is if there is some way to catch it.
30241
Similar problem as here  However, the solution given there is not universal.  My problem: I have a command that produces output to STDOUT. At the same time, I can specify via arguments to produce another output that goes straight to a file.  tcsh$ hmmscan --domtblout FILEOUTPUT Pfam-A.hmm query.fasta   Pfam-A.hmm is a database file (input); query.fasta is also an input file  How can I redirect FILEOUTPUT to STDOUT ?  Background: I plan to use the command on thousands of inputs and I need to run it for each of them separately. The command is called within a Python script, and I would like to read its FILEOUTPUT directly into the memory/a variable using the subprocess module. I know how to get the STDOUT with the subprocess module; so if I could redirect FILEOUTPUT to STDOUT...  I tried already stuff like this (ended up with errors or created files):  tcsh$ hmmscan --domtblout &gt;/dev/tty Pfam-A.hmm query.fasta tcsh$ hmmscan --domtblout &gt;(mypipe) Pfam-A.hmm query.fasta tcsh$ hmmscan --domtblout &gt;$ Pfam-A.hmm query.fasta tcsh$ hmmscan --domtblout &gt;($1) Pfam-A.hmm query.fasta tcsh$ hmmscan --domtblout &gt;cat Pfam-A.hmm query.fasta tcsh$ hmmscan --domtblout test.tab Pfam-A.hmm query.fasta &gt;/dev/tty
110910
Here's my current bash which is just a loop while true ; do       python3 /Users/Name/Desktop/pythoncode.py done  I want to terminate pythoncode.py if it doesn’t output anything to the terminal in 2 min
2539
Is there any way to create a virtual file, such that reading from the file actually reads from the stdout of a command; writing to the file is acually writing to the stdin of a command?  So far I have kludged this with an inotifywait on a file, which calls a command when the file is modified, taking it's input from the file and writing back to it.  I don't like that the inotifywait has to be constantly restarted though (and I have to ensure that it is always running). I only use this file perhaps twice a week.
26953
I ran into notes for a server I used to work on back in the day but the redirection does not look right. Maybe someone can confim?  These were entries in my crontab entered by a third-party vendor who maintained the app/database that ran in this HP-UX server.  15 21 * * 0-6 /usr/sys/bin/stop  q  &gt;/stopout   2&lt;&amp;1 # 30 21 * * 0-6 /usr/sys/force     q  &gt;/out       2&lt;&amp;1 # 55 23 * * 0-6 /usr/sys/start     q  &gt;/startout  2&lt;&amp;1 #   I found this in the Bash manual but this means that stderr and stdout is going out to >/{stopout,out,startout} while stderr is copying stdout (which means stdout is twice being written to stopout,out,startout?)? I'm confused :)  3.6.8 Duplicating File Descriptors The redirection operator  [n]&lt;&amp;word is used to duplicate input file descriptors. If word expands to one or more  digits, the file descriptor denoted by n is made to be a copy of that file  descriptor. If the digits in word do not specify a file descriptor open for  input, a redirection error occurs. If word evaluates to ‘-’, file descriptor n  is closed. If n is not specified, the standard input (file descriptor 0) is  used.
17073
func() {     echo 'hello'     echo 'This is an error' &gt;&amp;2 }  a=$(func) b=???   I'd like to redirect the stderr to b variable without creating a temporary file.   echo $b  # output should be: "This is an error"   The solution that works but with a temporary file:  touch temp.txt exec 3&lt; temp.txt a=$(func 2&gt; temp.txt); cat &lt;&amp;3 rm temp.txt   So the question is, how do I redirect the stderr of the  function func to the variable b without the need of a temporary file?
37357
Say, for example, that there's one process that's writing to a file. While the process is running, I opened the file in notepad. The process keeps  writing to the file.   Other than closing and reopening the file, is there any way for me to "refresh" the data that notepad is showing?
47297
I have a program that receives an argument and prints it every second:    ~# ./myapp 234 234 234 234 234 ...     I want to be able to run it in the background and right after a shell but it must be in one line (I don't want to explain why I need it because I don't want to make the discussion too complicated right now).    This is what I tried and the error I received:    ~# bash -c "./myapp 234 &amp;; sh"  bash: -c: line 0: syntax error near unexpected token `;' bash: -c: line 0: `./myapp 234 &amp;; sh'   I also tried with &amp;&amp; as separator:    ~# bash -c "./myapp 234 &amp; &amp;&amp; sh"  bash: -c: line 0: syntax error near unexpected token `&amp;&amp;' bash: -c: line 0: `./myapp 234 &amp; &amp;&amp; sh'   Any idea how I can solve it?    EDIT: Thanks to @roaima. I also found a link that talks about it:
62738
Let's say I compile the following file with pdflatex -shell-escape test.tex:   \documentclass{minimal}  \begin{document}  File listing is:  \immediate\write18{ls /usr}  \end{document}   This will send the output of the command ls /usr to report/log of pdflatex (primarily to stdout).   There are then two cases I'd like to utilize:    The output of ls /usr being included directly in the document (LaTeX stream). The output of ls /usr becoming the contents of a \newcommand (by this I mean, that I'd like the script to be executed when the \newcommand is executed first - and upon subsequent calls to the new command, the shell code should not be executed anew).    I have read through , but I'm not sure if this "pipe input" can be applied to \newcommand.   I have also read through  - and it seems that Tex' file I/O should be used; but I'm quite disliking the fact that I'd still have to redirect the script output (actually, in this case, the ls /usr output) to a file, and then read it in, to have it as contents of a command.   So, is there an easier way to achieve what I'd want (hopefully, illustrated through an example based on the above code)?     EDIT: Ehm, I should have asked one more question earlier :) I'll try with an edit here, although it will probably get missed .. :)   I originally asked for a \newcommand that will execute shell code only upon its definition (i.e., in a sense it is "cached"); and the answer from @egreg does exactly that. But then - would it be possible to have a different \newcommand definition, such that each time this newcommand is called, the shell command is executed anew? I.e. executing something like \@@input|"cat tempfile" (which cannot be executed as such), where tempfile changes between calls?
26127
This question may sound a bit stupid, but I can not really see the difference between redirection and pipes.  Redirection is used to redirect the stdout/stdin/stderr, e.g. ls &gt; log.txt.  Pipes are used to give the output of a command as input to another command, e.g. ls | grep file.txt.  But why are there two operators for the same thing?  Why not just write ls &gt; grep to pass the output through, isn't this just a kind of redirection also? What I am missing?
36883
I use :  exec &gt;script.log 2&gt;&amp;1   in my script to redirect all output to a file. At the end of the script I would like to print a message to the screen. How do I stop the redirection?
7780
I have a script which calls two commands:  long_running_command | print_progress   The long_running_command prints progress but I'm unhappy with it. I'm using print_progress to make it nicer (namely, I print the progress in a single line).  The problem: Connection a pipe to stdout also activates a 4K buffer, so the nice print program gets nothing ... nothing ... nothing ... a whole lot ... :)  How can I disable the 4K buffer for the long_running_command (no, I do not have the source)?
34480
I want to write batch or something, that will write output of top into a file on login.  I did top &gt;&gt; output-file, but it contains some strange character!  Can somebody give a simple tutorial about how to write batch file in Linux?
99360
Back in university, when we had to submit an assignment in CS, we would have to perform a series of steps including running script, date, whoami, etc., and then running our program.  The script command would pipe all text sent to the display to both the display and a specified file as well.  Ever since, I have been looking for a Dos and/or Windows version, but have come up empty. Some programs can be redirected to a file, but then the display is not echoed, and some programs don’t seem to work with redirection at all.  Any ideas?    Edit:  So far, the answers that I’ve gotten seem to work exactly like the standard redirection commands (&lt;, >, |). These do not work with all programs. For example, the Microsoft C++ compiler CL.EXE. If you run cl /? through a redirection command or pipe it through another program (such as TEE), you will not get the header/banner text.  Another example is a program I wrote a while back in Pascal (I think the last compile was in FreePascal). The help text does not get redirected at all. I have seen this occur with other programs as well like MKISOFS. It has a long help text, but cannot be paused by piping it through MORE or redirected to a file!  I have wondered about this for many years. I used to think that it may be because the text is being written directly to the screen (eg port B800) or something, but I have yet to pin down the cause, let alone find a program that can do this job.
83398
I'm using bash script on AMazon Linux.  When I want to redirect stderr and stdout to a file, I can run  ./my_process.pl &amp;&gt;/tmp/out.txt   Is there a way I can redirect this output to a file and continue to see it on the console (in my shell)?  How is this done?