You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Jamf管理的macOS脚本中通过部门名称匹配获取部门编码

Solution for Bash 3.2 (macOS Big Sur+) Department Code Lookup

Since macOS's default bash (3.2) doesn't support associative arrays, here's a reliable, compatible approach to map department names to codes for your Jamf management scripts:

1. Define the Department Mapping

Use a here-document to store name-code pairs with a unique delimiter (we’ll use | here—pick one that won’t appear in your department names):

# Define department name ↔ code mappings
dept_mappings=$(cat <<EOF
Marketing|MKT
Engineering|ENG
Finance|FIN
Human Resources|HR
Customer Success|CS
EOF
)

2. Look Up the Code for a Selected Department

Assuming $departmentName holds the user-selected department name, use awk to directly match and extract the corresponding code. This handles spaces in department names and avoids regex-related issues:

# Example: departmentName is set via user input (e.g., Jamf policy prompt)
departmentName="Human Resources"

# Extract matching department code
deptCode=$(echo "$dept_mappings" | awk -F'|' -v target="$departmentName" '$1 == target {print $2}')

3. Handle Missing Matches

Add a validation check to prevent workflow failures if no matching code is found:

if [ -z "$deptCode" ]; then
    echo "Error: No matching department code found for '$departmentName'" >&2
    exit 1
fi

# Use deptCode in subsequent Jamf operations, e.g.:
# jamf recon -department "$departmentName" -attribute "DeptCode=$deptCode"

Why This Works

  • The here-document keeps mappings organized and easy to update.
  • awk is pre-installed on macOS and works seamlessly with bash 3.2.
  • Passing $departmentName to awk via -v ensures special characters in department names don’t break the match logic.

内容的提问来源于stack exchange,提问作者BenPhilware

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 21:32:44