Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can bash grep "*" instead of "\*"?

Tags:

bash

How can I grep asterisk * inside of a text file? Here is my code :

if grep -F "\#$minute $hour $date $month $day $cr_user $comm" filecron; then
        echo "$minute $hour $date $month $day $cr_user $comm" > /dev/null 2>&1
        echo "data exist"

elif [[ -z $minute || -z $hour ]]; then
        echo "#$minute $hour $date $month $day $cr_user $comm" > /dev/null 2>&1
        echo "nothing to input"
        echo "" >> filecron

elif [[ $stat == "inactive" ]]; then
        echo "\#$minute $hour $date $month $day $cr_user $comm" >> filecron
        echo "input data inactive to filecron"

else
        echo "$minute $hour $date $month $day $cron_user $comm" >> filecron
        echo "input data active to filecron"

fi

Here is filecron contain

#30 07 03 09 RAB root bash /media/data/once.sh > /dev/null 2>&1
#* * * * * root bash /media/data/looping.sh > /dev/null 2>&1
#56 * * * * root bash /media/data/minutely.sh /dev/null 2>&1
#43 * * * * root bash /media/data/hourly.sh > /dev/null 2>&1
#54 11 * * * root bash /media/data/daily.sh /dev/null 2>&1
#00 12 * * RAB root bash /media/data/weekly.sh > /dev/null 2>&1
#30 20 15 * * root bash /media/data/monthly.sh > /dev/null 2>&1
#45 21 01 09 * root bash /media/data/yearly > /dev/null 2>&1
#15 16 31 08 MIN root bash /media/data/other.sh > /dev/null 2>&1
#30 07 * * * root bash /media/data/daily2.sh > /dev/null 2>&1
#30 10 * * * root bash /media/data/daily3.sh > /dev/null 2>&1
#45 17 17 * * root bash /media/data/monthly2.sh > /dev/null 2>&1
#00 18 * * SAB root bash /media/data/weekly2.sh > /dev/null 2>&1

#30 07 03 09 RAB root bash /media/data/once.sh > /dev/null 2>&1
#* * * * * root bash /media/data/looping.sh > /dev/null 2>&1
#56 * * * * root bash /media/data/minutely.sh /dev/null 2>&1
#43 * * * * root bash /media/data/hourly.sh > /dev/null 2>&1

But grep doesn't work if $minute or $hour or $month or $day or $date is * So, how do I do this?

like image 852
Lazuardi N Putra Avatar asked Mar 19 '23 13:03

Lazuardi N Putra


1 Answers

If you don't have any regular expression patterns, and just want to match the string exactly, use the -F option to grep (or the older fgrep command):

if grep -F "#$minute $hour $date $month $day $cr_user $comm" filecron; then
like image 150
Barmar Avatar answered Apr 01 '23 16:04

Barmar