Suggestions to get desired output

时间:2017-07-12 08:04:33

标签: bash shell ksh

Need your help with following script to get the desired output.

  • Check file exist and not empty ---working
  • read input from that file ---working
  • For each line in that file run below loop ---working

    for i in 01 02 03 04 05
    do
    query -se=$i "q blabla node='$line'" >/dev/null
    
  • My problem is , If the line is found in anyone of 01 02 03 04 05 then it shouldn't go to failed.lst, It should only be displayed in success.lst, Which is not happening with the IF condition I have written.

Suggest some inputs to achieve this without major changes.

    echo " enter file name "
    read file
    if [[ -f "$file"  &&  -s "$file" ]]
    then
        echo " file exist, and not empty "
        while IFS='' read -r line
        do
            echo "Querying --->"$line""
            for i in 01 02 03 04 05
            do
                query -se=$i "q blabla node='$line'" >/dev/null
                if [ $? -ne 0 ]
                then
                    echo "$line" >>failed.lst
                else
                    echo "$line" >>success.lst
                fi
            done
        done<"$file"
    else
        echo "File doesn't exist/empty"
    fi

1 个答案:

答案 0 :(得分:1)

如果希望对for-list中的所有值执行query,则可以使用此方法。

    while IFS='' read -r line
    do
        echo "Querying ---> $line"
        query_ok=1 # some false value
        for i in 01 02 03 04 05
        do
            query -se=$i "q blabla node='$line'" >/dev/null
            if [ $? -eq 0 ]
            then
                query_ok=0 # true
            fi
        done
        if [ ${query_ok} -ne 0 ]
        then
           echo "$line" >>failed.lst
        else
           echo "$line" >>success.lst
        fi
    done<"$file"

如果找不到i的所有值,则可以使用break

while IFS='' read -r line
do
    echo "Querying ---> $line"
    query_ok=1 # some false value
    for i in 01 02 03 04 05
    do
        query -se=$i "q blabla node='$line'" >/dev/null
        if [ $? -eq 0 ]
        then
            query_ok=0 # true
            break # do not test other values of i
        fi
    done
    if [ ${query_ok} -ne 0 ]
    then
       echo "$line" >>failed.lst
    else
       echo "$line" >>success.lst
    fi
done <"$file"

偏离主题:您还可以使用var来计算query成功的值的数量。