[英]Switch user and store command output in variable
I'm trying to get command output into a variable when switching user in my script: 在脚本中切换用户时,我试图将命令输出转换为变量:
checkOutput="$(echo command1 | su - user1)"
But using echo
on the variable does not return the desired result: 但是在变量上使用
echo
不会返回期望的结果:
echo $checkOutput
Output: 输出:
Oracle Corporation SunOS ... January 2005 logout
Desired output (to be stored in variable): 所需的输出(存储在变量中):
Java version ...
Apache Qpid is RUNNING with PID=5227
When running command echo command1 | su - user1
当运行命令
echo command1 | su - user1
echo command1 | su - user1
in terminal the output is: echo command1 | su - user1
终端中的echo command1 | su - user1
输出为:
Oracle Corporation SunOS ... January 2005
Java version ...
Apache QPID is RUNNING with PID=5227
logout
I know that when su - user1
is called the script exits its current scope. 我知道调用
su - user1
时,脚本退出其当前作用域。 Is there a way to store the output of the command? 有没有办法存储命令的输出? Or is this impossible using bash.
还是使用bash无法做到这一点。
Thanks 谢谢
Use script
command to log output in a file. 使用
script
命令将输出记录在文件中。 So it would look something like this: 所以看起来像这样:
script -c '$(echo command1 | su - user1)' -f file.txt
You can then reference this file to get desired output from it. 然后,您可以引用该文件以获得所需的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.