简体   繁体   English

将Shell变量传递给系统用户

[英]Passing shell variables to system user

Let's say I want to run some command as system user (no shell): 假设我想以系统用户(没有shell)运行一些命令:

su -s /bin/bash -c "some_command $TEST" my_system_user

How di I pass $TEST variable to user? 我如何将$ TEST变量传递给用户?

I have the variable configured for all users in /etc/profile.d/my_vars.sh 我在/etc/profile.d/my_vars.sh为所有用户配置了变量

export TEST=test_arg

But that doesn't get loaded for the above scenario... 但这在上述情况下不会加载...

I have tried with no luck: 我尝试过没有运气:

-m (--preserve-environment)

First, I tried to create such a file as /etc/profile.d/my_vars.sh and I put a test variable in the file. 首先,我尝试创建/etc/profile.d/my_vars.sh这样的文件,并将测试变量放入文件中。 I can see the value of the variable, no matter I run command as whoever. 无论我以谁的身份运行命令,我都可以看到变量的值。 I've also tried to set a variable on-the-fly with the following code, it works on my centos: 我还尝试使用以下代码即时设置变量,该变量适用于我的centos:

unset A_VAR; export A_VAR=foo
su -s /bin/bash -c "echo val=$A_VAR" my_system_user
> val=foo
unset A_VAR

An alternative solution if it does not work for you, you can consider of playing a ssh trick. 如果您不适合使用其他解决方案,则可以考虑使用ssh技巧。

unset A_VAR; export A_VAR=foo
ssh -o SendEnv my_system_user@localhost "echo val=$A_VAR"
> val=foo
unset A_VAR

That's weird. 这很奇怪。 It seems to work for me. 它似乎为我工作。 I can do: 我可以:

$ export TEST="hello"

$ su -s /bin/bash -c "echo $TEST"
password:
hello

...I guess I'm missing something about the question. ...我想我在这个问题上漏了一些东西。

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

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