I have a problem with "if statement" inside while read loop.\\n
Example 1:
81 function processconfig2()
82 {
83 pattern='^monthly'
84 while read line
85 do
86 if [[ $line =~ $pattern ]]; then
87 echo $line
88 fi
89 done < /etc/logrotate.conf | awk '/^\/var\/log\/wtmp/, /^}/'
90
91 exit 0
92 }
Output is empty.\\n After adding after line 89 echo $line:\\n
81 function processconfig2()
82 {
83 pattern='^monthly'
84 while read line
85 do
86 if [[ $line =~ $pattern ]]; then
87 echo $line
88 fi
89 echo $line
90 done < /etc/logrotate.conf | awk '/^\/var\/log\/wtmp/, /^}/'
91
92 exit 0
93 }
I've got proper output (condition at line 86 is processed properly):
/var/log/wtmp {
monthly
monthly
create 0664 root utmp
minsize 1M
rotate 1
}
Could someone explain to me this strange behavior, please.
Your problem is that your awk
filter is on the output of your read loop. So when you don't echo the whole file you don't output enough for awk to match so it drops all output.
Try without the awk
filter to see what I mean.
Alternatively move the awk filter to before the read loop (I imagine this was the idea anyway).
pattern='^monthly'
while read line
do
if [[ $line =~ $pattern ]]; then
echo $line
fi
done < <(awk '/^\/var\/log\/wtmp/, /^}/' /etc/logrotate.conf)
Another working example:
81 function processconfig2()
82 {
83 pattern='^monthly'
84 content=`awk '/^\/var\/log\/wtmp/, /^}/' /etc/logrotate.conf`
85 while read line
86 do
87 if [[ $line =~ $pattern ]]; then
88 echo $line"twice"
89 fi
90 done <<< "$content"
91
92 exit 0
93 }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.