测试以确定git clone命令是否成功
我试图通过传递用户名和密码来克隆git存储库。这是成功的 但我的目的是想知道git clone命令是否执行。 如果不是,我希望在shell脚本本身中处理此类错误 我的工作shell脚本:测试以确定git clone命令是否成功,git,bash,bitbucket,Git,Bash,Bitbucket,我试图通过传递用户名和密码来克隆git存储库。这是成功的 但我的目的是想知道git clone命令是否执行。 如果不是,我希望在shell脚本本身中处理此类错误 我的工作shell脚本: cd .. git clone https://username:password@bitbucket.org/username/repositoryname.git cd repositoryname git checkout branchname1 cd .. mv repositoryname newfo
cd ..
git clone https://username:password@bitbucket.org/username/repositoryname.git
cd repositoryname
git checkout branchname1
cd ..
mv repositoryname newfoldername
git clone https://username:password@bitbucket.org/username/respositoryname.git
cd repositoryname
git checkout branchname2
cd ..
mv repositoryname newfoldername
如何在脚本中测试这些步骤是否成功?返回值存储在$?。0表示成功,其他表示错误
some_command
if [ $? -eq 0 ]; then
echo OK
else
echo FAIL
fi
我还没有用git尝试过,但我希望它能起作用。这个应该能起作用(只需将您的脚本放在下面标记“---您的脚本在这里----”的位置即可):
例子
请参阅。感谢罗宾为我的问题提供了如此好的解决方案。现在,它对我的脚本来说运行良好。
#!/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
(
set -e
--- your script here ---
)
if some_command
then
echo "Successful"
fi
if ! git clone http://example.com/repo.git
then
echo "Failed"
else
echo "Successful"
fi