Friday, May 3, 2013

Shell scripts

1)Arithmetic comparison and arithmetic addition
 s=1;
while( [ $s -lt 100 ] )
do
echo $s;
s=$[$s + 2];
done

2)Read  input
read input
echo "Welcome $input"

3)Display the first positional parameter
echo "Welcome $1"

4)Display the natural number from 1 to 50
for i in {1..50}do
echo $i;
done

5)Usage of if and arithmetic comparison operatorsread a;
read b;
if([ $a -lt $b ]) then
echo "X is less than Y";
fi
if([ $a -gt $b ]) then
echo "X is greater than Y";
fi
if([ $a -eq $b ]) then
echo "X is equal to Y";
fi


6)Usage of string comparison operators.
read input;
if ([ "$input" == 'y' ] || [ "$input" == 'Y'  ]) then
echo "YES";
fi
if ([ "$input" == 'n' ] || [ "$input" == 'N' ]) then
echo "NO";
fi


7) Usage of logic operators and if condition.

read a;
read b;
read c;
if ( [ $a -eq $b ] && [ $b -eq $c ] ) then
echo "EQUILATERAL"
elif ( [ $a -eq $b ] || [ $b -eq $c ] || [ $c -eq $a ] ) then
echo "ISOSCELES"
else
echo "SCALENE"
fi


8)Below script shows the usage of bc to perform arithmetic operations. Scale = 4 directs bc to compute upto 4 decimal places  and printf rounds the result to 3 decimal places.  a is an arithmetic expression sent as an input to the script E.g. 2+2-5*6/4*7

read a;
b=`echo "scale=4;$a"|bc`
printf %.3f $b;


9)Below script takes n integers which is first input and then all the integers one by one .The task is to calculate  the average . The script combines printf to round the result to 3 places and bc to calculate the average upto 4 decimal places . While loop is used to calculate  the sum of numbers by iterating exactly n times.

read n;
i=0;
s=0;
while ([ $i -lt $n ])
do
read input;
s=$[$s+$input];
i=$[$i+1];
done
average=`echo "scale=4;$s/$n"|bc`;
printf %.3f $average;

10) Display the 3rd character of each line given as input .

cut -c 3
E.g. echo "Hello World"|cut -c 3 displays l as output .
cut -c 2,7 will display the 2nd and 7th  character of the lines given as input.
cut -c 2-7  will display from 2nd to 7th  character of the lines given as input.
cut -c 13-  will display from 13th character to the end of the line.

11) Display first 3 fields of input delimited by tab. TAB is the default delimiter for cut.
f specifies the fields . Here, first to third field are displayed.
 cut  -f 1-3
cut -d " " -f 4 displays fourth field delimited by single space. cut -d " " -f 1-3 displays first to third  field delimited by single space.
cut -f 2-  displays second to last  field delimited by tab.

12)Below script displays first 20 lines of a file.

i=1;
while ( [ $i -lt 21 ])
do
read input;
echo $input;
i=$[$i+1];
done


head -c 12 . Displays first 12 characters of a file.
head -n 20 . Displays first 20 lines of file
head -n 20|tail -n +12 . Displays 12th to 20th line of file
tail -n -20 . Displays last 20 lines of file.
tail -c 20 . Displays last 20 characters of a file.


13)If you want to write  the name of all the Queue Manager  to a single file to create a stop/start script for WebSphere MQ
dspmq|cut -d'(' -f1|cut -d')' -f1 > strmq.sh

14)
Removing control M characters from several files at a time in UNIX platforms
find . -name *.sh -exec perl -pi -e 's/^M//g' {} \;

Make sure to type control M character , do not copy and paste control M character .
You have to type control M by pressing ctrl v and ctrl m one after another

then check again for control M character with the below command
 find . -name *.sh -exec grep -l "^M" {} \;


15)
If you want to remove control M character from a single file
perl -pi -e 's/^M//g'  FILE NAME


