‘\n’ 在我运行 ssh 命令时与 parallel 命令一起无法识别。

huangapple go评论64阅读模式
英文:

'\n' cannot be identified when i was running ssh command with parallel command

问题

  • 通常情况下,我可以运行以下命令 ssh -lroot [主机IP] "echo -e 'test1\ntest2'" > ~/test.txt 来将两行文本写入主机 [主机IP] 的 test.txt 文件。

  • 然而,当我尝试与 parallel 结合使用时,即 parallel -a argfile -j3 -k --tag ssh -lroot {} "echo -e 'test1\ntest2' \> ~/test.txt",它会忽略 '\n' 并实际写入 test.txt 的内容为 'test1ntest2'。

  • 如何使 '\n' 在 parallel 命令中按预期工作?有人可以帮助澄清吗?

  • 我尝试在单引号前添加反斜杠,如下所示:
    parallel -a argfile -j3 -k --tag ssh -lroot {} "echo -e \'test1\ntest2\' \> ~/test.txt"
    但仍然不如预期。

  • 尝试在 '\n' 前添加反斜杠,也没有起作用。
    parallel -a argfile -j3 -k --tag ssh -lroot {} "echo -e 'test1\\ntest2' \> ~/test.txt"

我只是想使用 parallel 批量将两行文本写入远程服务器的特定文件...

英文:
  • Commonly, I can run this command ssh -lroot [host ip] "echo -e 'test1\ntest2'" > ~/test.txt to write two lines in test.txt file in host [host ip].

  • However, when i used it with parallel, that is parallel -a argfile -j3 -k --tag ssh -lroot {} "echo -e 'test1\ntest2' \> ~/test.txt", it'll ignore '\n' and write 'test1ntest2' actually into test.txt.

  • How to make '\n' work as expect in parallel command? Anybody can help to clarify?

  • I have tried to add backslash before single quotes as below:
    parallel -a argfile -j3 -k --tag ssh -lroot {} "echo -e \'test1\ntest2\' \> ~/test.txt"
    It's still not as expect.

  • Tried to add backslash before '\n', also did not work.
    parallel -a argfile -j3 -k --tag ssh -lroot {} "echo -e 'test1\\ntest2' \> ~/test.txt"

I just want to write two lines to a certain file of remote server in bulk by using parallel...

答案1

得分: 1

已经使用以下命令修复了此问题:parallel -a argfile -j3 -k --tag ssh -lroot {} "echo -e 'test1\\\\ntest2' \> ~/test.txt"

我认为解析过程可能如下:

  • 正常输入:\\\\n
  • 本地主机 shell 解析一层反斜杠:\\n
  • parallel 命令识别单引号,保持原样:\\n
  • ssh 解析一层反斜杠:\n
  • 'echo -e' 命令将 \n 解析为换行符。

如果我们将单引号更改为双引号或直接删除单引号,则反斜杠的数量需要更改为两个:parallel -a argfile -j3 -k --tag ssh -lroot {} "echo -e "test1\\\\\\\\ntest2" \> ~/test.txt"

祝大家玩得开心:)。

英文:

Already fixed this issue with command:parallel -a argfile -j3 -k --tag ssh -lroot {} "echo -e 'test1\\\\ntest2' \> ~/test.txt"

I think the parsing process maybe as below:

  • normal input:\\\\n
  • local host shell parses one layer backslash: \\n
  • parallel command identifys single quotes, keep origin: \\n
  • ssh parses one layer backslash: \n
  • 'echo -e' command parses \n to new line.

If we change single quotes to double quotes or remove single quotes directly, the number of backslash needs to change to double: parallel -a argfile -j3 -k --tag ssh -lroot {} "echo -e "test1\\\\\\\\ntest2" \> ~/test.txt".
Have fun guys:).

答案2

得分: 0

也尝试 --nonall

parallel --slf argfile -j3 -k --nonall --tag --ssh 'ssh -lroot' echo -e 'test1\\ntest2' \> ~/test.txt
英文:

Also try --nonall:

parallel --slf argfile -j3 -k --nonall --tag --ssh 'ssh -lroot' echo -e 'test1\\ntest2' \> ~/test.txt

huangapple
  • 本文由 发表于 2023年7月13日 11:50:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定