gpt4 book ai didi

bash - 可以在 bash if 语句中使用调用 "test"的函数吗?

转载 作者:行者123 更新时间:2023-11-28 20:33:11 24 4
gpt4 key购买 nike

为了使我的脚本更具可读性,我想将 if-then-else-fi 语句的测试放在一个函数中,例如下面的示例:

#!/bin/bash

function isFileUnderVersionControl() {
test $( svn info "$1" 2>1 1>/dev/null; echo $? ) -eq 1
}


if isFileUnderVersionControl "$1"
then
echo "is under version control
else
echo "is not under version control"
fi

这在 bash 中可能吗?如果可以,如何正确地做到这一点???

最佳答案

不需要测试:

if svn info "$1" &>/dev/null ; then
echo Under version control
else
echo Not under vc
fi

或者,如果您需要该功能:

function isFileUnderVersionControl() {
svn info "$1" &>/dev/null
}
if isFileUnderVersionControl "$1" ; then ... fi

关于bash - 可以在 bash if 语句中使用调用 "test"的函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8358664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com