简体   繁体   English

当使用 here-string 进入登录 shell 并使用 sudo 命令时,如何从文件中读取环境变量?

[英]How environment variables can be read from a file when going into a login shell with here-string and using the sudo command?

I am trying to write a small script that aims to login to a remote server, load environment variables and print one of them.我正在尝试编写一个旨在登录远程服务器、加载环境变量并打印其中一个的小脚本。 (In the actual script, instead of an echo , the parameters that are read are to be used. For the sake of simplicity here I am using just echo .) (在实际脚本中,将使用读取的参数而不是echo 。为了简单起见,我在这里只使用echo 。)

The structure of the script and the commands that I tried are as follows but unfortunately none succeeds:脚本的结构和我尝试过的命令如下,但不幸的是没有成功:

ssh -i lightsail.pem ubuntu@production <<< '
  sudo echo $TEST_PARAMETER
  sudo sh -c "~/Environment/environment-variables.sh && echo $TEST_PARAMETER"
  sudo bash -c "~/Environment/environment-variables.sh && echo $TEST_PARAMETER"
  sudo bash -c "source ~/Environment/environment-variables.sh && echo $TEST_PARAMETER"
  sudo bash <<< "source ~/Environment/environment-variables.sh && echo $TEST_PARAMETER"
';

How environment variables can be read from a file when going into a login shell with here-string and using the sudo command?当使用 here-string 进入登录 shell 并使用 sudo 命令时,如何从文件中读取环境变量?

If your environment variable is set for ubuntu and not root you will need to use sudo -E如果您的环境变量是为 ubuntu 而不是 root 设置的,您将需要使用sudo -E

-E Indicates to the security policy that the user wishes to preserve their existing environment variables -E向安全策略表明用户希望保留其现有环境变量

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

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