11

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
Hailwood
  • 89,623
  • 107
  • 270
  • 423

2 Answers2

17

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.

sge
  • 7,330
  • 1
  • 15
  • 18
  • I have changed the bottom part of the script to be `git clone $templateurl $project --quiet success=$? if [[ success -eq 0 ]]; then echo "Repository successfully cloned." fi` But I am getting the error `[[: not found` on the line with the if statement. also, I think my question is slightly missunderstood, I still want to display the normal output as the command is running so the user gets some feedback on what is going on. but I want to clear away that output once the command has finished. – Hailwood Dec 05 '12 at 03:09
  • There is a $ missing in the if statement: `[[ $success ...;` If you want to delete already printed output you will have to deal with carriage return (\r) and then overwrite the old output. I assume that deleting already printed output is not possible. – sge Dec 05 '12 at 03:21
  • Another possibility would be using ANSI-Escape-Codes http://ascii-table.com/ansi-escape-sequences.php. eg `echo -e "\033[2K"` removes the active line. A combination of setting the cursor position and remove lines could lead to success. – sge Dec 05 '12 at 03:28
  • Mmm, I think I might just store what output I want to keep in a variable, and just keep using a combination of `clear; echo $progress` – Hailwood Dec 05 '12 at 03:46
5
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.log redirects stdout to git.log 2>&1 redirects stderr to the same place as stdout(thus, git.log).

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

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

antik
  • 5,282
  • 1
  • 34
  • 47
  • `GITOUTPUT=\`git clone user@server:repo localrepo\`` will, unfortunately, not suppress the output of git. Even `git clone ... > /dev/null` does not suppress any output. – sge Dec 05 '12 at 02:47
  • 1
    It must be writing to stderr then. I'll post a different answer. – antik Dec 05 '12 at 12:18
  • 1
    git checkout also goes to stderr (but git pull goes to stdout?). This saved me! – rob5408 Feb 19 '14 at 05:22