2

I am trying to create a script that will parse the /var/log/secure file for failed login attempts for today's date only and redirect those attempts into a separate file so I can view those attempts only in that file. I am having a problem with getting the script to work with the date command output. This is my script:

#!/bin/bash
mydate=$(date '+%D')
mytime=$(date '+%H:%M')
monthday=$(date '+%b %d)
error=$(grep Failed /var/log/secure | grep $monthday >> ~/logs/failed.log

With this code, it will not work. However, if I hard code the date in like this:

error=$(grep Failed /var/log/secure | grep 'Feb 28' >> ~/logs/failed.log

it works.

How can I make it so that the $monthday is the thing grep'd?

1 Answer 1

3

You're missing quotes:

grep Failed /var/log/secure | grep "$monthday" >> ~/logs/failed.log
# .................................^.........^

Without the quotes, grep will see 2 arguments -- grep will ignore stdin and search for the string "Feb" in the file "28". I see an error "grep: 28: no such file or directory"

2
  • Thank you! I knew I was missing something. I even tried '$someday' earlier. LOL
    – user60618
    Feb 28, 2014 at 21:41
  • also, you need to use the double quotes as shown above, in single quotes the variable will not expand Mar 1, 2014 at 4:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .