简体   繁体   English

捕获变量中的命令输出并抑制它

[英]Capture command output in variable and suppress it

I want to capture output of the following command in a shell script:我想在 shell 脚本中捕获以下命令的输出:

echo "User-Name=root,User-Password=root123" | radclient 192.168.19.104 auth testing123

I have a shell script as follows:我有一个shell脚本如下:

FILE=server

        while IFS="\t" read -r ip key;
        do
                case "$ip" in
                        "0.0.0.0") continue ;;
                esac
                echo "User-Name=root,User-Password=root123" | radclient $ip auth $key

        done < "$FILE"
        exit 1

Edit: Removed the extra quotes and it works.编辑:删除了额外的引号,它的工作原理。

Now, Upon running the command:现在,运行命令后:

echo "User-Name=root,User-Password=root123" | radclient 192.168.19.104 auth testing123

It returns following output: (Actually the radclient returns the following output).它返回以下输出:(实际上radclient返回以下输出)。

Received response ID 1, code 2, length = 33
    Reply-Message = "Hello, root"

How to capture output from the command.如何捕获命令的输出。

Edit: As per Answer below I used:编辑:根据下面我使用的答案:

OUTPUT=`echo "User-Name=$username,User-Password=$passwd" | radclient $ip auth $key`

This captures returned string in OUTPUT.这会在 OUTPUT 中捕获返回的字符串。 However it also prints the output on screen.但是它也会在屏幕上打印输出。

How can we suppress output of radclient from printing on screen as well as store the ouput in OUTPUT.我们如何抑制 radclient 的输出在屏幕上打印以及将输出存储在 OUTPUT 中。

You can store the output in a variable like this:您可以将输出存储在这样的变量中:

OUTPUT=$(echo "User-Name=root,User-Password=root123" | radclient $ip auth $key)
echo "$OUTPUT"

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

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