shell - 检查列表中保存的给定数字是否符合条件,并在shell中打印相应的输出

标签 shell error-handling conditional-statements rundeck

假设保存在变量中的数字列表。
Part=369,424,652,另存为$part=@option.partnumber@

curl --location -k --request GET 'https://myprojt.test9.abc.com/api/part/config=8594&select=parts,Action,refernceNumber&SalesID=333&partNumber=$part'

动态地,当我调用curl命令时,所有数字都是可变的。

假设是否缺少任何数字,并且预期输出为:

存在的零件:
零件数:369,操作数:新,引用编号:06
零件数:652,操作数:旧,引用编号:09

零件缺失:
零件数:424

尝试运行这个shell命令来运行它是RunDeck,请帮助,谢谢

最佳答案

您可以通过以下方式比较两个列表。我举个例子:

<joblist>
  <job>
    <context>
      <options preserveOrder='true'>
        <option name='mylist' value='494,192,111,214' />
      </options>
    </context>
    <defaultTab>nodes</defaultTab>
    <description></description>
    <executionEnabled>true</executionEnabled>
    <id>5a067bd8-3aa4-47eb-a522-19a5d9cdd797</id>
    <loglevel>INFO</loglevel>
    <name>ExampleList</name>
    <nodeFilterEditable>false</nodeFilterEditable>
    <plugins />
    <scheduleEnabled>true</scheduleEnabled>
    <sequence keepgoing='false' strategy='node-first'>
      <command>
        <exec>echo "starting"</exec>
      </command>
      <command>
        <fileExtension>.sh</fileExtension>
        <script><![CDATA[#!/bin/bash

# your list
VAR1=@option.mylist@

# hypotetical expected values
VAR2='494,192,111,214,999'

# for debug
echo "******************"
echo "Your list: $VAR1"
echo "Your expected values: $VAR2"
echo "******************"

Field_Separator=$IFS

# set comma as internal field separator for the string list
IFS=,

# store the missing values
a=()

# check two lists
for i in $VAR2; do
    found=
    for j in $VAR1; do
        if [ $i == $j ]; then
            found=1
            break
        fi
    done
    if [ ! $found ]; then
        a+=($i)
    fi
done

# now verify the possible missing values
if [[ -z "${a[*]}" ]]; then
   echo "All ok!"
else
  echo "missing values: ${a[*]}"
  # exit with error
  exit 1
fi

IFS=$Field_Separator]]></script>
        <scriptargs />
        <scriptinterpreter>/bin/bash</scriptinterpreter>
      </command>
    </sequence>
    <uuid>5a067bd8-3aa4-47eb-a522-19a5d9cdd797</uuid>
  </job>
</joblist>

当两个列表匹配时,返回Here;当两个列表不匹配时,返回here

关于shell - 检查列表中保存的给定数字是否符合条件,并在shell中打印相应的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61876513/

相关文章:

java - 不寻常的异常(exception)

Python 模式

error-handling - Go 中的自定义错误处理

c++ - 区分读取循环中的失败和文件结尾

crystal-reports - Crystal Reports 中的条件求和

sql - 是否可以在 Oracle SQL 中将 CASE 与 ANY() 一起使用?

Bash 在另一个变量中扩展变量

bash - 将输出从文件重定向到标准输出

c - 用 C 写一个 shell,不返回任何东西

java - 使用不同语言的最佳实践是什么?