在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.
awkis pre-installed on macOS and works seamlessly with bash 3.2.- Passing
$departmentNametoawkvia-vensures special characters in department names don’t break the match logic.
内容的提问来源于stack exchange,提问作者BenPhilware
相关产品推荐
相关产品推荐

