程序的输出和输出到标准错误中的输出

今天编译运行程序的时候,发现在程序中的断点输出 和 输出到操作系统标准错误中的输出是不一样的!!!哇哇,哈哈,开心,又多了解了一点知识。

上一段golang 的代码

package main

import (
	// "errors"
	"fmt"
	// "os"
	"log"
)

func main() {
	/* local variable definition */

	/* function for division which return an error if divide by 0 */
	ret := 12
	log.SetPrefix("now: ")
	fmt.Println(log.Prefix())
	fmt.Println(ret)
	fmt.Println(ret)
	fmt.Println(ret)
	log.Print("aaaaaaaa")

	// fmt.Println(ret)
	// fmt.Println(ret)
	// os.Exit(21)
	// json := "lin1"
	// json = "%" + json + "%"
	// fmt.Println(json)
}

  set GOOS=linux 后编译位linux 下的可执行程序, go build -o "main"

 运行main ,  将程序的输出 和 输出到标准错误中的输出分别重定向到不同的文件:

这样的操作命令, 可以将程序的输出和输出到标准错误中的输出分别输出到不同的文件。

./main >a.txt   2>b.txt

下面分别查看 a.txt 和 b.txt 的内容:vim  -p   a.txt    b.txt

a.txt

b.txt

 -------------------------------------------------------------

Linux Shell 环境中的输入输出重定向,用符号<和>来表示。0、1和2分别表示标准输入、标准输出和标准错误。

1.重定向标准输出到文件:
cat foo > foo.txt
2.重定向标准错误到文件
cat foo 2> foo.txt
3.重定向标准输出到标准错误
cat foo 1>&2
4.重定向标准错误到标准输出
cat foo 2>&1
5.重定向标准输出,标准错误到同一个文件
cat foo > foo.txt 2>&1或cat foo &> foo.txt
原文地址:https://www.cnblogs.com/oxspirt/p/8081543.html