将带有空格+通配符的变量传递给远程ssh

时间:2018-04-11 07:04:51

标签: unix ssh wildcard

我试图将带有空格的变量传递给ssh。

report_name="This is a sample report"
dir=/home/sample

ssh $remote_host "ls -t "$dir/$report_name"*.xlsx | head -1"

输出:

ls: is: No such file or directory
ls: a: No such file or directory
ls: sample: No such file or directory
ls: report*.xlsx: No such file or directory

如何使这项工作?

编辑:

解答: 经过几次试验和错误,我使用下面的工作。

ssh $remote_host "ls -t "$dir"/'"$report_name"'.xlsx | head -1"

1 个答案:

答案 0 :(得分:0)

这是一个不涉及逃避空白区域的示例:

#!/bin/bash
report_name="This is a sample report"
dir=/tmp

echo "First file:"
ssh $remote_host "ls -t '$dir/$report_name'*.xlsx | head -1"
echo "All files:"
ssh $remote_host "ls -t '$dir/$report_name'*.xlsx"

外部双引号基本上是逃避内部单引号。这导致它们进入远程shell。这是我的输出:

First file:
/tmp/This is a sample report_4.xlsx
All files:
/tmp/This is a sample report_4.xlsx
/tmp/This is a sample report_2.xlsx
/tmp/This is a sample report_3.xlsx
/tmp/This is a sample report_1.xlsx