Mac OS X 实用脚本指南
1. 列出 NetInfo 用户
1.1 代码实现
#!/bin/sh
# listmacusers - Simple script to list users in the Mac OS X NetInfo database.
# Note that Mac OS X also has an /etc/passwd file, but that's
# used only during the initial stages of boot time and for
# recovery bootups. Otherwise, all data is in the NetInfo db.
fields=""
while getopts "Aahnprsu" opt ; do
case $opt in
A ) fields="uid passwd name realname home shell" ;;
a ) fields="uid name realname home shell" ;;
h ) fields="$fields home" ;;
n ) fields="$fields name" ;;
p ) fields="$fields passwd" ;;
r ) fields="$fields realname" ;;
s ) fields="$fields shell" ;;
u ) fields="$fields uid" ;;
? ) cat << EOF >&2
Usage: $0 [A|a|hnprsu]
Where:
-A output all known NetInfo user fields
-a output only the interesting user fields
-h show home directories of accounts
-n show account names
-p passwd (encrypted)
-r show realname/fullname values
-s show login shell
-u uid
EOF
exit 1
esac
done
exec nireport . /users ${fields:=uid name realname home shell}
1.2 工作原理
该脚本主要用于构建
fields
变量,
nireport
工具允许指定想要查看的字段名。例如,用户指定
-a
时,
nireport
会得到
fields="uid name realname home shell"
。
1.3 运行脚本
该脚本接受多种命令参数,可通过
hnprsu
指定确切字段和字段顺序,也可使用
-a
列出除加密密码字段外的所有字段,使用
-A
列出所有字段。无参数时,默认显示所有有趣的用户字段(
-a
)。
1.4 运行结果
$ listmacusers -u -n -r -s
-2 nobody Unprivileged User /dev/null
0 root System Administrator /bin/tcsh
1 daemon System Services /dev/null
99 unknown Unknown User /dev/null
25 smmsp Sendmail User /dev/null
70 www World Wide Web Server /dev/null
74 mysql MySQL Server /dev/null
75 sshd sshd Privilege separation /dev/null
505 test3 Mr. Test Three /bin/tcsh
501 taylor Dave Taylor /bin/bash
502 badguy Test Account /bin/tcsh
503 test /bin/tcsh
506 tintin Tintin, Boy Reporter /bin/tcsh
507 gary Gary Gary /bin/bash
若只想查看登录账户,可过滤掉
/dev/null
外壳:
$ listmacusers -u -n -r -s | grep -v /dev/null
0 root System Administrator /bin/tcsh
505 test3 Mr. Test Three /bin/tcsh
501 taylor Dave Taylor /bin/bash
502 badguy Test Account /bin/tcsh
503 test /bin/tcsh
506 tintin Tintin, Boy Reporter /bin/tcsh
507 gary Gary Gary /bin/bash
若要修改 NetInfo 条目,可使用苹果提供的 NetInfo Manager 应用,可在
Applications/Utilities
中找到,或使用命令
open -a "NetInfo Manager"
启动。
1.5 流程图
graph TD;
A[开始] --> B{是否有参数};
B -- 是 --> C{参数类型};
C -- -A --> D[输出所有已知 NetInfo 用户字段];
C -- -a --> E[输出有趣的用户字段];
C -- hnprsu --> F[按指定字段和顺序输出];
B -- 否 --> E;
D --> G[结束];
E --> G;
F --> G;
2. 在 Mac OS X 系统中添加用户
2.1 代码实现
#!/bin/sh
# addmacuser - Adds a new user to the system, including building the
# home directory, copying in default config data, etc.
# You can choose to have every user in his or her own group (which requires
# a few tweaks) or use the default behavior of having everyone put
# into the same group. Tweak dgroup and dgid to match your own config.
dgroup="guest"; dgid=31 # default group and groupid
hmdir="/Users"
shell="uninitialized"
if [ "$(/usr/bin/whoami)" != "root" ] ; then
echo "$(basename $0): You must be root to run this command." >&2
exit 1
fi
echo "Add new user account to $(hostname)"
echo -n "login: " ; read login
if nireport . /users name | sed 's/[^[:alnum:]]//g' | grep "^$login$" ; then
echo "$0: You already have an account with name $login" >&2
exit 1
fi
uid1="$(nireport . /users uid | sort -n | tail -1)"
uid="$(( $uid1 + 1 ))"
homedir=$hmdir/$login
echo -n "full name: " ; read fullname
until [ -z "$shell" -o -x "$shell" ] ; do
echo -n "shell: " ; read shell
done
echo "Setting up account $login for $fullname..."
echo "uid=$uid gid=$dgid shell=$shell home=$homedir"
niutil -create . /users/$login
niutil -createprop . /users/$login passwd
niutil -createprop . /users/$login uid $uid
niutil -createprop . /users/$login gid $dgid
niutil -createprop . /users/$login realname "$fullname"
niutil -createprop . /users/$login shell $shell
niutil -createprop . /users/$login home $homedir
niutil -createprop . /users/$login _shadow_passwd ""
# adding them to the $dgroup group
niutil -appendprop . /groups/$dgroup users $login
if ! mkdir -m 755 $homedir ; then
echo "$0: Failed making home directory $homedir" >&2
echo "(created account in NetInfo database, though. Continue by hand)" >&2
exit 1
fi
if [ -d /etc/skel ] ; then
ditto /etc/skel/.[a-zA-Z]* $homedir
else
ditto "/System/Library/User Template/English.lproj" $homedir
fi
chown -R ${login}:$dgroup $homedir
echo "Please enter an initial password for $login:"
passwd $login
echo "Done. Account set up and ready to use."
exit 0
2.2 工作原理
- 检查权限 :脚本会检查是否以 root 用户身份运行,非 root 用户运行会产生权限错误。
-
检查账户名
:使用
nireport生成账户名列表,通过sed去除空格和制表符,再用grep搜索指定登录名。 -
生成新用户 ID
:使用
nireport提取 NetInfo 数据库中最高的用户 ID 值,加 1 得到新账户 ID。 -
创建账户
:使用
niutil命令在 NetInfo 数据库中创建账户及相关属性。 -
复制文件
:使用
ditto命令复制用户文件和目录,确保特殊资源分支的文件完整复制。 -
设置密码
:使用
passwd命令为指定账户设置密码。
2.3 运行脚本
该脚本会提示输入信息,无需命令标志或命令行参数。
2.4 运行结果
$ addmacuser
addmacuser: You must be root to run this command.
使用
sudo
命令以 root 身份运行:
$ sudo addmacuser
Add new user account to TheBox.local.
login: gareth
full name: Gareth Taylor
shell: /bin/bash
Setting up account gareth for Gareth Taylor...
uid=508 gid=31 shell=/bin/bash home=/Users/gareth
Please enter an initial password for gareth:
Changing password for gareth.
New password:
Retype new password:
Done. Account set up and ready to use.
2.5 可能的改进
-
更改组模型
:可添加代码自动生成比当前 NetInfo 数据库中最大组 ID 大 1 的组 ID,并使用
niutil命令创建新组。 - 发送欢迎邮件 :自动向新用户发送欢迎邮件,包含系统使用说明、默认打印机信息等。
3. 添加邮件别名
3.1 代码实现
#!/bin/sh
# addmacalias - Adds a new alias to the email alias database on Mac OS X.
# This presumes that you've enabled sendmail, which can be kind of
# tricky. Go to http://www.macdevcenter.com/ and search for "sendmail"
# for some good reference works.
showaliases="nidump aliases ."
if [ "$(/usr/bin/whoami)" != "root" ] ; then
echo "$(basename $0): You must be root to run this command." >&2
exit 1
fi
if [ $# -eq 0 ] ; then
echo -n "Alias to create: "
read alias
else
alias=$1
fi
# Now let's check to see if that alias already exists...
if $showaliases | grep "${alias}:" >/dev/null 2>&1 ; then
echo "$0: mail alias $alias already exists" >&2
exit 1
fi
# Looks good. let's get the RHS and inject it into NetInfo
echo -n "pointing to: "
read rhs # the right-hand side of the alias
niutil -create . /aliases/$alias
niutil -createprop . /aliases/$alias name $alias
niutil -createprop . /aliases/$alias members "$rhs"
echo "Alias $alias created without incident."
exit 0
3.2 工作原理
- 检查权限 :确保以 root 用户身份运行。
-
检查别名是否存在
:使用
nidump命令列出所有别名,通过grep检查指定别名是否已存在。 -
创建别名
:使用
niutil命令在 NetInfo 系统中创建别名及相关属性。
3.3 运行脚本
该脚本较为灵活,可在命令行指定要创建的别名,若未指定会提示输入。
3.4 运行结果
$ sudo addmacalias
Alias to create: gareth
pointing to: gareth@hotmail.com
Alias gareth created without incident.
3.5 可能的改进
可添加
-l
标志,用于列出所有当前邮件别名,提高脚本实用性。
4. 动态设置终端标题
4.1 代码实现
#! /bin/sh
# titleterm - Tells the Mac OS X Terminal application to change its title
# to the value specified as an argument to this succinct script.
if [ $# -eq 0 ]; then
echo "Usage: $0 title" >&2
exit 1
else
echo -ne "\033]0;$1\007"
fi
exit 0
4.2 工作原理
终端应用程序理解特定的转义序列,
titleterm
脚本发送
ESC ] 0; title BEL
序列,将终端窗口标题更改为指定值。
4.3 运行脚本
在命令行输入想要的新标题即可更改终端窗口标题。
4.4 运行结果
$ titleterm $(pwd)
命令无明显输出,但会立即将终端窗口标题更改为当前工作目录。
4.5 可能的改进
在
.cshrc
或
.bashrc
中添加以下命令,可自动使终端窗口标题始终显示当前工作目录:
-
tcsh
:
alias precmd 'titleterm "$PWD"'
-
bash
:
export PROMPT_COMMAND="titleterm \"\$PWD\""
5. 生成 iTunes 音乐库摘要列表
5.1 代码实现
#!/bin/sh
# itunelist - Lists your iTunes library in a succinct and attractive
# manner, suitable for sharing with others, or for
# synchronizing (with diff) iTune libraries on different
# computers and laptops.
itunehome="$HOME/Music/iTunes"
ituneconfig="$itunehome/iTunes Music Library.xml"
musiclib="/$(grep '>Music Folder<' "$ituneconfig" | cut -d/ -f5- | \
cut -d\< -f1 | sed 's/%20/ /g')"
echo "Your music library is at $musiclib"
if [ ! -d "$musiclib" ] ; then
echo "$0: Confused: Music library $musiclib isn't a directory?" >&2
exit 1
fi
exec find "$musiclib" -type d -mindepth 2 -maxdepth 2 \! -name '.*' -print |
sed "s|$musiclib/||"
5.2 工作原理
-
确定音乐库位置
:从 iTunes 配置文件中提取
Music Folder字段值,处理 URL 格式,得到音乐库的实际路径。 -
生成列表
:使用
find命令查找指定目录下的音乐文件夹,使用sed命令去除路径前缀,生成简洁的音乐列表。
5.3 运行脚本
该脚本无需命令参数或标志。
5.4 运行结果
$ itunelist | head
Your music library is at /Volumes/110GB/iTunes Library/
Acoustic Alchemy/Blue Chip
Acoustic Alchemy/Red Dust & Spanish Lace
Acoustic Alchemy/Reference Point
Adrian Legg/Mrs. Crowe's Blue Waltz
Al Jarreau/Heaven And Earth
Alan Parsons Project/Best Of The Alan Parsons Project
Alan Parsons Project/Eve
Alan Parsons Project/Eye In The Sky
Alan Parsons Project/I Robot
5.5 可能的改进
可尝试使用可通过网络访问的 iTunes 目录的 URL 作为 XML 文件中的
Music Folder
值。
总结
本文介绍了多个 Mac OS X 实用脚本,包括列出 NetInfo 用户、添加用户、添加邮件别名、动态设置终端标题和生成 iTunes 音乐库摘要列表。这些脚本可帮助用户更方便地管理系统和处理日常任务。通过理解脚本的工作原理和运行方法,用户可以根据自身需求进行定制和改进。
脚本总结表格
| 脚本名称 | 功能 | 主要命令 | 可能的改进 |
|---|---|---|---|
| listmacusers | 列出 Mac OS X NetInfo 数据库中的用户 | nireport | 无 |
| addmacuser | 在 Mac OS X 系统中添加新用户 | niutil, ditto, passwd | 更改组模型,发送欢迎邮件 |
| addmacalias | 在 Mac OS X 系统中添加邮件别名 | niutil | 添加 -l 标志列出所有别名 |
| titleterm | 动态设置 Mac OS X 终端窗口标题 | 转义序列 | 在 .cshrc 或 .bashrc 中自动显示当前工作目录 |
| itunelist | 生成 iTunes 音乐库摘要列表 | grep, cut, sed, find | 使用网络访问的 iTunes 目录 URL |
6. 各脚本的使用建议与注意事项
6.1 listmacusers 脚本
-
使用建议
-
若想快速查看常见用户信息,可直接运行
listmacusers命令,它会默认显示有趣的用户字段。 -
当需要查看特定字段时,使用
-hnprsu等参数组合,如-u -n -r -s查看用户 ID、登录名、真实姓名和登录 shell。
-
若想快速查看常见用户信息,可直接运行
-
注意事项
-
该脚本依赖
nireport工具,确保系统中已正确安装该工具。 - 若要修改 NetInfo 条目,使用 NetInfo Manager 应用时需谨慎操作,避免误删或修改重要信息。
-
该脚本依赖
6.2 addmacuser 脚本
-
使用建议
-
运行该脚本前,确保对用户组配置有清晰的规划,可根据需求修改
dgroup和dgid。 - 输入用户信息时,确保登录名唯一,避免与现有账户冲突。
-
运行该脚本前,确保对用户组配置有清晰的规划,可根据需求修改
-
注意事项
-
必须以 root 身份运行该脚本,可使用
sudo命令。 -
创建用户时,若
mkdir命令创建主目录失败,虽已在 NetInfo 数据库中创建账户,但后续需手动处理。
-
必须以 root 身份运行该脚本,可使用
6.3 addmacalias 脚本
-
使用建议
- 若要创建邮件别名,可在命令行直接指定别名,也可在脚本提示时输入。
-
若需要查看当前所有邮件别名,可考虑添加
-l标志的改进。
-
注意事项
-
该脚本假设已启用
sendmail服务,使用前需确保该服务已正确配置。 -
检查别名是否存在时,使用
nidump命令,确保该命令能正常执行。
-
该脚本假设已启用
6.4 titleterm 脚本
-
使用建议
-
若想快速更改终端窗口标题,直接在命令行输入
titleterm加上新标题。 -
若希望终端窗口标题始终显示当前工作目录,可在
.cshrc或.bashrc中添加相应命令。
-
若想快速更改终端窗口标题,直接在命令行输入
-
注意事项
-
该脚本依赖终端应用程序对转义序列的支持,确保终端支持
ESC ] 0; title BEL序列。
-
该脚本依赖终端应用程序对转义序列的支持,确保终端支持
6.5 itunelist 脚本
-
使用建议
- 若需要共享或同步不同计算机上的 iTunes 音乐库,可运行该脚本来生成摘要列表。
-
可将生成的列表与
diff命令结合使用,找出不同音乐库之间的差异。
-
注意事项
-
该脚本假设 iTunes 配置文件
iTunes Music Library.xml存在且格式正确。 - 若音乐库路径包含特殊字符,可能需要对脚本进行适当调整。
-
该脚本假设 iTunes 配置文件
脚本使用注意事项流程图
graph TD;
A[选择脚本] --> B{是否需要 root 权限};
B -- 是 --> C[使用 sudo 命令运行];
B -- 否 --> D[直接运行];
C --> E{是否依赖特定工具};
D --> E;
E -- 是 --> F[确保工具已安装并可正常使用];
E -- 否 --> G[输入必要信息];
F --> G;
G --> H{是否有特定配置要求};
H -- 是 --> I[检查并修改配置];
H -- 否 --> J[执行脚本];
I --> J;
J --> K{脚本是否成功运行};
K -- 是 --> L[完成操作];
K -- 否 --> M[检查错误信息并解决问题];
M --> J;
7. 脚本的综合应用示例
7.1 批量添加用户并设置邮件别名
假设需要批量添加多个用户,并为每个用户设置对应的邮件别名。可以编写一个新的脚本,结合
addmacuser
和
addmacalias
脚本的功能。
#!/bin/sh
# batch_add_users - 批量添加用户并设置邮件别名
users=(user1 user2 user3)
fullnames=("User One" "User Two" "User Three")
shells=("/bin/bash" "/bin/bash" "/bin/bash")
domains=("example.com" "example.com" "example.com")
for i in "${!users[@]}"; do
user=${users[$i]}
fullname=${fullnames[$i]}
shell=${shells[$i]}
domain=${domains[$i]}
# 添加用户
sudo addmacuser <<EOF
$user
$fullname
$shell
EOF
# 添加邮件别名
sudo addmacalias <<EOF
$user
$user@$domain
EOF
done
echo "批量添加用户和邮件别名完成。"
7.2 定期更新 iTunes 音乐库列表
为了及时了解 iTunes 音乐库的变化,可以编写一个定时任务脚本,定期运行
itunelist
脚本并保存结果。
#!/bin/sh
# update_itunes_list - 定期更新 iTunes 音乐库列表
itunelist > ~/Music/iTunes/iTunes_library_list.txt
echo "iTunes 音乐库列表已更新。"
可以使用
cron
工具设置定时任务,例如每天凌晨 2 点运行该脚本:
0 2 * * * /path/to/update_itunes_list.sh
综合应用示例流程图
graph TD;
A[开始] --> B{选择综合应用场景};
B -- 批量添加用户并设置别名 --> C[定义用户信息数组];
B -- 定期更新 iTunes 列表 --> D[编写更新脚本];
C --> E[循环遍历用户信息];
E --> F[调用 addmacuser 脚本添加用户];
F --> G[调用 addmacalias 脚本添加别名];
G --> H{是否还有用户};
H -- 是 --> E;
H -- 否 --> I[完成批量操作];
D --> J[使用 cron 设置定时任务];
J --> K[定时运行更新脚本];
I --> L[结束];
K --> L;
8. 总结与展望
8.1 总结
本文详细介绍了多个 Mac OS X 实用脚本,包括它们的功能、代码实现、工作原理、运行方法以及可能的改进。通过这些脚本,用户可以更高效地管理系统、处理日常任务,如管理用户、设置邮件别名、定制终端标题和管理 iTunes 音乐库等。
8.2 展望
-
脚本功能扩展
:可以进一步扩展这些脚本的功能,例如为
addmacuser脚本添加更多用户属性设置选项,为itunelist脚本添加排序和过滤功能。 - 脚本集成 :将多个脚本集成到一个管理系统中,实现更自动化和便捷的系统管理。
- 兼容性优化 :确保这些脚本在不同版本的 Mac OS X 系统上都能正常运行,对可能出现的兼容性问题进行修复和优化。
未来改进方向表格
| 脚本名称 | 未来改进方向 |
|---|---|
| listmacusers | 增加排序和过滤功能 |
| addmacuser | 添加更多用户属性设置选项,如用户描述、用户状态等 |
| addmacalias | 支持批量添加别名,添加别名管理功能 |
| titleterm | 支持更多终端样式定制,如颜色、字体等 |
| itunelist | 增加排序和过滤功能,支持导出不同格式的列表 |
超级会员免费看
6

被折叠的 条评论
为什么被折叠?



