【问题标题】:Install Check Script that checks Profiles -P for a specific profile安装检查脚本以检查特定配置文件的配置文件 -P
【发布时间】:2014-07-10 17:57:28
【问题描述】:

我正在尝试编写一个运行profiles -P并根据响应退出的安装检查脚本。

我们使用来自 meraki 的配置文件,其输出如下所示(如果已安装):

_computerlevel[1] attribute: profileIdentifier: com.meraki.sm.mdm
There are 1 configuration profiles installed

有没有办法检查这个确切的响应?

我在想这样的事情:

#!/bin/bash
output=profiles -P
if [ output = com.meraki.sm.mdm ]; then
exit 0;
else
exit 1;

有什么想法吗?

【问题讨论】:

    标签: macos bash terminal mdm profiles


    【解决方案1】:

    尝试以下方法:

    #!/bin/bash
    
    if sudo profiles -P | egrep -q ': com.meraki.sm.mdm$'; then
      exit 0
    else
      exit 1
    fi
    
    • 来自sudo profiles -P 的输出(注意profiles 始终需要root 权限)通过管道(|)发送到egrep;这两个命令形成一个管道
    • egrep -q ': com.meraki.sm.mdm$' 搜索 profile 的输出:
      • -q (quiet) 选项不产生任何输出,只是通过其退出代码指示是否找到匹配项 (0) 或未找到匹配项 (1)。
      • ': com.meraki.sm.mdm$' 是一个正则表达式,匹配位于行尾 ($) 的字符串 'com.meraki.sm.mdm',前面是 ':'。
      • (egrepis the same asgrep -E` - 它激活对扩展正则表达式的支持 - 这里不是绝对必要的,但通常建议减少意外)。
    • if 语句如果管道返回退出代码 0 评估为 true,否则评估为 false(非零)。请注意,默认情况下,它是管道中的 last 命令,其退出代码决定了管道的整体退出代码。

    顺便说一句,如果您只想让脚本反映是否找到了字符串(即,如果您不需要在脚本中采取进一步的操作),则以下内容就足够了:

    sudo profiles -P | egrep -q ': com.meraki.sm.mdm$'
    exit $?   # Special variable `$?` contains the most recent command's exit code
    

    如果您只想在发生失败的情况下立即退出脚本:

    sudo profiles -P | egrep -q ': com.meraki.sm.mdm$' || exit
    
    # Alternative, with error message:
    sudo profiles -P | egrep -q ': com.meraki.sm.mdm$' ||
      { ec=$?; echo 'Profile not installed.' >&2; exit $ec; }
    

    相反,如果您想在成功之后立即退出:

    sudo profiles -P | egrep -q ': com.meraki.sm.mdm$' && exit
    

    【讨论】:

    • 非常感谢。这将完美地工作。我将不得不为我使用它的目的交换退出代码,但它是完美的。谢谢!现在解决卷曲问题>:D
    • @WardsParadox:很高兴听到它对你有用;我的荣幸。
    • 只是一个快速的,但如果它被用于安装程序的情况下,它仍然需要sudo吗?由于安装程序提示(以及我构建的所有)需要root权限的管理权限,所以它不需要sudo,对吗?
    • @WardsParadox:是的,如果脚本真正以 root 身份运行,则不需要 sudo - 但如果您保留 sudo 前缀,它也可以工作。如果您想要显式检查脚本中的管理员(root)权限,使用[[ $(id -u) -eq 0 ]] || { echo 'This script must be run as root.' >&2; exit 1; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 2020-08-08
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    相关资源
    最近更新 更多