bash 从 git 命令捕获输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13715261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:55:51  来源:igfitidea点击:

Capture output from git command?

bashcaptureoutput

提问by Hailwood

I am writing a script to automate setting up new projects for me.

我正在编写一个脚本来自动为我设置新项目。

this includes pulling down a github repository.

这包括拉下 github 存储库。

What I want to do is have some output from my script, then call git clone $repo

我想要做的是从我的脚本中获得一些输出,然后调用 git clone $repo

I want to show the output from that command while it is running, but then when it has run if it has run successfully replace it's output (note just the git commands output, I still want the output from before that to be there) with repository successfully clonedand if failed just leave the output there, and print repository cloning failed.

我想在该命令运行时显示该命令的输出,但是当它运行时,如果它已成功运行,则替换它的输出(仅注意 git 命令输出,我仍然希望之前的输出在那里)repository successfully cloned和如果失败,只需将输出留在那里,然后打印repository cloning failed

How can I do this?

我怎样才能做到这一点?

Below is my current (rather simple) script.

下面是我当前的(相当简单的)脚本。

#! /bin/bash

# -p project name

templateurl="[email protected]:xxx/xxx-site-template.git"

while getopts ":p:" opt; do #eventually I'll add more options here

case $opt in
  p)
    project=$OPTARG
    ;;
  \?)
    echo "Invalid option: -$OPTARG" >&2
    exit 1
    ;;
  :)
    echo "Option -$OPTARG requires an argument." >&2
    exit 1
    ;;
esac
done

if [ -z "$project" ]; then
    echo "Project name required"
    exit 1
fi

clear
echo "|==========================|"
echo "| New xxx Project Creator  |"
echo "|==========================|"
echo "Project: $project"

if [ -d "$project" ]; then
    echo "Directory $project already exists!"
    exit 1
fi


mkdir $project
if [ ! -d "$project" ]; then
    echo "Failed to create project directory!"
    exit 1
fi

echo "Cloning xxx Template repository"
git clone $templateurl $project

回答by sge

git clonedoes provide a exit code you can read with $? like follows:

git clone是否提供了可以用 $ 读取的退出代码?如下:

git clone user@server:repo
echo $?

This will print 0 if everything worked just fine. If for example the folder is not a git repository you will get the exit code 128.

如果一切正常,这将打印 0。例如,如果文件夹不是 git 存储库,您将获得退出代码 128。

you can check if the clone worked as follows:

您可以检查克隆是否按如下方式工作:

git clone user@server:repo localrepo --quiet
success=$?
if [[ $success -eq 0 ]];
then
    echo "Repository successfully cloned."
else
    echo "Something went wrong!"
fi

--quietwill suppress any output from git, as long as there are no errors. So if you just remove the else-branch you will get you positive output or the error produced by git.

--quiet只要没有错误,就会抑制 git 的任何输出。因此,如果您只是删除 else 分支,您将获得正输出或 git 产生的错误。

回答by antik

git clone user@server:repo localrepo > git.log 2>&1
if [[ $? eq 0 ]];
then
   echo Repository successfully cloned.
else
   cat git.log
   echo Repository cloning failed.
fi

rm git.log

Explanation:

解释:

git clone user@server:repo localrepo > git.log 2>&1Redirects stdout and stderr streams to git.log. > git.logredirects stdout to git.log 2>&1redirects stderr to the same place as stdout(thus, git.log).

git clone user@server:repo localrepo > git.log 2>&1将 stdout 和 stderr 流重定向到 git.log。 > git.log将 stdout 重定向到 git.log 2>&1将 stderr 重定向到与 stdout 相同的位置(因此,git.log)。

$? eq 0Checks the retcode of git which should be 0 if the clone was successful.

$? eq 0如果克隆成功,检查 git 的 retcode 应该是 0。

cat git.logoutputs the contents of the git.log file.

cat git.log输出 git.log 文件的内容。