bash将输出重定向到文件 - 无法存储临时文本

I search about this and didn't fine an answer for it.

So my question is not about simple output redirection in Linux shell. I want it to happen inside a bash script I'm writing and I want output and error of anyscripts or commands I run in that script be redirected into separate files.

This is what im currently using:

STATUSFILE=x.out
LOGFILE=x.log
exec > ${STATUSFILE} 2>${LOGFILE}
echo "bla bla"
#some other commands

which works fine for normal outputs. But for example in part of my script I put to my backup server using ftp put. And when I run in manually I see kind of a progress bar saying how much time is passed and how much time its gonna take.

What im trying to do is to capture that kind of output which seems to be updating on the fly and kinda dynamic and currently I get other outputs stored in files but not that kinda dynamic ones.

And another question is if im following a good approach doing this. As I said I want the output and Im gonna use that by another process to see the progress and then use it to inform user trying to run a task with browser.

So for example in my case its uploading a big file into ftp storage which user will command it from browser and I run those scripts and I want to have kind of a progress bar showing up the status to the user. (I know how to handle it on client side - probably a socket connection or interval ajax request checking on a file or an event being triggered by server)

My second question is how should I get the progress of commands that have dynamic output.

If anything is unclear please let me know Ill try to fix it.

Thanks for your help guys.

Good Morning,

So my question is not about simple output redirection in Linux shell. I want it to happen inside a bash script I'm writing

That is quite simple. You just need to enclose your commands in {}.

#!/bin/bash

{

  echo "hello world!"
  ls -la 

} >> script.log

I want output and error of anyscripts or commands I run in that script be redirected into separate files.

You could use tee if the same output should go in seperate files.

#!/bin/bash

{

  echo "hello world!"
  ls -la 

} | tee foo bar baz

If the output of different commands should go in different files, you would just use different {} blocks.

My second question is how should I get the progress of commands that have dynamic output.

Some tools tend to put the progress bar to STDERR. You can redirect STDOUT and STDERR to same file using 2>&1. 2 is the descriptor for STDERR, 1 for STDOUT and you are basically appending 2 on 1.

Thus you would rewrite the first script to:

#!/bin/bash

{

  echo "hello world!"
  ls -la 

} >> script.log 2>&1

Be carefull with that, though. Some tools use colors and carriage return to display such nice formatted progress bars. This will look ugly in the log file.