【问题标题】:Issue with all conditions (configuration profiles) being checked检查所有条件(配置文件)的问题
【发布时间】:2015-06-23 08:19:56
【问题描述】:

好的,现在我已经设置好了,但它仍然给了我一个 ;出口; 注销

[处理完成]

当通过终端运行时,我知道有一些配置文件不在我的计算机上,它们应该使脚本继续运行,或者更确切地说是保持循环。这是怎么回事?

提前谢谢....!

这是我目前所拥有的:

#!/bin/bash

profilesInstalled=`profiles -P|awk '/attribute/ {print $4}'`

while read line ; do

if [ "$line" = "F2CC78D2-A63F-45CB-AE7D-BF2221D41218" ];then

echo "AD Binding is present"

elif [ "$line" = "1C94DAD1-5FC7-46CE-9E09-576841C15093" ];then

echo "Energy Saver is present"

elif [ "$line" = "A0E5B977-F0AF-44C9-8001-DA0511B702B8" ];then

echo "Finder is present"

elif [ "$line" = "5E9DE5BF-34E4-4A7F-AA29-461FB0631943" ];then

echo "FV2 Redirect is present"

elif [ "$line" = "9AE91C88-D1B2-4227-9E95-80F492DCAA11" ];then

echo "Login Window/Security and Privacy is present"

elif [ "$line" = "00000000-0000-0000-A000-4A414D460003" ];then

echo "MDM Profile is present"

elif [ "$line" = "5E85BBF0-3483-4C80-A1FC-70AF20F82E7C" ];then

echo "Restrictions is present"

elif [ "$line" = "E433D546-5502-4C3F-9E5F-4732ED1F0032" ];then

echo "SAC SUBCA-01 is present"

elif [ "$line" = "5C2AE16B-D4E9-4D15-B190-3CD7B28779E8" ];then

echo "SAC SUBCA-02 is present"

elif [ "$line" = "2C620A13-DF1E-4F6A-A32B-9FA3149F8A56" ];then

echo "SAC-CA-01 is present"

elif [ "$line" = "3B44AE14-E0CE-4621-BACF-1A9C3BA4A459" ];then

echo "Screensaver is present" 

elif [ "$line" = "396A9D84-A9CA-4575-8D09-C9F054B76AF7" ];then

echo "Spotlight is present"

elif [ "$line" = "E0138F02-9A15-47BD-8CA5-7D1D0985A1A6" ];then

echo "Workday Corp is present"
fi 

exit 0

done <<<"$profilesInstalled"

【问题讨论】:

  • if [ "$line" = "3B44AE14-E0CE-4621-BACF-1A9C3BA4A459" ];then 中的 if 必须是 elif - 另请注意,您的 while 循环在第一次迭代后无条件退出,并且它隐式地从 stdin读取> - 你可能想要done &lt;&lt;&lt;"$profilesInstalled"

标签: bash loops profiles


【解决方案1】:

在这些测试中,您需要在 = 周围留一个空格。第一个测试将始终按书面通过。

您还应该引用"$line" 变量扩展。

除非您在其他地方使用$profilesInstalled,否则您根本不需要该变量,只需将profiles 管道直接传递到while 循环即可。

您还可以将该管道中的grep 替换为awk '/attribute/ {print $4}'

【讨论】:

  • 谢谢我已经添加了您的建议,并确保某些配置文件不存在,以确保脚本正在执行它应该执行的操作。不幸的是,它仍然无法正常工作,我收到语法错误:第 34 行:意外标记“完成”附近的语法错误
  • 这是目前为止的脚本。必须缩短它以适应 #!/bin/bash profilesInstalled=profiles -P|awk '/attribute/ {print $4}' while read line ; do if [ "$line" = "F2CC78D2-A63F-45CB-AE7D-BF2221D41218" ];then echo "AD Binding is present" elif [ "$line" = "1C94DAD1-5FC7-46CE-9E09-576841C15093" ];then echo "节能器存在" elif [ "$line" = "A0E5B977-F0AF-44C9-8001-DA0511B702B8" ];然后 echo "Finder 存在" elif [ "$line" = "5E9DE5BF-34E4-4A7F-AA29- 461FB0631943" ];然后 echo "FV2 Redirect is present" fi exit 0 done
  • @dondo 这样完全不可读。将新代码添加到原始帖子中(不要删除原始代码)并包含确切的错误。
  • @mklement0 在问题上添加评论可能是一个更好的主意。
