[英]Redirect stdout and stderr when ampersand(&) is used between commands in bash
I am writing a bash script to pull master branch from origin.Git is initialized in every app module. 我正在编写一个bash脚本以从原点提取master分支.Git在每个应用模块中都已初始化。
My directory structure: 我的目录结构:
.-----bash_script
|----app1(git init)
|...various modules
|...error.txt
|...output.txt
|----app2(git init)
|...various modules
|...error.txt
|...output.txt
In bash_script: $directory contains (app1 or app2... it is called in a loop) 在bash_script中:$ directory包含(app1或app2 ...在循环中调用)
cd "${directory}" && git checkout master && git pull origin master >> output.txt 2>>error.txt
cd ..
The above two lines are called for every app. 上面的两行针对每个应用程序调用。 This is what i have done so far 这是我到目前为止所做的
Problem: 问题:
I am only getting the stdout/stderr
only for my last command ie pulling from origin and not from checkout . 我只为我的最后一条命令获取stdout/stderr
,即从原点而不是从checkout提取 。 Is it possible to get the output of whole command without writing the file names before every &&
?? 是否可以在不&&
之前写文件名的情况下获得整个命令的输出? then how 那怎么
error.txt/output.txt files are generated inside the app directory. error.txt / output.txt文件在应用目录内生成。 How can I generate them in the bash_script directory ie one level up. 我如何在bash_script目录(即上一级)中生成它们。
when git pull origin master is executed 当执行git pull origin master时
From http://gitProjectUrl branch master -> FETCH_HEAD
The above line is redirected in error.txt . 上一行在error.txt中重定向。
Already up-to-date.
And this line is redirected in output.txt 并且此行在output.txt中重定向
Why both the lines are not in output.txt ?? 为什么这两行都不在output.txt中 ?
Use a subshell (...)
to collect all the outputs in a single stream: 使用子shell (...)
在单个流中收集所有输出:
( cd ... && ... && git pull origin master ) >> output.txt 2>>error.txt
This solves both problems since the streams are prepared before the cd
command. 因为流是在cd
命令之前准备的,所以这解决了两个问题。
Just group them together using {...}
: 只需使用{...}
它们分组在一起:
{ cd "${directory}" && git checkout master && git pull origin master; } >>output.txt 2>>error.txt
cd ..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.