Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Shell脚本以root身份登录,通过.txt文件提供输入,并在终端中执行python脚本_Linux_Bash_Shell_Scripting - Fatal编程技术网

Linux Shell脚本以root身份登录,通过.txt文件提供输入,并在终端中执行python脚本

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

我编写了以下脚本以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:~/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用户(安全性目前不在考虑范围内,所以不介意以明文形式传递密码)
  • 更改到目标脚本所在的目录
  • 打开向目标脚本提供输入的文本文件。按住/等待,直到用户保存并关闭文本文件
  • 执行目标脚本
  • 因此,我们可以从上面的输出中看到,只满足了3个部分和4个部分(不保留或等待用户保存并关闭
    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