Command assigned to a variable is failed or not having any data - error

Hi,

My command is getting stuck while running it.

observed that the grep command doesn't returned any data ($? was 1) and it failed.
This command is assigned into the variable and used in other command as script progresses. To continue the script output, i have to press ^C twice and script continues for the loop.

Break didn't worked on this and have to press ^C twice.

full code

pdate=` { date +%Y%m%d; for d in Mon Tue Wed Thu Fri; do date +%Y%m%d -d "last $d"; done; } | sort | tail -5`
for d in $pdate; 
do 
filename=`grep $i /home/lim/updates/logs/log*$d* | cut -d ':' -f1 | uniq`; 
updatefile=$(awk -v var=$i  '1; $0 ~ var {exit}' $filename | awk '/upd_/ {a=$0} END{print a}' | cut -d ' ' -f3); 
completiontime=( $(grep $updatefile /home/lim/config/load_updates.hst | grep "Unpack complete" | cut -d " " -f5-7) ); 
echo  "$(tput sgr 0) Processing_date - $(tput setaf 2) $d $(tput sgr 0) UpdatePackage - $(tput setaf 6) $updatefile $(tput sgr 0) Processing_Time $(tput setaf 3)${completiontime[@]:0} $(tput sgr 0)" ;
done;
filename=`grep $i /home/lim/updates/logs/log*$d* | cut -d ':' -f1 | uniq`; updatefile=$(awk -v var=$i  '1; $0 ~ var {exit}' $filename | awk '/upd_/ {a=$0} END{print a}' | cut -d ' ' -f3 ); 
completiontime=( $(grep $updatefile /home/lim/config/load_updates.hst | grep "Unpack complete" | cut -d " " -f5-7 ) )

Command failed and $? = 1

grep $i /home/lim/updates/logs/log*$d* | cut -d ':' -f1 | uniq

tried workaround to get always true before passing in if condition

grep $i /home/lim/updates/logs/log*$d* | cut -d ':' -f1 | uniq || true 

if code i tried

---------- Post updated 10-24-16 at 01:46 AM ---------- Previous update was 10-23-16 at 10:31 PM ----------

The below code worked as of now:)

if [[ ! -f `grep $i /home/lim/updates/logs/log*$d* | cut -d ':' -f1 | uniq` ]]; then
 echo "File is missing for $d ";
 break;
 else
stmt;
fi

You have three grep s in your code - so which one is the culprit? Is it possible that some command is trying to read from the terminal becaues e.g. $filename is not set? Is $i defined, or empty?

Still it might be worthwhile to step back and reconsider the entire approach. The comments in your other thread, esp. this post and post#11 there still apply. The entire thing might be doable in one single awk script.

The below code fails as there will be no file having such pattern. $i is not available in the log files at start of the day till evening.in that case the below grep fails.
grep $i /home/lim/updates/logs/log*$d*
If this fails automatically the next two grep/awk fails.
Currently the working condition for me is using if and based on that proceed with script.