Sunday, September 23, 2007

UNIX tips and tricks


Printing a line in UNIX when you know the line number using sed?
 To print line 75 from file example_file  use the following sed command.

$> sed -n 75p example_file


How to access previous (history) comands in Unix ksh?

For those wanting to access history commands on Unix somewhat similar to doskey on DOS you can set the vi as your history editor with the set command.

$> set -o vi
$>


Once done you can use the vi editing commands to access the history commands. Esc (escape) + k to access previous commands.


Getting the current working directory to show up in the prompt (ksh)?
You have to use the $PWD variable here. Add the following line in your $HOME/.profile file.
export PS1='$PWD>'


Deleting Control-M (^M) characters from a file in UNIX using vi?
In vi do run the following command
:%s/^M//g
To get the ^M hold the control key, press V then M (Both while holding the control key) and the ^M will appear. This will find all occurances and replace them with nothing.
In order to avoid the Ctrl-M characters when using ftp to transfer files from windows to UNIX use the ASCII mode.


Usage of eval
Consider:
$> list_dir=' ls -l grep "^d" '
$> $list_dir
: No such file or directorygrep: No such file or directory
"^d": No such file or directory
$> eval $list_dir
drwxr-xr-x 3 biflpu20 other 512 Feb 7 16:39 inside_nihar
The shell evaluates variables after it has looked for metacharacters like
The command $> eval $list_dir works because the line is "rescanned" by the shell.



Error handling with piped commands
The default behaviour of a UNIX piped command is to return the status of the last command in a list piped commands.
set -o pipefail

No comments:

A TRIP TO BHANDUP PUMPING STATION - the birdwatchers paradise

"A Picture from Bhandup Pumping Station" I am a nature lover, and I often like to go birdwatching with my dad. We had read that &q...