I have a simple script that is not working as expected:
#!/bin/bash
CRONFILE="/tmp/temp_cron"
crontab -l > "$CRONFILE"
grep "* * * * * /usr/local/bin/temp_cpu_gmetric.sh" /tmp/temp_cron
CRONCONT=`grep "* * * * * /usr/local/bin/temp_cpu_gmetric.sh" /tmp/temp_cron`
echo $CRONCONT
My crontab consists of one line, which is * * * * * /usr/local/bin/temp_cpu_gmetric.sh
-- exactly the thing being grep'd for. The first instance of grep returns one instance of this character string, as expected.
However, the echo $CRONCONT
instance returns with * * * * *
replaced by a whole bunch of stuff. This includes the contents of the directory from which the script is run, but also many other things that I don't immediately know the location of.
If I remove * * * * *
from my cronfile, the problem goes away, but I can't figure out why it shouldn't work with the * * * * *
present.
Basically one problem is that *
is a meta-character for grep
. If you want to search for *
you should quote it like \\*
. To see why your pattern had worked out, see: this comment by chepner .
Or replace the grep
command with fgrep
(search for exact string.
The other problem is, that *
is a meta-character for shell expansion, so you should do echo "${YOURVARIABLE}"
.
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.