【解决方案2】:

一些“元”注释首先:

不做这些事情:

  • 大大降低了您获得所需帮助的可能性。
  • 降低您的问题及其答案对未来读者的价值。

这是您的代码的清理版本:

# Helper function to determine a string's element index in an array.
# SYNOPSIS
#     indexOf needle "${haystack[@]}"
# *Via stdout*, returns the zero-based index of a string element in an array of strings or -1, if not found.
# The *return code* indicates if the element was found or not.
indexOf() {
  local e ndx=-1
  for e in "${@:2}"; do (( ++ndx )); [[ "$e" == "$1" ]] && echo $ndx && return 0; done
  echo '-1'; return 1
}

# Define array of profile IDs, and parallel ID of profile names.
# Note: in bash 4+, this could be handled more elegantly with a single
#       associative array.
profileIds=( F2CC78D2-A63F-45CB-AE7D-BF2221D41218 1C94DAD1-5FC7-46CE-9E09-576841C15093
             A0E5B977-F0AF-44C9-8001-DA0511B702B8 5E9DE5BF-34E4-4A7F-AA29-461FB0631943
             9AE91C88-D1B2-4227-9E95-80F492DCAA11 00000000-0000-0000-A000-4A414D460003
             5E85BBF0-3483-4C80-A1FC-70AF20F82E7C E433D546-5502-4C3F-9E5F-4732ED1F0032
             5C2AE16B-D4E9-4D15-B190-3CD7B28779E8 2C620A13-DF1E-4F6A-A32B-9FA3149F8A56
             3B44AE14-E0CE-4621-BACF-1A9C3BA4A459 396A9D84-A9CA-4575-8D09-C9F054B76AF7
             E0138F02-9A15-47BD-8CA5-7D1D0985A1A6 )
profileNames=( "AD Binding"                        "Energy Saver"
               "Finder"                            "FV2 Redirect"
               "Login Window/Security and Privacy" "MDM Profile"
               "Restrictions"                      "SAC SUBCA-01"
               "SAC SUBCA-02"                      "SAC-CA-01"
               "Screensaver"                       "Spotlight"
               "Workday Corp" )

# Feeding the list of installed profile IDs via a process
# substitution (<(...)), loop over them and print their
# respective names.
while read -r line ; do

  # Find the line in the array of profile IDs and
  # print the corresponding name.
  if ndx=$(indexOf "$line" "${profileIds[@]}"); then
    echo "${profileNames[ndx]} is present"
  else
    echo "WARNING: Unknown profile: $line" >&2
  fi

done < <(profiles -P | awk '/attribute/ {print $4}')

至于为什么你的代码没有循环:

  • 您的循环中有一个无条件exit 0 语句,这意味着循环总是在第一行之后退出
  • 由于使用&lt;&lt;&lt; 来提供配置文件列表,您始终会获得至少1 行输入,因为&lt;&lt;&lt; 在其输入后面附加了一个尾随换行符。如果输入为空,您将获得一个带有 empty 行的迭代。

关于这条消息:

; exit; 
logout

[Process completed]

它告诉我两件事:

  • 您在 OSX 上,并从 Finder 运行脚本。
  • 脚本没有输出(如果有输出,它会在exit;logout 行之间打印)。

当您从 Finder 运行脚本时,用于运行脚本的 shell 退出 在脚本运行后 - 其终端窗口是否保持打开状态取决于您的终端首选项 - 在您的情况下,窗口保持打开状态,但由于 shell 已退出,您无法再与它进行交互。

无论哪种方式,您的特定脚本都应该产生相同的输出,无论是直接从终端运行还是通过 Finder 运行。

【讨论】:

  • 哇,这太棒了。太感谢了!对于没有正确格式化和删除内容,我深表歉意。我是新来的。我一定会跟进如何提供 MCVE。我要研究一下你整理的内容,以便更好地理解它。再次感谢!
  • @dondo:我的荣幸;很高兴听见。另一个值得一提的提示:shellecheck.net 允许您对 shell 代码进行语法检查。
猜你喜欢
  • 1970-01-01
  • 2013-06-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-28
  • 1970-01-01
  • 2016-05-28
  • 1970-01-01
相关资源
最近更新 更多