【问题标题】:Create a stdout and stderr files using a multiple instruction (with cicles and statements) bash script使用多指令(带有 cicles 和语句)bash 脚本创建 stdout 和 stderr 文件
【发布时间】:2020-11-14 09:23:02
【问题描述】:

我有一个 bash 脚本,其中包含多个指令,例如循环和语句。

我想创建一个 stdout 和 stderr 文件来打印分析过程中可能发生的错误。

但是,此论坛和其他论坛中的建议无法正常工作:事实上,尽管分析正确执行,但没有生成文件。

这是我的代码框架:

#!bin/bash

{
start=`date +%s` ;
while IFS=, read -r col1 col2 ;
do
    name="$col1$col2" ; 
done < "/path/to/file.csv" ;
...
for file in ./*txt
do
    operation1
    operation2
done
} > stdout.txt 2stderr.txt

tar -zcf ./folder

我尝试用和 (&) 更改分号 (;) 但没有成功。你们有什么线索吗?

提前致谢。

【问题讨论】:

  • 你能提供几行 CSV 文件吗?当你说它不起作用时,哪个部分不起作用?您想要的行为与您看到的行为是什么?
  • 分号是不必要的。 2stderr.txt 可能会导致语法错误。哪些文件没有被创建?

标签: bash stdout stderr


【解决方案1】:

你很接近,假设你的operation1和operation2写stdout或stderr取决于每个命令的结果,你想要

#!/bin/bash

{
    commands

} 2>stderr.txt >stdout.txt

tar ... stuff

您重定向到stderr,然后重定向到stdout(最好两种顺序都可以)...但是重要的部分是@987654328 之间没有空格@ 和&gt;。

简单的工作示例

#!/bin/bash

{
    [ 1 -eq 1 ] && echo "good int"
    [ a -eq a ] && echo "good char"

} 2>stderr.txt >stdout.txt

结果

$ cat stdout.txt
good int

$ cat stderr.txt
./s.sh: line 5: [: a: integer expression expected

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2012-03-09
    • 2012-11-19
    相关资源
    最近更新 更多