此问题在此处已有答案:
Why can't I use 'sudo su' within a shell script? How to make a shell script run with sudo automatically(6个答案)
How to sudo su; then run command(4个答案)
Why can't I use 'sudo su' within a shell script? How to make a shell script run with sudo automatically(1个答案)
Run sudo -s inside shell script(4个答案)
4天前关闭。
无法在用户的同一会话中执行以下触摸命令- weblogic。相反,下面的touch命令正在用户-djaiswa 2上执行,并且失败[因为它试图访问所有者为weblogic的路径]。
[djaiswa2@cdcesb01 tmp]$ cat test.sh
#!/bin/bash
sudo su - weblogic;
touch /opt/middleware/.ssh/authorized_keys;
chmod 755 /opt/middleware/.ssh/authorized_keys;
[djaiswa2@cdcesb01 tmp]$ sh -x test.sh
+ sudo su - weblogic
Last login: Thu Aug 26 00:38:06 EDT 2021 on pts/0
-bash-4.2$ exit
logout
+ touch /opt/middleware/.ssh/authorized_keys
touch: cannot touch ‘/opt/middleware/.ssh/authorized_keys’: Permission denied
+ chmod 755 /opt/middleware/.ssh/authorized_keys
chmod: cannot access ‘/opt/middleware/.ssh/authorized_keys’: No such file or directory
字符串
1条答案
按热度按时间ha5z0ras1#
您需要使用
su
命令和-c
选项来指定要执行的命令。例如:
字符串