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

标签 linux bash shell awk ssh

我正在尝试将外部变量 $time 传递给 awk 命令,该命令通过 ssh 运行到另一台机器。

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

这是我到目前为止所做的,我传递给 ssh 的命令可以自行工作,但通过 ssh 传递它会导致它失败。

我也试过了..

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

但是 ssh 调用不会返回新行,因此 awk 无法获得正确的信息。 任何人都知道如何在我首先尝试的内容中传递外部变量,或者如何在我第二次尝试的内容中保留 ssh 调用中的换行符?

最佳答案

我有以下 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

当我运行bash test.sh <some_valid_host> 20:00时它按预期打印用户列表,所有用户都在同一行中。那些处于同一行的人也是预期的。

如果您有一个文件 test.txt,例如:

abc
def ghi

正在运行

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

将打印abc def ghi都在同一条线上。这是预期的行为。

关于linux - 在awk中使用外部变量传递给bash中的ssh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29859535/

相关文章:

c - 内核模块系统调用覆盖

linux - 删除两个句子对齐文件中的重复行,Linux

linux - 如何使用 AWK 来唯一化一个表(为每个唯一 ID 保留最大值)?

linux - 强制 shell 脚本 fflush

shell - 1>/dev/null 2>&1 & pid1=$!意思?

linux - 在 Linux 中如何通知用户空间应用程序驱动程序已收到中断?

linux - ARM 不会产生未对齐的异常

bash - Bash 中双引号前的美元字符

c - 链表的空闲列表递归函数有什么作用?

linux - 需要在 Windows korn shell 中运行 ksh 脚本