find、printf 和 ssh 的奇怪行为

find、printf 和 ssh 的奇怪行为

所以,这有效:

find /dir/ -type f -printf "%p|%TY-%Tm-%Td|%TH:%TM|%s|%u|%U\n" > /dir/output.txt

但这并没有:

ssh -o StrictHostKeyChecking=no "servername" find /dir/ -type f -printf "%p|%TY-%Tm-%Td|%TH:%TM|%s|%u|%U\n" > /dir/output.txt

当运行 ssh 命令时,出现以下错误:

bash: %TY-%Tm-%Td: command not found
bash: %TH:%TM: command not found
bash: %s: command not found
bash: %u: command not found
bash: %Un: command not found

我缺少重新格式化此命令的特定内容吗?

答案1

我缺少重新格式化此命令的特定内容吗?

是的。当您通过 ssh 执行命令时,您会丢失一级引用。所以这:

find /dir/ -type f -printf "%p|%TY-%Tm-%Td|%TH:%TM|%s|%u|%U\n"

变成:

find /dir/ -type f -printf %p|%TY-%Tm-%Td|%TH:%TM|%s|%u|%U\n

这是一堆用管道 ( |) 连接在一起的 shell 命令。您可以通过将整个内容放在单引号中来解决此问题:

ssh -o StrictHostKeyChecking=no "servername" 'find /dir/ -type f -printf "%p|%TY-%Tm-%Td|%TH:%TM|%s|%u|%U\n"' > /dir/output.txt

或者将脚本作为 stdin 传递给 bash:

ssh -o StrictHostKeyChecking=no "servername" bash <<EOF > /dir/output.txt
find /dir/ -type f -printf "%p|%TY-%Tm-%Td|%TH:%TM|%s|%u|%U\n"
EOF

答案2

明信片上的答案是...在原始命令的前缀和后缀上加上单引号,如下所示:

ssh -o StrictHostKeyChecking=no "servername" 'find /dir/ -type f -printf "%p|%TY-%Tm-%Td|%TH:%TM|%s|%u|%U\n" > /dir/output.txt'

相关内容