Linux Shell脚本以root身份登录,通过.txt文件提供输入,并在终端中执行python脚本
我编写了以下脚本以root身份登录,更改目录(目标脚本所在的位置),打开输入文本文件“input.txt”,并执行Linux Shell脚本以root身份登录,通过.txt文件提供输入,并在终端中执行python脚本,linux,bash,shell,scripting,Linux,Bash,Shell,Scripting,我编写了以下脚本以root身份登录,更改目录(目标脚本所在的位置),打开输入文本文件“input.txt”,并执行python脚本: #!/bin/bash echo "hello" whoami echo pass**rd | su cd ~/Scripts pwd gedit input.txt gnome-terminal -e 'bash -c \"python test.py\"' echo "good bye" 上面给出了以下输出: user4@user-pc-4:~/Deskto
python
脚本:
#!/bin/bash
echo "hello"
whoami
echo pass**rd | su
cd ~/Scripts
pwd
gedit input.txt
gnome-terminal -e 'bash -c \"python test.py\"'
echo "good bye"
上面给出了以下输出:
user4@user-pc-4:~/Desktop$ ./DAT_run.sh
hello
user4
su: must be run from a terminal
/home/user4/Scripts
good bye
The child process exited normally with status 1.
除上述内容外,我还有一个打开“input.txt”的gedit
窗口,还有一个gnome终端
窗口,上面写着:
The child process exited normally with status 1.
它仅满足我的部分要求,这些要求(按各自的顺序)是:
su
转换为root用户(安全性目前不在考虑范围内,所以不介意以明文形式传递密码)gedit
)。我在终端窗口上也看不到目标程序的打印。我是个笨蛋,我觉得我很可能会错过一些东西。我怎样才能达到上述所有要求
更新:
在阅读了下面的建议之后,我将NOPASSWD:ALL
previlege规范添加到user4
文件userConfig
,并将其添加到/etc/sudoers.d/
。然后,我将脚本修改为以下内容:
#!/bin/bash
echo "hello"
gnome-terminal -e 'bash -c \"cd ~/Scripts;pwd;gedit input.txt;python test.py\"'
echo "good bye"
这仍然为我提供了以下输出:
user4@user-pc-4:~/Desktop$ ./DAT_run.sh
hello
user4
su: must be run from a terminal
/home/user4/Scripts
good bye
The child process exited normally with status 1.
这里可能出了什么问题?您不能在没有密码的情况下设置
sudo
,也许只是为了特定的命令
读取密码的进程,如su
和ssh
(以及sudo
)坚持从终端读取密码。有很多方法可以提供,但你不想跳过这些障碍,因为这要容易得多
另一件事是脚本中应该作为超级用户运行的部分必须作为
su
/sudo
的参数提供。由于su
/sudo
不会将当前shell“切换”为超级用户,因此它们执行子shell。但是脚本中的下一个命令将在执行该脚本的shell中运行,该shell位于su
/sudo
之后,包括它的子shell,exit,而不是在该子shell中。您不能在没有密码的情况下设置sudo
,可能只是针对特定的命令吗
读取密码的进程,如su
和ssh
(以及sudo
)坚持从终端读取密码。有很多方法可以提供,但你不想跳过这些障碍,因为这要容易得多
另一件事是脚本中应该作为超级用户运行的部分必须作为
su
/sudo
的参数提供。由于su
/sudo
不会将当前shell“切换”为超级用户,因此它们执行子shell。但是脚本中的下一个命令将在su
/sudo
之后执行该脚本的shell中运行,包括它的子shell exit,而不是在该子shell中。您的引用很有趣,cd
会重复。尝试gnome终端-ebash-c'cd~/脚本;pwd;gedit input.txt;python test.py'
。您似乎忘记了在更新版本中实际调用sudo
。您的引用很有趣,cd
会重复。尝试gnome终端-ebash-c'cd~/脚本;pwd;gedit input.txt;python test.py'
。您似乎忘记了在更新版本中实际调用sudo
。