Friday, April 25, 2008

Displaying chess board on the screen


Small Chess Board


#!/bin/bash
# SCRIPT : smallchessboard.sh
# PURPOSE : Prints small chess board on the screen.

clear
for (( i=1 ; i<=8 ; i++ ))
do

for (( j=1 ; j<=8 ; j++ ))
do

if [ `expr $(($i+$j)) % 2` -eq 0 ]
then
echo -e -n "\033[47m " # White background
else
echo -e -n "\033[40m " # Black background
fi

done
echo # move to next line

done

echo -e "\033[0m" # Restores color settings.


OUTPUT:


Big Chess Board


#!/bin/bash
# SCRIPT : bigchessboard.sh
# PURPOSE : Prints big chess board on the screen.

clear
a=4

for (( i=1 ; i<=8; i++ ))
do

for (( j=1 ;j<=2; j++ )) # prints same line twice
do
tput cup $a 15 # moves cursor to LINE COLUMN

for (( k=1 ; k<=8; k++ ))
do

c=`expr $((i+k)) % 2`

if [ $c -eq 0 ]
then
echo -e -n "\033[40m " # Black background
else
echo -e -n "\033[47m " # White background
fi

done
let a=a+1

done

done

echo -e "\033[0m" # Restores color settings
read key # Waits for enter


OUTPUT:


Tuesday, April 1, 2008

How can get nth line from a file

  1. time head -5 emp.lst tail -1
    It has taken time for execution is
    real 0m0.004s
    user 0m0.001s
    sys 0m0.001s
    or
  2. awk 'NR==5' emp.lst
    It has taken time for execution is
    real 0m0.003s
    user 0m0.000s
    sys 0m0.002s
    or
  3. sed -n '5p' emp.lst
    It has taken time for execution is
    real 0m0.001s
    user 0m0.000s
    sys 0m0.001s
    or
  4. using some cute trick we can get this with cut command
    cut -d “
    “ -f 5 emp.lst
    # after -d press enter ,it means delimiter is newline
    It has taken time for execution is
    real 0m0.001s
    user 0m0.000s
    sys 0m0.001s

    Analysis: comparing above commands 'head' command taken maximum time
    because it pipes the output to tail command. piping consumes some
    time.
    Next “awk” command has taken greater time ,because 'awk' not only
    a command, it is a programing language too.

When not to use shell scripts

· Resource-intensive tasks, especially where speed is a factor (sorting, hashing,
etc.)Procedures involving heavy-duty math operations, especially floating
point arithmetic, arbitraryprecision calculations, or complex numbers
(use C++ or FORTRAN instead)

· Cross-platform portability required (use C or Java instead) Complex applications, where structured programming is a necessity (need type-checking of variables, function prototypes, etc.)

· Project consists of subcomponents with interlocking dependencies Extensive file operations required (Bash is limited to serial file access, and that only in a particularly clumsy and inefficient line-by-line fashion)

· Need native support for multi-dimensional arrays
· Need data structures, such as linked lists or trees
· Need to generate or manipulate graphics or GUIs
· Need direct access to system hardware
· Need port or socket I/O
· Need to use libraries or interface with legacy code