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!
CodePudding user response:
This code is creating a pipeline.
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"
Basically:
- The commands on the lefthand and righthand side get spawned in their own subshells. This is why you get no output from
echo "$value"
afterwards, the contents of the subshell are not visible from the parent process. - The output of the lefthand side is connected to the input of the righthand side...except that
value=$( command )
doesn't output anything, and even if it didecho
doesn't read from its standard input so that output wouldn't be used at all.
Rather than trying to capture the file in a variable and reuse it, find -exec
can detect the files and print things into them for you at the same time.
find /var/log/yum-cron/ -mtime -1 -size 0 -type f -iname "yumCronUpdate-2022*" \
-exec /bin/bash -c 'echo "Updates checked, no updates found on $1" >> "$2"' \
_ "$(date "%F %T")" {} \;
For each file matching the criteria, this command spawns a small bash script that prints your message and the current date to the matched file.