26、Mac OS X 实用脚本指南

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 数据库中创建账户,但后续需手动处理。

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 存在且格式正确。
    • 若音乐库路径包含特殊字符,可能需要对脚本进行适当调整。

脚本使用注意事项流程图

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 增加排序和过滤功能,支持导出不同格式的列表
下载前可以先看下教程 https://pan.quark.cn/s/16a53f4bd595 小天才电话手表刷机教程 — 基础篇 我们将为您简单的介绍小天才电话手表新机型的简单刷机以及玩法,如adb工具的使用,magisk的刷入等等。 我们会确保您看完此教程后能够对Android系统有一个最基本的认识,以及能够成功通过magisk root您的手表,并安装您需要的第三方软件。 ADB Android Debug Bridge,简称,在android developer的adb文档中是这么描述它的: 是一种多功能命令行工具,可让您与设备进行通信。 该命令有助于各种设备操作,例如安装和调试应用程序。 提供对 Unix shell 的访问,您可以使用它在设备上运行各种命令。 它是一个客户端-服务器程序。 这听起来有些难以理解,因为您也没有必要去理解它,如果您对本文中的任何关键名词产生疑惑或兴趣,您都可以在搜索引擎中去搜索它,当然,我们会对其进行简单的解释:是一款在命令行中运行的,用于对Android设备进行调试的工具,并拥有比一般用户以及程序更高的权限,所以,我们可以使用它对Android设备进行最基本的调试操作。 而在小天才电话手表上启用它,您只需要这么做: - 打开拨号盘; - 输入; - 点按打开adb调试选项。 其次是电脑上的Android SDK Platform-Tools的安装,此工具是 Android SDK 的组件。 它包括与 Android 平台交互的工具,主要由和构成,如果您接触过Android开发,必然会使用到它,因为它包含在Android Studio等IDE中,当然,您可以独立下载,在下方选择对应的版本即可: - Download SDK Platform...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值