具有一个变量的输入,使用静态列表动态地为另一个变量创建值。

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

Having an input for a variable, dynamically create values for another variable using a static list

问题

INPUT=cn01

OUTPUT:-

SERVERS=cn01
CLIENTS=cn02,cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10
(echo "server $SERVERS"; echo "client $CLIENTS") > cn01.cmd

INPUT=cn02

OUTPUT:-

SERVERS=cn02
CLIENTS=cn01,cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10
(echo "server $SERVERS"; echo "client $CLIENTS") > cn02.cmd

SERVERS=cn01,cn02
CLIENTS=cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10
(echo "server $SERVERS"; echo "client $CLIENTS") > cn01_cn02.cmd

INPUT:- Values for the SERVERS for example

If cn03, cn07 is provided as input then

SERVERS=cn03,cn07
CLIENTS=cn01,cn02,cn04,cn05,cn06,cn08,cn09,cn10
(echo "server $SERVERS"; echo "client $CLIENTS") > cn03_cn07.cmd

我尝试了以下代码:

for i in cn{01..10}; do 
   echo $i
   sed "s/$i//g" nodes.txt | sed 's/,,/,/g' | sed 's/,*$//g' | sed 's/^,//g' 
done
英文:

Having an input for a variable, dynamically create values for another variable using a static list

I have a set of hostnames, I would want to assign these hostnames to the variable SERVERS or CLIENTS. When the hostname from the given list is selected for SERVERS it should get removed from the CLIENTS variable.

Note:- variable SERVER might have more than one hostname

> LIST_OF_NODEScn01,cn02,cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10
INPUT=cn01
>
OUTPUT:-
>
> SERVERS=cn01
> CLIENTS=cn02,cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10
> (echo "server $SERVERS"; echo "client $CLIENTS") > cn01.cmd
>
INPUT=cn02
>
OUTPUT:-
>
> SERVERS=cn02
> CLIENTS=cn01,cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10
> (echo "server $SERVERS"; echo "client $CLIENTS") > cn02.cmd
>
> SERVERS=cn01,cn02
> CLIENTS=cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10
> (echo "server $SERVERS"; echo "client $CLIENTS") > cn01_cn02.cmd

INPUT:- Values for the SERVERS for example

If cn03, cn07 is provided as input then

> SERVERS=cn03,cn07
> CLIENTS=cn01,cn02,cn04,cn05,cn06,cn08,cn09,cn10
> (echo "server $SERVERS"; echo "client $CLIENTS") > cn03_cn07.cmd

I tried the following

for i in cn{01..10}; do 
   echo $i
   sed "s/$i//g" nodes.txt | sed 's/,,/,/g'| sed 's/,*$//g' | sed 's/^,//g' 
done

答案1

得分: 2

如果我理解您的需求正确的话,请尝试以下操作。

cat script.ksh
echo "以cn01或cn02等形式输入cn值在这里...."
read value

SERVERS="$value"
LIST_OF_NODES="cn01,cn02,cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10"

awk -v servers="$SERVERS" -v clients="$LIST_OF_NODES" '
BEGIN{
  s1=","
  split(servers,array,",")
  for(i in array){
    gsub(s1 array[i] s1,s1,clients)
    gsub("^"array[i] s1,"",clients)
    gsub(s1 array[i]"$","",clients)
  }
  gsub(/,/,"_",servers)
  print clients > servers".cmd"
}'

创建上述脚本。现在当我们运行脚本时,将会得到以下输出:

./file.ksh
以cn01或cn02等形式输入cn值在这里....
cn04,cn07

将创建名为cn04_cn07.cmd的输出文件。

英文:

If I understood your requirement correctly, could you please try following.

cat script.ksh
echo "Enter cn values in form of cn01 or cn02 etc here...."
read value

SERVERS="$value"
LIST_OF_NODES="cn01,cn02,cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10"   

awk -v servers="$SERVERS" -v clients="$LIST_OF_NODES" '
BEGIN{
  s1=","
  split(servers,array,",")
  for(i in array){
    gsub(s1 array[i] s1,s1,clients)
    gsub("^"array[i] s1,"",clients)
    gsub(s1 array[i]"$","",clients)
  }
  gsub(/,/,"_",servers)
  print clients > servers".cmd"
}'

Created above script. Now following is when we run the script:

./file.ksh
Enter cn values in form of cn01 or cn02 etc here....
cn04,cn07

Output file named cn04_cn07.cmd will be created.

答案2

得分: 2

假设:

  • nodes.txt 包含所有可能的节点列表,最初所有节点都被视为“客户端”节点。
  • nodes.txt 条目可以位于一个或多个行上,如果一行上有多个节点,则它们由逗号(,)分隔。
  • 脚本的输入是一个或多个节点名称,多个节点之间用逗号(,)分隔。

示例 nodes.txt 内容:

$ cat nodes.txt
cn01,cn02,cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10
cn11,cn12

我们将使用 awk 解决方案从节点列表中删除“服务器”节点,首先将所有节点(nodes.txt)加载到 clients[] 数组中。对于服务器节点,我们将从 clients[] 数组中删除节点。最后,我们将打印 clients[] 数组中剩下的内容。

