Basic Shell Scripting in BASH

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.


  • 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>


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

  • http://www.cyberciti.biz/faq/linux-howto-unzip-files-in-root-directory/
  • Check if a directory exists in a shell script
  • How to use Bash to create a folder if it doesn't already exist?
  • http://www.electrictoolbox.com/test-file-exists-bash-shell/
  • http://www.cyberciti.biz/tips/howto-linux-unix-write-to-syslog.html
  • 链接地址: http://www.djcxy.com/p/17498.html

    上一篇: cp命令的怪异行为

    下一篇: BASH中的基本shell脚本