简体   繁体   English

在awk中使用外部变量传递给bash中的ssh

[英]Using external variable in awk being passed to ssh in bash

I'm trying to pass an external variable $time to an awk command that im running through an ssh to another machine. 我正在尝试将外部变量$ time传递给awk命令,该命令通过ssh运行到另一台计算机。

time=...
pastusers=`ssh -n $host "last -n 10 | awk -v TIME=$time '{if ($7 > TIME) print $1}'"`

Is what I've done so far and the command Im passing to ssh works on its own but passing it through ssh causes it to fail. 到目前为止,这是我做的事情,传递给ssh的命令Im可以单独工作,但是通过ssh传递会导致它失败。

I also tried.. 我也试过..

pastusers=`ssh -n $host "last -n 10"` | awk -v TIME=$time '{if ($7 > TIME) print $1}'

but the ssh call does not return the new lines so awk doesn't get the right information. 但是ssh调用不会返回新行,因此awk无法获得正确的信息。 Anyone have an idea to pass an external variable in what I tried first or how to keep the newline characters from the ssh call in what I tried second? 任何人都有一个想法,要在我首先尝试的内容中传递一个外部变量,或者在我第二次尝试的内容中如何保持ssh调用中的换行符?

I have the following test.sh: 我有以下test.sh:

#! /bin/bash
host=$1
mytime=$2
pastusers=`ssh -n $host "last -n 10" | awk -v TIME=$mytime '{if($7>TIME) print $1}'`
echo $pastusers

When I run bash test.sh <some_valid_host> 20:00 it prints a list of users as expected, all in the same line. 当我运行bash test.sh <some_valid_host> 20:00它将按预期打印用户列表,所有列表都在同一行中。 Those being in the same line is also expected. 那些在同一行中的人也是可以预期的。

If you have a file test.txt like: 如果您有一个文件test.txt,例如:

abc
def ghi

Running 跑步

words=`cat test.txt`; echo $words

will print abc def ghi all in same line. 将在同一行中全部打印abc def ghi This is the expected behavior. 这是预期的行为。

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

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