Showing posts with label Technical - Unix. Show all posts
Showing posts with label Technical - Unix. Show all posts

Monday, January 17, 2011

UNIX : The difference between [ ... ] and [[ ... ]] (The extended test operator)

The [[...]] is processed as part of the shell grammar whereas [...] is processed like any other command.
Operators and operands are detected when the command is read, not after expansions are performed. 
 
  1. The shell does not do word splitting or path-name generation inside [[...]].This allows patterns to be specified for 
    string matching purposes.
  2. Parameter expansion and command substitution is allowed.
  3. Prevents many logic errors in scripts. e.g [ "$x" = "TEST" ] can be written as [[ $x = "TEST" ]] the variables don't need to be quoted.
  4. The &&, ||, <, and > operators work within a [[ ]] test, despite giving an error within a [ ] construct.

Saturday, October 6, 2007

How to read leading spaces from a file in UNIX?

How do you retain leading spaces while reading from a file in UNIX? One way to do this is change the variable used to set the Internal Field Separator (IFS) in UNIX and set it to nothing.

ORIGINAL_IFS=$IFS
IFS=
while read LINE
do
...
...
...

done < $IN_FILE

IFS=$ORIGINAL_IFS

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

How to print a line of dashes in UNIX using the print command in ksh?

Normally when you try to print a line of dashes or any text starting with a dash e.g -3 using the 'print' command in an Unix korn shell script it will not print anything

$> print "--------------------------"

$>

To overcome this you can either use the -R option which will make print ignore all following options and escapes.

$> print -R "--------------------------"
--------------------------
$>

or use the print command with the double dash option print -- this tells the print command where -- stated that no more options follow.

$> print -- "--------------------------"
--------------------------
$>

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