Wednesday, October 3, 2012

Syntax for basic shell script constructs

  • if statement - can be used to perfroms a specific action/s based on the result of conditional expression
if [ a -eq b ]
then
.......
.......
else
........
........
fi
 
or
in one line
if [ a -eq b ]; then .......; .......; else ........; ........; fi
 
#Here ';' plays an important role. In unix ';' represents end of command line
 
ex:
$ if [ 1 = 2 ];then echo true;else echo false;fi
false
 
 
  • for statement - can be used to iterate through a list of values
 
array_list="1 2 3 4 5 6 7 8 9 10"
for i in $array_list
do
echo $i
done
 
in one line
array_list="1 2 3 4 5 6 7 8 9 10"; for i in $array_list; do echo $i; done
 
ex:
$ array_list="1 2 3 4 5 6 7 8 9 10"; for i in $array_list; do echo $i; done
1
2
3
4
5
6
7
8
9
10

  • while statement - can be used to read a list


any command that gives a list of values | while read a; do echo $a; done

$ ls t*|while read a; do echo $a; done
t
test
testif.ksh
tpt
try.ksh

  • To replace any search word on the fly in vi editor
to replace in whole file use Esc, then type :%s/findwors/replaceword/gc
This promts for confirmation every word it find and to replace

If you want to suppress promt remove c from the above command.

from a spefic line in file to specific line number
Esc then type :10,20s/findword/replaceword

Compare two files for specific word/s

Problem: We may have defined a set of functions used across project level, and assume that there is need to change/add/delete the functions as we move ahead.

For some reason, if your development and production copy may may not in sync. But you already added or changed the dev copy. Then, you want to make sure that you are missing any function/s that you really need.
In such cases we can use regular diff command - But the problem here is the output of diff may not be that readable. for a beginner like me.

So, here is another way of comparing two files no matter in what order they have the functions/keywords you need to compare.


echo "The function missing in new/modified copy if any"
grep "^search keyword" $PATH/$OLD_FILE_NAME | while read i; do echo %%%%$i%%%%% $(grep "$i" $PATH/NEW_FILE_NAME) ; done | grep "%%$"
echo "The result above usually should be empty"\

echo "Newly added functions are"
grep "^search keyword" $PATH/NEW_FILE_NAME | while read i; do echo %%%%$i%%%%% $(grep "$i" $PATH/$OLD_FILE_NAME) ; done | grep "%%$"

Tuesday, October 2, 2012

Compress and decompress files and ftp between servers

To compress files and sub directories present in root directory

tar -cvf [Target - Compressed File Name.desired extension] [Source - Directory to compress]

to ftp from [unix.source.server] to [unix.target.server]

Login to [unix.source.server]
cd to sourc dircetory from where you would want to ftp file/s
$ ftp [unix.target.server]
promts to login: enetr user-name
promts for password: eneter valid password
ftp$ binary -- To set the file transfer mode to binary
or
ftp$ ascii -- To set the file transfer mode to ASCII
ftp$ put [filename] or mput [pattern]-- to place one or more files respectively
ftp$ bye -- to come out successfully from ftp mode

Similarly we can use get / mget to get the files from remote server to the currently connected server


To decompress to the desired location

tar -xvf [Source - Compressed File Name.desired extension] [Target - Directory to which you want decompress files]