Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 安装检查脚本,用于检查配置文件-P中的特定配置文件_Macos_Bash_Terminal_Mdm_Profiles - Fatal编程技术网

Macos 安装检查脚本,用于检查配置文件-P中的特定配置文件

Macos 安装检查脚本,用于检查配置文件-P中的特定配置文件,macos,bash,terminal,mdm,profiles,Macos,Bash,Terminal,Mdm,Profiles,我正在尝试编写一个instal检查脚本,根据响应运行配置文件和退出 我们使用meraki的一个配置文件,如果安装了该配置文件,其输出看起来是这样的: _computerlevel[1] attribute: profileIdentifier: com.meraki.sm.mdm There are 1 configuration profiles installed 有没有一种方法可以检查出这个确切的响应 我的想法是: #!/bin/bash output=profiles -P if [

我正在尝试编写一个instal检查脚本,根据响应运行配置文件和退出

我们使用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;
有什么想法吗?

试试以下方法:

#!/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$'通过配置文件的输出进行搜索: -q quiet选项不产生输出,只是通过其退出代码指示是否找到匹配项0或1。 “:com.meraki.sm.mdm$”是一个正则表达式,它与第$行末尾的字符串“com.meraki.sm.mdm”匹配,前面加“:”。 egrepis与GREP-E相同——它激活了对扩展正则表达式的支持——这里不是严格必要的,但通常建议减少意外。 如果管道返回退出代码0,if语句的计算结果为true,否则为false。请注意,默认情况下,它是管道中的最后一个命令,其退出代码决定管道的整体退出代码。 顺便说一句,如果您只想让脚本反映是否找到了字符串,也就是说,如果您不需要在脚本中采取进一步的操作,那么以下内容就足够了:

sudo profiles -P | egrep -q ': com.meraki.sm.mdm$'
exit $?   # Special variable `$?` contains the most recent command's exit code
如果您只想在发生故障时立即退出脚本:

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


非常感谢你。这将完美地发挥我们的作用。我将不得不交换退出代码的目的,我使用它,但它是完美的。谢谢现在要解决卷曲问题>:D@WardsParadox:很高兴听到它为你工作;我很乐意。很快,但是如果在安装程序中使用它,它还需要sudo吗?由于安装程序提示我构建的所有脚本都需要根权限的管理权限,而不需要sudo,对吗?@WardsParadox:是的,如果脚本确实以根用户身份运行,则不需要sudo-但如果保留sudo前缀,它也可以工作。如果要显式检查脚本中的管理员root权限,请使用[$id-u-eq 0]| |{echo'此脚本必须以root身份运行。>&2;退出1;}
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