捕获的git命令的输出? [英] Capture output from git command?

查看:445
本文介绍了捕获的git命令的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个脚本来自动为我设立新的项目。

这包括拉下来GitHub的信息库。

我想要做的就是从我的脚本一些输出,然后调用 git的克隆$回购

我要显示从该命令的输出在运行时,但它已经运行,那么当如果它已成功运行取代它的输出(注意只是Git命令的输出,我还是想从之前的输出在那里)与库成功克隆,如果失败了刚刚离开的输出出现,并打印库的克隆失败。

我怎样才能做到这一点?

下面是我目前的(很简单)脚本。

 #! /斌/庆典#-p项目名称templateurl =git@bitbucket.org:XXX / XXX-站点template.git而getopts的:P:选择;做#eventually我会在这里添加更多选项案例$选择在
  p)
    项目= $ OPTARG
    ;;
  \\?)
    回声无效选项: - $ OPTARG>和2
    1号出口
    ;;
  :)
    回声选项 - $ OPTARG需要一个说法。 >和2
    1号出口
    ;;
ESAC
DONE如果[-z$项目];然后
    回声项目名称所需
    1号出口
科幻明确
回声| ========================== |
回声|新建XXX项目的创作者|
回声| ========================== |
回声项目:$工程如果[-d$项目];然后
    回声目录$项目已经存在!
    1号出口
科幻
MKDIR $项目
如果[! -d$项目];然后
    回声无法创建项目目录!
    1号出口
科幻回声克隆XXX模板库
git的克隆$ templateurl $项目


解决方案

git的克隆并提供了一​​个退出code你可以用$看?像如下:

  git的克隆用户名@服务器:回购
回声$?

如果一切工作就好这将打印0。例如,如果该文件夹不是一个Git仓库,你会得到出境code 128。

您可以检查是否曾克隆如下:

  git的克隆用户名@服务器:回购localrepo --quiet
成功= $?
如果[[$成功-eq 0];
然后
    回声库成功克隆。
其他
    回声出事了!
科幻

- 安静将坐席preSS从git的任何输出,只要没有错误。所以,如果你只是删除了其他分支你会得到你正输出或混帐产生的误差。

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

this includes pulling down a github repository.

What I want to do is have some output from my script, then call 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.

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 $? like follows:

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.

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.

这篇关于捕获的git命令的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