在比较从 awk 填充的变量值时,for 循环不会中断 shell 脚本

for loop do not break in shell script while comparing variable value populated from awk

我正在尝试使用竖线分隔符验证文件中的数据。我仍然需要针对所有检查条件进行处理。当验证失败时,不应进行进一步验证。需要跳出一个循环。即使满足条件,我也无法确定为什么 break(或 exit)不起作用。

encoding_details 变量将从配置 table 中填充数据,并保存单个文件的字段详细信息。根据配置,这些详细信息集对于每个文件都是动态的。

1-memid-ID|2-memfirstname-NAME|3-memlastname-NAME

值代表列位置-列名-列类型

下面是正在进行的代码块。放置评论以突出问题

#!/bin/sh

#initiat variables here

retval=0 #this is a variable related with issue

#get configuration details from the table

encrpt_avoid_flag=Y

#extract all files in loop 
#get part of file_name to related configuration
#start validation for each file

#change directory to work on files now
cd  $directy_with_files;

echo "Initiating a process to read files..."
 
extractfilelist=`ls *.txt 2>/dev/null`; 
for file in $extractfilelist
do 
    #now get details of columns to be encrypted for a file 
    encoding_details=`<populated using sqlquery>`   
    
    if [[ -z $encoding_details ]]
    then
        # lets keep this output in a log file - need to work on it
        echo "WARNING: Encoding details not found for file: ${file}"
        echo "Skipping the file from validation scope..." 
    else 
        IFS="|" read -a encoding_column <<< $encoding_details 

        for column in "${!encoding_column[@]}"
        do 
            IFS="-" read -a encoding_column_detail <<< ${encoding_column[$column]}
            column_order=${encoding_column_detail[0]}
            column_name=${encoding_column_detail[1]}
            encoding_type=${encoding_column_detail[2]}
            
            awk -F'|' -v p_column_order="$column_order" -v p_encoding_type="$encoding_type" -v p_encrpt_avoid_flag="$encrpt_avoid_flag" '
            { 
                if ((p_encoding_type == "ID" || p_encoding_type == "NAME") && p_encrpt_avoid_flag == "Y") 
                { 
                    if ($p_column_order != "Blinded" && p_encoding_type == "NAME")
                    {
                        print 1; #this is an error condtion
                        exit;
                    }
                } 
            }' $file | 
            while read retval
            do
                if [ $retval -gt 0 ]
                then
                    echo "ERROR: encryption failed. Please find details below..."
                    echo "column name    : "$column_name 
                    echo "column position: "$column_order
                    echo "file_name      : "$file
                    exit #this exit do not works
                fi
            done
        if [ $retval -gt 0 ]
        then
            break #issue - this break do not work. while printing values inside the if block $retval shows 1 and output get printed as well
        fi
        done #end for column (encoding_column)
    fi #encoding details check end
    if [ $retval -gt 0 ]
    then
        break #issue - this break do not work here as well. while printing values inside the if block $retval shows 1 and output get printed as well
    fi
done #end of file loop
echo "finally : $retval" #and at then end value printed for retval variable is 0? it was 1 when error occoured

问题是你所有的 break 都依赖于 $retval 的值,这在各自的范围内不存在。 exit 似乎不起作用的原因是只有 while 循环退出,它存在于一个单独的进程中,该进程从 stdin 读取 awk 的输出。考虑以下最小示例:

#!/bin/sh

echo 1 | 
while read retval
do
    if [ $retval -gt 0 ]
    then
        echo "retval is greater than 0"
        exit # this exits only the while loop which is in a separate process
        echo "This does not print"
    fi
done

# retval does not exist in this scope
echo retval: [$retval]

这将为您提供以下输出:

retval is greater than 0
retval: []

如您所见,exit 正在退出 while 循环并且 $retval 在相关代码块之后没有任何值。