有没有办法在脚本中切换用户身份(作为安装过程的一部分以 root 身份执行)以执行某些命令而不调用外部脚本,然后返回root运行其他命令?
有点:
#!/bin/bash
some commands as root
SWITCH_USER_TO user
some commands as user including environment variables checks, without calling an external script
SWITCH_USER_BACK
some other stuff as root, maybe another user id change...
Run Code Online (Sandbox Code Playgroud) [Computer]$ grep "foo|bar" filename
Run Code Online (Sandbox Code Playgroud)
我知道上面的命令应该返回文件名中存在“foo”或“bar”的每一行。手册页确认 | 作为正则表达式或符号,代码独立用于“foo”和“bar”。我错过了什么?