在Applescript中有效,但在Shell中无效



我在Applescript中写了一些东西,我想将其更改为shell脚本。申请说明如下:

set computerlevel to do shell script "(profiles -P )" with administrator privileges
if computerlevel contains "F2CC78D2-A63F-45CB-AE7D-BF2221D41218" then
	set theAnswer to "Active Directory Bind Present"
else
	set theAnswer to "Active Directory Bind Not Present"
end if

它工作得很好,但我想写shell脚本版本。这就是我到目前为止所想到的。

#!/bin/sh
configprofiles='profiles -P'
if $configprofiles == "F2CC78D2-A63F-45CB-AE7D-BF2221D41218"; then
  echo "<result>Active Directory Bind Present.</result>"
	else
		echo "<result>Active Directory Bind Not Present.</result>"
	fi

我以为它是有效的,但它真的是一个假阳性。我认为它只是在寻找导致误报的任何字符,而不是查看F2CC78D2-A63F-45CB-AE7D-BF2221D41218的整个字符串是否存在。有人知道出了什么问题吗?提前谢谢。

只有几个语法方面的东西。。。最重要的是在测试中添加[...]。(您也可以在与test进行比较之前)

#!/bin/sh
if profiles -P  | grep attribute | awk '{print $4}' | grep -q "F2CC78D2-A63F-45CB-AE7D-BF2221D41218"
then
    echo "<result>Active Directory Bind Present.</result>"
else
    echo "<result>Active Directory Bind Not Present.</result>"
fi
#!/bin/sh
if profiles -P  | grep attribute | awk '{print $4}' | grep -q "F2CC78D2-A63F-45CB-AE7D-BF2221D41218"
then
    echo "<result>Active Directory Bind Present.</result>"
else
    echo "<result>Active Directory Bind Not Present.</result>"
fi

您的脚本几乎完成了,但比较是准确的。要像在AppleScript中那样创建"包含"比较,您可以将星号包裹在字符串周围。我发现的另一件事是,该变量包含一个字符串,并且没有执行代码。要执行命令并将其输出设置为变量,应使用$(command)。最后但同样重要的是,其他答案使用了所有额外的命令和管道,而shell本身可以很好地处理这一问题,不需要如此复杂的方法。所以你的脚本看起来像:

#!/bin/sh
configprofiles=$(profiles -P)
if [[ $configprofiles == *"F2CC78D2-A63F-45CB-AE7D-BF2221D41218"* ]]
then
    echo "<result>Active Directory Bind Present.</result>"
else
    echo "<result>Active Directory Bind Not Present.</result>"
fi

最新更新