Bash 我在这个边读边循环中使用了什么shell作用域?

Bash 我在这个边读边循环中使用了什么shell作用域?,bash,while-loop,Bash,While Loop,这是我使用上述代码的目标。我有一个文本文件。文本文件包含以下内容 function readIpFile () { configfile='node_list.txt' [ $# -gt 0 ] && [ -r "$1" ] && configfile="$1" sed -e 's/[[:space:]]*#.*// ; /^[[:space:]]*$/d' "$configfile" | while read target role;

这是我使用上述代码的目标。我有一个文本文件。文本文件包含以下内容

function readIpFile () {
configfile='node_list.txt'
    [ $# -gt 0 ] && [ -r "$1" ] && configfile="$1"

    sed -e 's/[[:space:]]*#.*// ; /^[[:space:]]*$/d' "$configfile" |
    while read target role;
    do
    case "$role" in
        m)  esMaster="\"$esMaster\", \"$target\""
            ssh -n "root@$target" "sed  -i 's/#node\.master: false/node\.master: true/' /etc/elasticsearch/elasticsearch.yml"
            ssh -n "root@$target" "sed  -i 's/#node\.data: false/node\.data: false/' /etc/elasticsearch/elasticsearch.yml"
            ssh -n "root@$target" "sed  -i 's/#node\.ingest: false/node\.ingest: false/' /etc/elasticsearch/elasticsearch.yml"
            echo "Node configuration is set!"
            ssh -n "root@$target" "service elasticsearch start"
            ;;
        k)  kbip=$target
            echo "The Kibana node IP is: $kbip"
            echo "Beginning remote kibana deployment!"
            deployRemoteKibana
            ;;
        d)  ssh -n "root@$target" "sed  -i 's/#node\.master: false/node\.master: false/' /etc/elasticsearch/elasticsearch.yml"
            ssh -n "root@$target" "sed  -i 's/#node\.data: false/node\.data: true/' /etc/elasticsearch/elasticsearch.yml"
            ssh -n "root@$target" "sed  -i 's/#node\.ingest: false/node\.ingest: false/' /etc/elasticsearch/elasticsearch.yml"
            echo "Node configuration is set!"
            ssh -n "root@$target" "service elasticsearch start"
            ;;
        i)  ssh -n "root@$target" "sed  -i 's/#node\.master: false/node\.master: false/' /etc/elasticsearch/elasticsearch.yml"
            ssh -n "root@$target" "sed  -i 's/#node\.data: false/node\.data: false/' /etc/elasticsearch/elasticsearch.yml"
            ssh -n "root@$target" "sed  -i 's/#node\.ingest: false/node\.ingest: true/' /etc/elasticsearch/elasticsearch.yml"
            echo "Node configuration is set!"
            ssh -n "root@$target" "service elasticsearch start"
            ;;
        c)  ssh -n "root@$target" "sed  -i 's/#node\.master: false/node\.master: false/' /etc/elasticsearch/elasticsearch.yml"
            ssh -n "root@$target" "sed  -i 's/#node\.data: false/node\.data: false/' /etc/elasticsearch/elasticsearch.yml"
            ssh -n "root@$target" "sed  -i 's/#node\.ingest: false/node\.ingest: false/' /etc/elasticsearch/elasticsearch.yml"
            echo "Node configuration is set!"
            ssh -n "root@$target" "service elasticsearch start"
            ;;
        di) ssh -n "root@$target" "sed  -i 's/#node\.master: false/node\.master: false/' /etc/elasticsearch/elasticsearch.yml"
            ssh -n "root@$target" "sed  -i 's/#node\.data: false/node\.data: true/' /etc/elasticsearch/elasticsearch.yml"
            ssh -n "root@$target" "sed  -i 's/#node\.ingest: false/node\.ingest: true/' /etc/elasticsearch/elasticsearch.yml"
            echo "Node configuration is set!"
            ssh -n "root@$target" "service elasticsearch start"
            ;;
        md) ssh -n "root@$target" "sed  -i 's/#node\.master: false/node\.master: true/' /etc/elasticsearch/elasticsearch.yml"
            ssh -n "root@$target" "sed  -i 's/#node\.data: false/node\.data: true/' /etc/elasticsearch/elasticsearch.yml"
            ssh -n "root@$target" "sed  -i 's/#node\.ingest: false/node\.ingest: false/' /etc/elasticsearch/elasticsearch.yml"
            echo "Node configuration is set!"
            ssh -n "root@$target" "service elasticsearch start"
            ;;
        mi) ssh -n "root@$target" "sed  -i 's/#node\.master: false/node\.master: true/' /etc/elasticsearch/elasticsearch.yml"
            ssh -n "root@$target" "sed  -i 's/#node\.data: false/node\.data: false/' /etc/elasticsearch/elasticsearch.yml"
            ssh -n "root@$target" "sed  -i 's/#node\.ingest: false/node\.ingest: true/' /etc/elasticsearch/elasticsearch.yml"
            echo "Node configuration is set!"
            ssh -n "root@$target" "service elasticsearch start"
            ;;
        mdi)ssh -n "root@$target" "sed  -i 's/#node\.master: false/node\.master: true/' /etc/elasticsearch/elasticsearch.yml"
            ssh -n "root@$target" "sed  -i 's/#node\.data: false/node\.data: true/' /etc/elasticsearch/elasticsearch.yml"
            ssh -n "root@$target" "sed  -i 's/#node\.ingest: false/node\.ingest: true/' /etc/elasticsearch/elasticsearch.yml"
            echo "Node configuration is set!"
            ssh -n "root@$target" "service elasticsearch start"
            ;;
        *)  echo "Unrecognized node setting in node_list.txt!"
            exit
    esac
    recordNodes
    deployRemoteElastic
    applyDiscoverHosts
    done
}
上面的case语句应该根据字母进行选择,字母是文件中每行的最后一个字段。此字母应保存到$role变量中。IP地址应保存到$target,并且$target应附加到$esMaster。这两个变量都将在while循环之外使用