16)Below script replaces ( to [ and ) to ]. Take a note that we have only used -p not -pi . When you use i , command expects you to provide an input file name .

 perl -p -e 's/\(/\[/g'  |perl -p -e 's/\)/\]/g'


17)Replace 2 or more spaces with one .

perl -p -e 's/ +/ /g'

18) sort  . Sorts on first column
sort -r. Sorts on first column in reverse order
sort -n. Sorts on  numeric column. Only one numeric column is present.
sort -n -r . Sorts on numeric column in reverse order. Only one numeric column is present


19)Below script converts the input
A 25 27 50
B 35 37 75
C 75 78 80
D 99 88 76
 
to output
 
A 25 27 50 : FAIL
B 35 37 75 : FAIL
C 75 78 80 : B
D 99 88 76 : A 
 
awk 'BEGIN{} {sum=$2+$3+$4;mean=sum/3;if( mean >= 80 ) print 
$1,$2,$3,$4,":","A"; else if( mean >= 60 )print $1,$2,$3,$4,":","B"; 
else if( mean >= 50 ) print $1,$2,$3,$4,":","C"; else print 
$1,$2,$3,$4,":","FAIL";} END{}'  

20)
Below script converts the input 
A 25 27 50
B 35 37 75
C 75 78 80
D 99 88 76 
 
to 
 
A 25 27 50;B 35 37 75
C 75 78 80;D 99 88 76 
 
awk '{if(NR%2==0) printf $0"\n"; else printf $0";";}'
 
Note that when you change the printf to print the output changes to 
A 25 27 50;
B 35 37 75

C 75 78 80;
D 99 88 76
 
 
8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
 
https://www.thegeekstuff.com/2010/01/8-powerful-awk-built-in-variables-fs-ofs-rs-ors-nr-nf-filename-fnr/?ref=binfind.com/web
 
 
21) 
grep  "[t,T]he " 
searches for the line containing string "the"

grep -v "[t,T]hat " displays lines which do not contain that .

grep -e "[t,T]he " -e "[t,T]hat " -e "[t,T]hen " -e "[t,T]hose "  searches for the words the, that ,then , those

 

22) Copy a file specified on the input prompt from one location to another .If the file already exists in the other location ,then rename the file using date as suffix and then copy.
"$ cat testscript.sh
echo "Enter the name of file"
read file
echo $file
Date=`date|cut -f1 -d ' ' `
if(test -e $file)
then
mv $file $file$Date
cp TEST1/$file ./
echo $?
else
cp TEST1/$file ./
fi

$ echo $?
0

if [ $? -eq 0 ]
then
echo "Success"
fi


23)Script to check whether list of files written in a FILE 'TEMP1.txt' exist in directory inside which the script is run

for file in `cat filelist.txt`
do
if(test -e $file)
then
c=0
else
echo $file >> ListOfFiles.txt
fi
done

24)Find files containing a string and then replace string with new string in those files

find . -type f -exec cmd.sh "jdbc\/SomeDB" {} \;
OR
find . -type f -exec ./cmd.sh "jdbc\/SomeDB" {} \;

##contents of cmd.sh
grep $1 $2
if [ $? -eq 0 ]
then
echo $2 >> filenames.txt
fi
##Below script to replace
for file in `cat filenames.txt`
do
sed 's/old_string/new_string/g' $file > $file.new
mv $file.new $file
done

25)Merge files line by line in UNIX
 paste file1 file2 > file3

Generic Numeric Sorting on second column seperated by tab in descending order
sort -t$'\t' -k2 -g -r
Generic Numeric Sorting on second column seperated by | in descending order
sort -t'|' -k2 -g -r
Generic Numeric Sorting on second column seperated by | in ascending order
sort -t'|' -k2 -g

Remove duplicates in standard input
Uniq

Counts and displays number of consecutive duplicates
uniq -c|xargs -l

Counts and displays number of consecutive duplicates and ignore cases
uniq -ic|xargs -l

Convert newline to tab from a standard input
awk 'BEGIN{ORS="\t";}{print;} END{}'

No comments: