我需要创建一个脚本来修改一些数据,这需要超级用户权限。有没有办法通过 Mac 10 上的 shell 脚本自动执行此过程?
示例脚本:
sed s/Hello/World/g /usr/local/opt/test.xml
要运行此脚本,用户需要超级用户权限,有没有办法在脚本中提供root凭据并执行?
答案1
您可以更改 shell 脚本文件的权限,以自动使其以 root 身份运行。
来自 chmod 手册页:
4000 (the set-user-ID-on-execution bit) Executable files with
this bit set will run with effective uid set to the uid of
the file owner. Directories with the set-user-id bit set
will force all files and sub-directories created in them to
be owned by the directory owner and not by the uid of the
creating process, if the underlying file system supports
this feature: see chmod(2) and the suiddir option to
mount(8).
示例命令可能如下所示:
sudo chown root:root file.sh
sudo chmod 4755 file.sh