Need your help with following script to get the desired output.
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
答案 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
成功的值的数量。