Capture output from git command?

前端 未结 2 1180
南笙
南笙 2021-02-08 18:24

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 s

2条回答
  •  耶瑟儿~
    2021-02-08 18:59

    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.

提交回复
热议问题