bash 测试以确定 git clone 命令是否成功

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26771612/
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 11:43:56  来源:igfitidea点击:

Test to determine if git clone command succeeded

gitbashbitbucket

提问by bhadram

I tried to clone the git repository by passing the username, password. That was successful.

我试图通过传递用户名和密码来克隆 git 存储库。那是成功的。

But what my intention is that I want to know whether the git clone command executed or not. If not, I would like to handle such kind of errors in shell script itself.

但我的意图是我想知道 git clone 命令是否执行。如果没有,我想在 shell 脚本本身中处理此类错误。

My working shell script:

我的工作 shell 脚本:

cd ..
git clone https://username:[email protected]/username/repositoryname.git
cd repositoryname
git checkout branchname1
cd ..
mv repositoryname newfoldername
git clone https://username:[email protected]/username/respositoryname.git
cd repositoryname
git checkout branchname2
cd ..
mv repositoryname newfoldername

How do I test, in the script, whether these steps were successful?

如何在脚本中测试这些步骤是否成功?

回答by Ajay

The return value is stored in $?. 0 indicates success, others indicates error.

返回值存储在 $? 中。0 表示成功,其他表示错误。

some_command
if [ $? -eq 0 ]; then
    echo OK
else
    echo FAIL
fi

I haven't tried it with git, but I hope this works.

我还没有用 git 试过,但我希望这有效。

回答by timofey.com

if some_command
then
  echo "Successful"
fi

Example

例子

if ! git clone http://example.com/repo.git
then
  echo "Failed"
else
  echo "Successful"
fi

See How to detect if a git clone failed in a bash script.

请参阅如何检测 git clone 在 bash 脚本中是否失败

回答by Robin Hsu

This one should work (Just put your script at the place marked "--- your script here ---" below):

这个应该可以工作(只需将您的脚本放在下面标记为“---您的脚本在这里---”的地方):

#!/bin/bash

# call your script with set -e to stop on the first error
bash <<EOF
set -e
--- your script here ---
EOF

# test status: I don't want this part to stop on the first error,
# and that's why use the HERE document above to wrap a sub-shell for "set -e"
if [ $? -eq 0 ]; then
  echo success
else
  echo fail
fi

Alternatively, HERE document can be replaced by:

或者,HERE 文档可以替换为:

(
  set -e
  --- your script here ---
)