我想实现以下目标
- 从
read nodename
获取用户输入 - ssh 到远程主机和
grep -w/path/to/file $nodename
- 如果找到 $nodename
echo "Sorry, hostname already exists, please try again"
- 为新的 $nodename 获取用户输入
- 重复直到 $nodename 是唯一的
我有什么
echo "Please enter a node name"
read nodename
while ssh remotehost.example.com "grep -w '$nodename' /path/to/file"
do
echo "Sorry, node name already exists, please try again"
read nodename
done
如果我在提示符下运行 ssh remotehost.example.com "grep -w '$node'/path/to/file"
,它会返回正确的答案。然而,当在上面的 while 循环中运行时,远程文件中的所有内容都会被打印到标准输出,而答案总是要求一个新的节点名。
我需要更改什么才能使 ssh remotehost.example.com "grep -w '$node'/path/to/file"
在 while 循环中工作?
最佳答案
下一步试试:
echo "Please enter a node name"
read nodename
while true
do
ssh example.com "grep -q ${nodename} /path/to/file"
status="${?}"
if [[ ${status} -eq 0 ]]
then
echo "Sorry, node name already exists, please try again"
read nodename
else break
fi
done
grep -q ...
- 安静;不要向标准输出写入任何内容。如果找到任何匹配项,即使检测到错误,也会立即以零状态退出。status="${?}"
- 通过 ssh 命令执行的远程命令的退出状态- 检查远程命令的
状态
并执行所需的逻辑 - 利润
关于linux - Bash while 循环使用包含本地 $variable 的远程 grep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48662903/