Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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"

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.