【发布时间】:2019-06-29 14:09:48
【问题描述】:
我正在尝试使用 PSSH 编写一个 bash 脚本,它根据主机发送相同的命令但不同的参数。主机名和参数将从不同的文件“list.txt”中提取。
“list.txt”文件的示例如下所示:
10.0.0.1;'hello';'world'
10.0.0.2;'goodbye';'everyone'
10.0.0.3;'thank';'you!'
我目前拥有的一个示例(但不幸的是不工作)如下所示:
#!/bin/bash
# grab the list items and make them into a variable to be able to parse
actionList=$(</root/scripts/list.txt)
# parse out host name for pssh
host_name="$( cut -d ';' -sf 1 <<< "$actionList" )";
# parse out the first argument
argument1="$( cut -d ';' -sf 2 <<< "$actionList" )";
# parse out the second argument
argument2="$( cut -d ';' -sf 3 <<< "$actionList" )";
# pssh command that creates a new file on each server with their respective argument1 and argument2
pssh -i -AH $host_name -t 300 -O StrictHostKeyChecking=no "$(argument1) ' and ' $(argument2) >> arg1_and_arg2.txt"
我很确定削减 $actionList 变量并没有给我想要的东西,但我真正坚持的是,在我完成之后,pssh 命令是否会为 'list.txt' 中的每个项目正确运行从 $actionList 中解析出正确的字符串。
有没有办法让相同的命令,改变文件中的参数与 PSSH 一起工作?有没有更好的程序可以做到这一点?如果有怎么办?任何帮助表示赞赏。
谢谢!
附:如果我对这篇文章进行了格式化或做错了什么,我深表歉意。 StackOverflow 通常是我最后的手段,所以我不经常使用它。再次感谢您的帮助/建议!
【问题讨论】:
-
我不认为
pssh可以为所欲为;它旨在在不同的主机上运行 same 命令(包括参数)。 -
请注意,扩展变量会创建 literal 引号(即数据),而不是 syntactic 引号(被 bash 解析为语法的引号) .因此,您几乎肯定希望将
's 从您的文件中删除。 -
@chepner 我害怕这个答案.. 碰巧知道可以帮助完成此任务的任何其他实用程序/程序吗?
-
@CharlesDuffy 感谢您提供的信息!从现在开始将改变它!
标签: bash scripting parameter-passing pssh