Friday, June 27, 2008

Shell Scripting: Using date within report/log file names

When you write a shell scripts you need to create filename with date in it. For example instead of log file name "traces.log", you can create a filename called "traces-Jun-27-8.log".

The date in file will make it easy to find out all logs or reports. You can display the current date and time in the given FORMAT using date command. If you just type date command it will display in standard FORMAT:

$ date
Output:
Fri Jun 27 11:43:25 IST 2008

To display date in MONTH-DAY-YEAR format you need to use date command as follows:

$ date +"%b-%d-%y"

Jun-27-08

As you can see I have used FORMAT as follows

date +"FORMAT"

Where, FORMAT can be any one of the following:

  • %a : Abbreviated weekday name (Sun..Sat)
  • %b : Abbreviated month name (Jan..Dec)
  • %B : Full month name, variable length (January..December)
  • %d : day of month (01..31)
  • %e : day of month, blank padded ( 1..31)
  • %H : 24 hour format (00..23)
  • %I : 12 hour format (01..12)
  • %j : day of year (001..366)

First obtained date:
$ NOW=$(date +"%b-%d-%y")

Create a file with date in filename
$ LOGFILE="log-$NOW.log"

Display filename:
$ echo $LOGFILE

You can use first two commands in script.

Thursday, June 26, 2008

BASH TIP: Script variables

Bash has some useful builtin variables. Here is a list of a few.

$$ : The PID of the script
$? : The exit status of the last command
$# : The number of command line arguments passed to script
$! : The PID of the command that has just been executed (and backgrounded)

Local variable in BASH (Setting a variables for one command only)

If you want to set a variable for one command only, you can always write the variable setting first, immediately followed by the simplecommand (don't write any ; after the assignment). This will result in the variable being set for that command only, but keeping the old value (if it had one) after the command has been executed.

As an example, if you execute the code

MANPATH=/usr/man:/usr/local/man man test

will only look for the manual for test in directories /usr/man and /usr/local/man. This regardless of what value MANPATH had before the call.

Super Bash Tricks

I'll start off this blog with some good bash shell tricks. You'll be the envy of your friends with these up your sleeve.

For the uninitiated, bash is the default shell in many Linux distros, including Fedora, Ubuntu, Redhat etc. If you use a Linux based OS, then chances are that you are using bash. For this reason, I outline below a few common annoyances, and the simple ways to overcome them.

1. Lost bash history

If you have a terminal open and are typing commands, then open another one and use that for a while, the new terminal won’t remember any of the commands typed in the first one. In addition, closing the first terminal, and then the second will overwrite any of the commands typed in the first terminal. Doubly annoying! This happens because the bash history is only saved when you close the terminal, not after each command. To fix it:


Edit your .bashrc (for beginners, any file starting with a . is hidden - they contain user preferences.)

nano ~/.bashrc

No need for a sudo here Ubuntuers, this is your own file, not a system setting. I like nano, but it’s up to you, choose gedit, kate, mousepad, vi or emacs as you wish.add the lines

shopt -s histappend

PROMPT_COMMAND=history -a;

And save. (control - O to write out. ^ means control in nano and other software, so the bottom of the editor does actually make sense to beginners! ^X to exit.) This makes bash append history instead of overwriting it, and makes it so that each time the prompt is shown it writes out all the history.

2. Stupid spelling mistakes

Add


shopt -s cdspell


to your .bashrc again. This will make sure that spelling mistakes such as ect instead of etc are ignored.

3. Duplicate entries in bash history


I often type cd .. multiple times in a row, when I then press UP to go back to earlier commands I don’t want to be reminded of my earlier inelegant excursions around the file system.
Add


export HISTCONTROL="ignoredups"


to .bashrc again.
Even better, add


export HISTIGNORE="&:ls:[bf]g:exit”


This will ignore duplicates, as well as ls, bg, fg and exit as well, making for a cleaner bash history.

4. Multiple line commands split up in history


Add


shopt -s cmdhist


to .bashrc, this will change multiple line commands into single lines for easy editing.


5. A couple of neat extras suggested by commenters


Press control + R in bash, then start typing and you can search through your past commands much easier than just pressing UP 300 times…
Alternatively, use


history grep "foo"


to search through your history - “foo” is the thing you are searching for. (Thanks to Ally)


cd -


goes to the last directory you were in - useful if you want to go somewhere to change something, then need to quickly flip back again.

Conclusion So, there are a few tips to get your bash history more manageable - if you have any extra tips, add them to the comments and I’ll add them to the main article!

Windows / Mac / or Linux?

Windows, Mac, or Linux is the question that comes to mind when people go to buy new computers. Now, which one should you use? If you want a bitten apple, an easy-to-use OS with no viruses, or want high-tech graphics editing, GET A MAC!!!!! If you want a semi-buggy OS with good network connectivity and a great range of software (but with 144,000 virus out to get it), get Windows. If you want to get the most secure OS in the world, get Windows Vista. It's more secure than any other OS because its restrictions are so great that nothing goes in and nothing goes out. If you're an internet user, Vista is not right for you. If you are an avid developer, want a FREE OS or if you just want a REALLY REALLY REALLY wide range of choices for an OS (there are over 500 different distributions of this OS), then Linux is right for you.

Personally, I prefer Linux. I'm into telecom software development and they're right for me. However, for daily use, I use Windows XP because we have to do a lots of documentation also

My First Post!

(Just setting this thing up)

I ...
-am happy in my Family
-live in Delhi
-work as a Test Manager
-love to work in UNIX enviorment