redirect command output into variable and standard output in ksh

与世无争的帅哥 提交于 2019-11-27 20:31:12

How about:

VAR=$(ls | tee /dev/tty)

great answer from @Gary_Barker, but this isn't possible on all systems.

On our teamcity there isn't a char-console. And there is another little problem.

If I use

VAR=$(ls -1); echo $VAR

it isn't the same like ls -1

My solution works, if it doesn't matter, that the output comes from error pipe.

VAR=$(ls -1 | tee >&2)

As i understand, VAR=`ls`;echo $VAR is OK but you don't like because has 2 commands on it.

While @Gary-Barker is working, i have not check on all systems. If you got problem with tee or another, you can build your own ALWAYS.

I don't know if you know that, but a lot of the programs you can use on Linux are just a bunch of code using the small binarys on system. While this is true, there's no sense about use 1 or 2 commans, because the final execution is really a bunch of little ones.

So, if your real problem is that you can only write a single command in your target, you can write your own "app", by making a sh script in /sbin folder an leaving it without .sh extension (because these are excecuted with ./ or sh prefix and not by name)

I wrote that as example:

#!/bin/bash if [ $1 ] then VAR=$*; echo $VAR fi

For this example i have make the file /sbin/varrun. I've tried it with the folling commands with successful (normal) output:

  • varrun ls
  • varrun uname
  • varrun uname -a

Note that i've not used "quotes" on commands with spaces.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!