sudo源:找不到命令

伯尼2436

我想以根用户身份运行该程序。它会激活一个虚拟环境并在该虚拟环境中运行python程序。

该程序看起来像这样

$cat myfile.sh
source /pathto/venv/bin/activate
python /pathto/rsseater.py

如果我仅以root身份运行它,我将得到

$ sudo ./myfile.sh 
./myfile.sh: 2: ./myfile.sh: source: not found

我发现了这一点,但是如果我尝试将其添加sudo -s到文件的顶部,似乎将提示更改为#,但是python并未按预期运行。我不确定在文件顶部有sudo -s时程序在做什么。

如何使该程序以root身份运行?

你好

通常,您希望在脚本顶部添加对解释器的调用,如下所示:

$cat myfile.sh
#!/bin/bash

source /pathto/venv/bin/activate
python /pathto/rsseater.py

这看起来可能与您所拥有的非常相似,但实际上却有很大的不同。在您的方案中,您尝试在shell中运行在运行时被调用的命令sudo在上面的示例中,您将获得一个单独的Bash shell,在其中#!/bin/bash调用之后将具有各种命令#!/bin/bash导致新的Bash shell被分叉。

口译员和社棒

的名称#!称为“舍邦”。您可以通过本网站以及Wikipedia上的各种问答获取更多信息但是足以说,它告诉系统,当“执行”(即运行)给定文件时,首先要从磁盘(解释器)中加载程序,然后负责解析文件内容的工作。刚刚执行。

注意:基本上在shebang行之后的所有内容。

口译员

解释器是可以一次处理一个文件中的命令的任何程序。在这种情况下,我们使用Bash shell作为解释器。但是,您也可以在这里使用其他外壳,例如C外壳或Korne外壳。

您也可以以相同的方式使用更高级别的解释器,例如Perl,Ruby或Python。

为什么使用'sudo -s'错误

如果您查看手册页,sudo它的内容是-s

 -s [command]
            The -s (shell) option runs the shell specified by the SHELL 
            environment variable if it is set or the shell as specified in 
            the password database.  If a command is specified, it is passed
            to the shell for execution via the shell's -c option.  If no 
            command is specified, an interactive shell is executed.

因此,当您运行此命令时:

$ sudo -s ./myfile.sh

发生以下情况:

$ /bin/sh
sh-4.2$ source somesource.bash 
sh: source: somesource.bash: file not found

该外壳程序/bin/sh不支持command source

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章