然而,每次我执行代码时,$kbip都是空的,我得到了这个错误

#######
#Instructions to configure file
#
#
#######

xxx.xxx.xxx.xxx m
xxx.xxx.xxx.xxx k
xxx.xxx.xxx.xxx d
如何从文件中读取并将字段1和字段2插入到各自的变量中,同时使这些变量在while循环的范围之外可用?根据我所做的研究,这个BASH脚本甚至不应该创建子shell

我正在考虑调用while循环中的远程部署函数,在那里它将使用子shell中的变量?不过我不想这样做,因为我想尝试将所有函数调用相对地分组在一起,以便根据需要移动它们


不过,我不确定有什么更好的方法可以遍历该文件。

您的脚本在此过程中调用ssh,因此所有本地(每个函数)和全局(每个脚本)变量都不会传递给被调用的shell(
ssh

我个人不会使用这种方法,但会这样做:

  • 读取目标
  • 目标发送执行所需操作的脚本(开关部分)
  • 目标上执行脚本

您还可以使用
scp
发送文件,这可能是最重要的部分:您不必留在客户机上,您也可以在服务器上执行—如果您负担得起的话,您的权限可能低于root。

您的脚本在过程中调用ssh,因此所有本地(每个函数)和全局(每个脚本)变量不会传递给被调用的shell(
ssh

我个人不会使用这种方法,但会这样做:

  • 读取目标
  • 目标发送执行所需操作的脚本(开关部分)
  • 目标上执行脚本

您还可以使用
scp
来发送文件,这可能是最重要的部分:您不必留在客户机上,也可以在服务器上执行—如果您负担得起的话,您的权限可能比root要小。

我不担心root,因为编写此脚本是为了开发实验室环境nts,而不是生产。当访问根目录时,此脚本不会涉及任何敏感内容。在脚本的后面部分,我使用herdocs将函数和变量传递给远程shell以供执行。唯一的问题是使此循环中定义的变量可供herdoc扩展客户端,以便在服务器上定义它们-然后将变量发送过来。通过编写脚本,尤其是当您的所有ssh调用对于每个情况都是相同的情况下,这样做会更好。该变量还需要在本地展开,因为脚本运行的elasticsearch节点需要知道所有MDI节点是什么,而不是kibana节点.但是,当我将kibana节点alsos插入配置行时,它需要访问该变量。基本上,我试图从一台机器上执行所有操作,使用尽可能少的脚本。但这也有点偏离了我最初的问题。我不担心root,因为编写此脚本是为了开发实验室环境,而不是生产环境。在访问根目录时,此脚本不会涉及任何敏感内容。在脚本的后面部分,我使用herdocs将函数和变量传递给远程shell以供执行。唯一的问题是使此循环中定义的变量可供herdoc用于扩展客户端,以便随后可以对它们进行修改定义服务器端并由远程函数使用。然后将变量发送过来。通过编写脚本,尤其是在每个情况下所有ssh调用都相同的情况下,这样做效果更好。该变量还需要在本地展开,因为脚本运行的elasticsearch节点需要知道所有MDI节点是什么,但不需要e kibana节点。但是当我将kibana节点插入配置行时,它也需要访问该变量。基本上,我试图从一台机器上执行所有操作,使用尽可能少的脚本。但这也有点偏离了我最初的问题。我不确定我是否理解
$kbip为空
,以及为什么你认为它与由于
kbip
仅在选项
k
下引用,并且在选项
k
下没有
ssh
调用,因此,对于
ssh
错误,我会一次测试一行输入(即,将一行数据放入
节点列表.txt
)在进入
while
循环之前设置-x
set+x
在退出
while
循环之后)并查看作为输出生成的内容…变量是否按预期设置?传递给
ssh
的是什么?是否有任何
ssh
命令成功执行?是否确实忽略空白/注释行?什么是
recordnodes
,是否验证它没有生成ssh错误?所有目标都保存到函数recordnodes中的n数组。函数的目的是确保所有配置更改都根据需要传播到每个主机。$kbip为空,我假设是因为变量在循环外使用时未更新。编辑:另请参阅更新的脚本。我做了一些更改,因为我开始认为我没有配置只需从循环结构内部调用函数。您确实意识到
sed
可以在循环过程中执行多个操作
ssh: Could not resolve hostname : Name or service not known