示例脚本:

$ cat split_nodes 
SERVERS=                                      # 存储我们的输入节点(们)

outfile=${SERVERS//,/_}.cmd                       # 通过将 ',' 替换为 '_' 生成输出文件名
echo "output file: ${outfile}"

echo "server ${SERVERS}" > "${outfile}"           # 将 'server' 行写入 ${outfile}

awk '
BEGIN   { FS=RS="[,\n\r]" }                       # 输入字段/记录分隔符为 "," 或 "\n" 或 "\r"

FNR==NR { clients[$1]=1; next}                    # FNR==NR => 第一个文件;使用节点名称填充关联数组,其中索引==节点名称
        { delete clients[$1] }                    # 此时我们正在处理第二个文件;从 client[] 数组中删除(服务器)节点

END     { pfx="client "                           # 输出的第一部分是字符串 "client "
          n=asorti(clients,sorted)                # 按名称对我们的客户端节点进行排序
          for ( i=1; i<=n; i++ )                  # 循环遍历已排序数组的索引列表
              { printf "%s%s", pfx, sorted[i]     # 打印前缀和节点名称
                pfx=","                           # 对于数组项目2+,将前缀更改为 ","
              }
          printf "\n"                             # 以换行符结束输出
        }
' nodes.txt <(echo "${SERVERS}") >> "${outfile}"  # 将输出写入我们的新文件

注意<(echo "${SERVERS}") 允许我们以文件的形式将变量 ${SERVERS} 的内容传递给 awk 脚本。

使用不同的输入参数运行脚本:

$ split_nodes cn01
output file: cn01.cmd

$ cat cn01.cmd
server cn01
client cn02,cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10,cn11,cn12

$ split_nodes cn03,cn07
output file: cn03_cn07.cmd

$ cat cn03_cn07.cmd
server cn03,cn07
client cn01,cn02,cn04,cn05,cn06,cn08,cn09,cn10,cn11,cn12

脚本可以根据输入的不同格式进行修改(例如,输入“字符串”是否包含任何空格等),但现在我们假设用户提供的输入与问题中列出的类似。

英文:

Assumptions:

  • nodes.txt contains list of all possible nodes and, initially, all nodes are considered 'client' nodes
  • nodes.txt entries may reside on one or more lines, and if multiple nodes are on one line they are delimited by a comma (,)
  • input to the script is one or more node names, with multiple nodes delimited by a comma (,)

Sample nodes.txt contents:

$ cat nodes.txt
cn01,cn02,cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10
cn11,cn12

We'll look at an awk solution to strip 'SERVER' nodes from the 'CLIENT' list of nodes. We'll start by loading all nodes (nodes.txt) into a clients[] array. For the server nodes we'll delete the nodes from the clients[] array. And finally we'll print what's left in the clients[] array.

Example script:

$ cat split_nodes 
SERVERS=                                      # store out input node(s)

outfile=${SERVERS//,/_}.cmd                       # generate output file by replacing &#39;,&#39; with &#39;_&#39;
echo &quot;output file: ${outfile}&quot;

echo &quot;server ${SERVERS}&quot; &gt; &quot;${outfile}&quot;           # dump our &#39;server&#39; line to ${outfile}

awk &#39;
BEGIN   { FS=RS=&quot;[,\n\r]&quot; }                       # input field/record delimiter is &quot;,&quot; or &quot;\n&quot; or &quot;\r&quot;

FNR==NR { clients[$1]=1; next}                    # FNR==NR =&gt; first file; use node names to populate associative array where index==node name
        { delete clients[$1] }                    # at this point we are processing 2nd file; delete (server) node from client[] array

END     { pfx=&quot;client &quot;                           # first part of output is the string &quot;client &quot;
          n=asorti(clients,sorted)                # sort our client nodes by name
          for ( i=1; i&lt;=n; i++ )                  # loop through list of indexes from the sorted array
              { printf &quot;%s%s&quot;, pfx, sorted[i]     # print prefix and node name
                pfx=&quot;,&quot;                           # for array items 2+ change prefix to &quot;,&quot;
              }
          printf &quot;\n&quot;                             # terminate output with a line feed
        }
&#39; nodes.txt &lt;(echo &quot;${SERVERS}&quot;) &gt;&gt; &quot;${outfile}&quot;  # dump output to our new file

NOTE: &lt;(echo &quot;${SERVERS}&quot;) allows us to pass the contents of the variable ${SERVERS} to the awk script in the form of a file.

Running the script with some different input parameters

$ split_nodes cn01
output file: cn01.cmd

$ cat cn01.cmd
server cn01
client cn02,cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10,cn11,cn12

$ split_nodes cn03,cn07
output file: cn03_cn07.cmd

$ cat cn03_cn07.cmd
server cn03,cn07
client cn01,cn02,cn04,cn05,cn06,cn08,cn09,cn10,cn11,cn12

The script could be modified a good bit to take into consideration different formats for the input (eg, does the input 'string' include any white space, etc), but for now we'll assume the user provides input similar to what's listed in the question.

huangapple
  • 本文由 发表于 2020年1月6日 02:49:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/59603123.html
匿名

发表评论

匿名网友

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

确定