简体   繁体   English

从git命令捕获输出?

[英]Capture output from git command?

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 cloned and 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="git@bitbucket.org: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

git clone does provide a exit code you can read with $? git clone确实提供了一个可以用$读取的退出代码? like follows: 如下:

git clone user@server:repo
echo $?

This will print 0 if everything worked just fine. 如果一切正常,这将打印0。 If for example the folder is not a git repository you will get the exit code 128. 例如,如果文件夹不是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

--quiet will suppress any output from git, as long as there are no errors. 只要没有错误, - --quiet就会抑制git的任何输出。 So if you just remove the else-branch you will get you positive output or the error produced by git. 因此,如果您只是删除else-branch,您将获得正输出或git产生的错误。

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>&1 Redirects stdout and stderr streams to git.log. git clone user@server:repo localrepo > git.log 2>&1将stdout和stderr流重定向到git.log。 > git.log redirects stdout to git.log 2>&1 redirects stderr to the same place as stdout(thus, git.log). > git.log将stdout重定向到git.log 2>&1将stderr重定向到与stdout相同的位置(因此,git.log)。

$? eq 0 $? eq 0 Checks the retcode of git which should be 0 if the clone was successful. $? eq 0检查git的重新编码,如果克隆成功则应为0。

cat git.log outputs the contents of the git.log file. cat git.log输出git.log文件的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM