简体   繁体   English

Sudo会中断导入我的脚本

[英]Sudo will break importing my script

I have a shell script (my.sh) which imports another file (myfile.env). 我有一个shell脚本(my.sh),可以导入另一个文件(myfile.env)。 The name of myfile.env is passed as an argument to my.sh. myfile.env的名称作为参数传递给my.sh。 It works well as root and I want the ability to run it to be given for other users via sudo. 它以root身份运行良好,我希望能够通过sudo将其运行功能提供给其他用户。

myfile.env myfile.env

some shell scripts........

my.sh 我的

ENVFILE="$1"
if [ -e $ENVFILE ] ; then
    . $ENVFILE
fi 

I call the my.sh as 我称my.sh为

sudo /abc/def/my.sh myfile.sh

sudoers file goes as below sudoers文件如下

Cmnd_Alias MYSUDO = /abc/def/my.sh, /bin/bash
%mygroup ALL = MUSUDO

Unfortunately this results with an error in the 3rd line in the my.sh code above. 不幸的是,这导致上述my.sh代码第三行出现错误。 It says 它说

.: myfile.env: cannot open [No such file or directory]

its funny that i get this error after a check on file exist returned true. 有趣的是我在检查文件存在后收到此错误,返回true。

sudo -s and -E options didn't help (don't know whether I used them correctly) sudo -s和-E选项没有帮助(不知道我是否正确使用了它们)

Can someone please help me to resolve this? 有人可以帮我解决这个问题吗? I am using CeNT OS 6.5 我正在使用CeNT OS 6.5

use sudo /abc/def/my.sh /absolute/path/to/myfile.env 使用sudo /abc/def/my.sh /absolute/path/to/myfile.env

If myfile.sh is supposed to be in the same dir as my.sh, then my.sh can do this: 如果应该将myfile.sh与my.sh放在同一目录中,则my.sh可以执行以下操作:

ENVFILE=$(dirname "$0")/"$1"

Finally I was able to get it done. 终于,我能够完成它。 It was setting in the sudoer file to get the directories (the folder in with those files were) into safe path list. 它是在sudoer文件中设置的,以将目录(包含这些文件的文件夹)放入安全路径列表。

All I had to do was, 我所要做的就是

run visudo

and append the two dolders (where the files are) into 并将两个海德(文件所在的位置)附加到

Defaults secure_path  list

That will add those directories into the PATH when a user get elevated rights. 当用户获得提升的权限时,会将这些目录添加到PATH中。

https://askubuntu.com/questions/128413/setting-the-path-so-it-applies-to-all-users-including-root-sudo https://askubuntu.com/questions/128413/setting-the-path-so-it-applies-to-all-users-includes-root-sudo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM