I've been working on a script which takes the output of the yum-cron command & redirects it to a new file. This part is working. However, if there's no new updates & no errors an empty file is created. I decided to try to get the script to write a simple "no new updates" message to the most recently created file.
The script looks like this:
#!/bin/bash
# Only run if this flag is set. The flag is created by the yum-cron init
# script when the service is started -- this allows one to use chkconfig and
# the standard "service stop|start" commands to enable or disable the yum-cron.
if [[ ! -f /var/lock/subsys/yum-cron ]]; then
exit 0
fi
# Action!
/usr/sbin/yum-cron 2>&1 >> /var/log/yum-cron/yumCronUpdate-"$(date +"%F %T")"
# sleep in case yum-cron actually finds something to do?? Unsure if necessary??
sleep 30s
value=$(find /var/log/yum-cron/ -mtime -1 -size 0 -type f -iname "yumCronUpdate-2022*") | echo "Updates checked, no updates found on $(date + "%F +%T")" >> "$value"
I know that the script works up through line 12. In this form I get the error "line 13: No such file or directory." If I run line 13 from the terminal the message is appended to the most recent "yumCornUpdate" file - so it seems to actually work. My guess is that the $value variable isn't getting set as echo "$value"
doesn't produce an output after the script has run.
If I modify the code to this:
#!/bin/bash
# Only run if this flag is set. The flag is created by the yum-cron init
# script when the service is started -- this allows one to use chkconfig and
# the standard "service stop|start" commands to enable or disable the yum-cron.
if [[ ! -f /var/lock/subsys/yum-cron ]]; then
exit 0
fi
# Action!
exec /usr/sbin/yum-cron 2>&1 >> /var/log/yum-cron/yumCronUpdate-"$(date +"%F %T")"
# sleep in case yum-cron actually finds something to do?? Unsure if necessary??
sleep 30s
echo "Updates checked, no updates found on $(date + "%F +%T")" >> $(find /var/log/yum-cron/ -mtime -1 -size 0 -type f -iname "yumCronUpdate-2022*")
I get the error "line 13: $(find /var/log/yum-cron/ -mtime -1 -size 0 -type f -iname "yumCronUpdate-2022*"): ambiguous redirect
I'm not skilled at bash scripting, this is my first time (I'm used to PowerShell on Windows). I looked around at these topics as well:
Redirect bash output to file within the bash script
Getting an "ambiguous redirect" error
How do I redirect output to a variable in shell?
Any help would be appreciated!