相关疑难解决方法(0)

通过 ssh 传输参数时避免字符串拆分

为了启动分布式计算,我编写了一个运行脚本,用于ssh登录远程计算机并启动从标准输入读取的脚本。但是,此运行脚本需要将用户提供的参数正确传输$@到远程计算机。但是,我注意到参数没有正确传输。

比方说,task.sh远程计算机应该执行的脚本是

i=0
for p in "$@"; do
  echo "#${i}: ${p}"
  i=$((i+1))
done
Run Code Online (Sandbox Code Playgroud)

该脚本只是迭代参数并逐行打印它们,例如,

$ bash task.sh hello world 'in one line'
#0: hello
#1: world
#2: in one line
Run Code Online (Sandbox Code Playgroud)

我的简化运行脚本run.sh现在如下所示:

ssh user@remote.example.com "bash -s $@" < task.sh
Run Code Online (Sandbox Code Playgroud)

但是,这将打印:

$ bash run.sh hello world 'in one line'
#0: hello
#1: world
#2: in
#3: one
#4: line
Run Code Online (Sandbox Code Playgroud)

当我$@', ie引用时

ssh user@remote.example.com "bash -s '$@'" < task.sh
Run Code Online (Sandbox Code Playgroud)

打印以下内容:

$ …
Run Code Online (Sandbox Code Playgroud)

linux ssh bash shell

4
推荐指数
1
解决办法
79
查看次数

如何使用转义字符显示多行作为单个字符串的文件(\n)

在bash中,我如何显示一个文件的内容,其中多行作为单个字符串显示为新行\n.

例:

$ echo "line 1
line 2" >> file.txt
Run Code Online (Sandbox Code Playgroud)

我需要"line 1\nline2"使用bash命令获取内容.

我尝试使用组合但cat/printf/echo没有成功.

bash shell terminal io-redirection

0
推荐指数
1
解决办法
152
查看次数

标签 统计

bash ×2

shell ×2

io-redirection ×1

linux ×1

ssh ×1

terminal ×1