1

I'm in my first semester of BASH Scripting and I am having some difficulty. I've taken other programming courses like C++ or Java but the syntax of Bash is killing me. I'd love some advice on this problem. I need to do the following:

  • Extract Today's data from /var/log/secure file
  • Check to see if I have a directory called 'mylogs'
  • If I don't - then create one
  • Check to see if you already have a file matching the current day, month and hour in the ‘mylogs’ directory.
  • If you do, echo to the screen “File exists, nothing written to my log”, and exit. If it doesn’t exist then write today’s data from /var/log/secure to your ‘mylog-month-day-hour’ file. Example (February, 4th at 2pm) output: mylog-02-04-14

I just need help with the syntax portion of the script.

Thanks - I'd love any websites helping out in BASH as well.

2
  • I recommend you narrow your question down to something with a definitive answer. Commented Feb 25, 2013 at 0:12
  • 1
    this really sounds like an assignment list for your class. whatHaveYouTried.com ? Good luck. Commented Feb 25, 2013 at 0:23

2 Answers 2

2
  • Extract Today's data from /var/log/secure file

You could do this ...

grep "^Feb 24" /var/log/secure
  • Check to see if I have a directory called 'mylogs' and If I don't - then create one

You can do this ...

test -d mylogs || mkdir mylogs
  • Check to see if you already have a file matching the current day, month and hour in the ‘mylogs’ directory. (Assuming file names are of the format DDMMHH)

    test -e mylogs/`date +%d%m%H` && echo "I already have a file"

  • If you do, echo to the screen “File exists, nothing written to my log”, and exit. If it doesn’t exist then write today’s data from /var/log/secure to your ‘mylog-month-day-hour’ file. Example (February, 4th at 2pm) output: mylog-02-04-14

Eh you should get the idea by now. You can tackle this one now I think ;) A helpful command to know is man -k <keyword>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - my only question is (I've been researching this) is that I need it be run everyday - so running Grep Feb 24 wouldn't work - so is there a variable for just date?
For dates with single-digit day-of-month values, grep "^Feb 2" will match 2nd, and 20th-29th... could use e.g. "^Feb 2[^0-9]", or if you knew it was followed by a particular character, look for that e.g. "^Feb 2," or "^Feb 2 ".
1

First read some bash basics. Then, there are links describing your particular problem.

3 Comments

Thanks - and if I wanted to grab the date from /var/log/secure - would I use grep?
Yep, it's good searching tool. When my server was hacked I was sniffing for details with grep.
grep, sed, awk, cut, find, date, tr, test operators, >, <, | (I am probably leave a few out) are all commands you need to read the manpages for if you want to be proficient in write shell script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.