Files are copied only once during a cycle.

I check conditional check logs and copy to another folder. The while loop executes for the first iteration. At the next iteration, the copy of the file does not work. here is my code.

current_time=$(date "+%Y.%m.%d-%H.%M.%S")
tail -n 0 -F hive-server2.log | \
while read LINE
do
if [ `echo "$LINE" | grep -c "DROP" ` -gt 0 ]
then
  AuditTypeID=14
  QueryResult="$(grep -oEi 'DROP TABLE [a-zA-Z][a-zA-Z0-9_]*' hive-server2.log | sed -n \$p)"
echo -e "$QueryResult" >/dev/null < op.txt
cp op.txt op/op.txt.$current_time
fi
done

In the first iteration, the output file is created and stored in the op directory. In the next iteration, the file is not created.

Expected result: for each iteration, a new file should be created in the op directory.

Any help would be appreciated.

+4
source share
1 answer

Hope this helps you.

tail -n 0 -F hive-server2.log | \
while read LINE
do
if [ `echo "$LINE" | grep -c "DROP" ` -gt 0 ]
then
  current_time=$(date "+%Y.%m.%d-%H.%M.%S")
  AuditTypeID=14
  QueryResult="$(grep -oEi 'DROP TABLE [a-zA-Z][a-zA-Z0-9_]*' hive-server2.log | sed -n \$p)"
  echo -e "$QueryResult" > op/op.txt.$current_time > /dev/null 2>&1
  #cp op.txt op/op.txt.$current_time
fi
done
+1
source

Source: https://habr.com/ru/post/1695637/


All Articles