linux重定向总结:如何将shell命令的输出信息自动输出到文件中保存

  在作批量实验室,例如跑批量MR的做业,咱们会写好shell脚本,而后启动脚本,等全部做业执行完再去看结果,可是这些执行时的信息如何保存下来到文件中呢?下面这个命令能够完成这个任务。html

sh batchjob.sh 2>&1 | tee mylog.log

  其中sh batchjob.sh:表示要执行的shell脚步;0,1,2:在linux分别表示标准输入、标准输出和标准错误信息输出。linux

  下面来总结下重定向问题。shell

 

输入输出重定向之:'<' and '>'

  '<' and '>'分别用来支持linux中的输入输出重定向,其中'<'支持输入重定向,'>'支持输出重定向。app

  1. '<':重定向输入oop

    sh test.sh < hadoop-hadoop-jobtracker-brix-00.out,将hadoop-hadoop-jobtracker-brix-00.out的内容做为test.sh的输入post

      2. '>':将内容全局覆盖式的加入文件,至关于删除该文件并从新创建该文件,再写入的效果this

        ls * > test.txt ,将ls * 的全部信息输出到文件test.txt中spa

  3. '>!':若是存在则覆盖code

  4. '>&':执行时屏幕上所产生的任何信息写入指定的文件中orm

  5. '>>':追加到文件中

  6. '>>&':屏幕上的信息追加到文件中

标准输入输出

  在 Linux 系统中:标准输入(stdin)默认为键盘输入;标准输出(stdout)默认为屏幕输出;标准错误输出(stderr)默认也是输出到屏幕(上面的 std 表示 standard)。在 BASH 中使用这些概念时通常将标准输出表示为 1,将标准错误输出表示为 2。下面咱们举例来讲明如何使用他们,特别是标准输出和标准错误输出。

tee命令

tee指令会从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件。

$ tee --help
Usage: tee [OPTION]... [FILE]...
Copy standard input to each FILE, and also to standard output.

  -a, --append              append to the given FILEs, do not overwrite
  -i, --ignore-interrupts   ignore interrupt signals
      --help     display this help and exit
      --version  output version information and exit

If a FILE is -, copy again to standard output.

Report tee bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'tee invocation'

 

转载于:https://www.cnblogs.com/gslyyq/p/4979095.html