使用Shell脚本Shell编程和脚本编制在while循环中迭代来自sql数据的多行数据

问题描述

我正在尝试从bash变量(输出)中的表中检索数据并循环遍历以发送自动邮件。我正在使用以下脚本-


#!/bin/bash
output=$(sqlplus -S UN/PW <<EOF
SET ECHO OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET HEADING OFF
select h.run_seq_id || '^',h.email_id || '^',h.EMAIL_SUBJECT || '^',h.target_system_business_owner || '^',('abc') || '^' to_list,('xyz') || '^' cc_list
    -- b_row.html_body
from email_header h
where 1=1 
--and h.email_id = b_row.email_id    
--and h.email_id in (2501,2502)
order by h.run_seq_id,h.email_id;
exit;
EOF
)
echo "$output"
    while IFS= read -r line
    do  
        run_seq_id=$(echo $output | cut -d '^' -f 1) 
        echo 'col1:' $run_seq_id
        
        email_id=$(echo $output | cut -d '^' -f 2) 
        echo 'col2:' $email_id
        email_subject=$(echo $output | cut -d '^' -f 3) 
        echo 'col3:' $email_subject
        target_system_business_owner=$(echo $output | cut -d '^' -f 4)
        echo 'col4:' $target_system_business_owner
        to_list=$(echo $output | cut -d '^' -f 5) 
        echo 'col5:' $to_list
        cc_list=$(echo $output | cut -d '^' -f 6)
        echo 'col6:' $cc_list
done<<<$output

数据没有遍历所有行,而是一次又一次地读取第一行。 echo语句一次又一次地具有相同的数据,即它一次又一次地读取第一行,并且没有遍历所有数据。任何人都可以帮助我解决这个问题。我现在从脚本中省略了sendemail语句。预先感谢。

解决方法

为了保持行间距,我们必须引用:

done <<<"$output"

(如果您还写了echo $output,您会发现其中的区别。)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...