Tuesday, April 22, 2014

Shell Scripting

========================================================================
sequence command
========================================================================
seq 1 10
seq 1 2 10 --> Generates odd numbers
seq 0 2 10 --> Generates even numbers
[root@ngdb ~]# seq 0 2 10
0
2
4
6
8
10
========================================================================
wc
========================================================================
--> wc -c--> character count
--> wc -l--> line count
--> wc -w--> word count

cat <filename> | wc -w --> Gives words in a file.
========================================================================
Head and Tail command
========================================================================
How to show only part of file, i.e from line 21 25
[root@ngdb ~]# cat nums.txt | head -25 | tail -5
21
22
23
24
25
[root@ngdb ~]#

to see first char of a word
========================================================================
[root@ngdb ~]# echo "hello" | head -c1
h[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]# echo "hello" | head -c3
hel[root@ngdb ~]#
[root@ngdb ~]# echo "hello" | head -c3 | tail -c2
el[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]# echo "hello" | head -c3 | tail -c3
hel[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]#
========================================================================
Cut command
========================================================================

[root@ngdb ~]# echo "hello" | cut -c2
e
[root@ngdb ~]# echo "hello" | cut -c1-4
hell
[root@ngdb ~]#
[root@ngdb ~]#
 echo "hello" | cut -c1,4
hl
[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]# echo "main:kill::fare:::" | cut -d":" -f1
main
[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]# echo "main:kill::fare:::" | cut -d":" -f2
kill
[root@ngdb ~]# echo "main:kill::fare:::" | cut -d":" -f3

[root@ngdb ~]# echo "main:kill::fare:::" | cut -d":" -f4
fare
[root@ngdb ~]#

========================================================================
trim command
========================================================================
[root@ngdb ~]# ls  -l
total 3910828
-rwxrwxrwx 1 root root        745 Sep 25  2012 !
-rwxrwxrwx 1 root root       1459 Sep 10  2012 anaconda-ks.cfg
drwxrwxrwx 2 root root       4096 Sep 10  2012 Desktop
-rwxrwxrwx 1 root root      12232 Apr  9  2013 epel-release-5-4.noarch.rpm
-rwxrwxrwx 1 root root      56384 Sep 10  2012 install.log
-rwxrwxrwx 1 root root       4069 Sep 10  2012 install.log.syslog
-rwxrwxrwx 1 root root 3997462528 Apr  9  2013 NGDB-OS-5U7_64bit_V3.iso
-rw-r--r-- 1 root root        292 Mar 10 12:31 nums.txt
drwxr-xr-x 2 root root       4096 Dec  9 16:24 old_sysfs_files
-rwxrwxrwx 1 root root    1227721 Apr  9  2013 php53-mbstring-5.3.3-1.el5_6.1.x86_64.rpm
-rw-r--r-- 1 root root     130686 Apr 12  2013 php-apc-3.1.9-3-mdv2011.0.x86_64.rpm
-rw-r--r-- 1 root root      40160 Apr 12  2013 php-apc-4.3.10_2.0.4-4mdk.x86_64.rpm
-rwxrwxrwx 1 root root     465536 Apr  9  2013 php-mbstring-5.3.3-22.el6.x86_64.rpm
-rwxrwxrwx 1 root root    1307354 Apr  9  2013 php-mbstring-5.5.0-0.20.201303201430.el5.remi.x86_64.rpm
-rwxrwxrwx 1 root root       4148 Feb 12  2013 settings.xml
[root@ngdb ~]#
[root@ngdb ~]# ls  -l| tr -s " "
total 3910828
-rwxrwxrwx 1 root root 745 Sep 25 2012 !
-rwxrwxrwx 1 root root 1459 Sep 10 2012 anaconda-ks.cfg
drwxrwxrwx 2 root root 4096 Sep 10 2012 Desktop
-rwxrwxrwx 1 root root 12232 Apr 9 2013 epel-release-5-4.noarch.rpm
-rwxrwxrwx 1 root root 56384 Sep 10 2012 install.log
-rwxrwxrwx 1 root root 4069 Sep 10 2012 install.log.syslog
-rwxrwxrwx 1 root root 3997462528 Apr 9 2013 NGDB-OS-5U7_64bit_V3.iso
-rw-r--r-- 1 root root 292 Mar 10 12:31 nums.txt
drwxr-xr-x 2 root root 4096 Dec 9 16:24 old_sysfs_files
-rwxrwxrwx 1 root root 1227721 Apr 9 2013 php53-mbstring-5.3.3-1.el5_6.1.x86_64.rpm
-rw-r--r-- 1 root root 130686 Apr 12 2013 php-apc-3.1.9-3-mdv2011.0.x86_64.rpm
-rw-r--r-- 1 root root 40160 Apr 12 2013 php-apc-4.3.10_2.0.4-4mdk.x86_64.rpm
-rwxrwxrwx 1 root root 465536 Apr 9 2013 php-mbstring-5.3.3-22.el6.x86_64.rpm
-rwxrwxrwx 1 root root 1307354 Apr 9 2013 php-mbstring-5.5.0-0.20.201303201430.el5.remi.x86_64.rpm
-rwxrwxrwx 1 root root 4148 Feb 12 2013 settings.xml
[root@ngdb ~]#

[root@ngdb ~]#
[root@ngdb ~]# echo -e "a b\t\tc d" | tr  "\t" " " |tr -s " "| cut -d" " -f3
c
[root@ngdb ~]#
[root@ngdb ~]#
========================================================================
TO COUNT THE WORDS WITH SUBSTITUTION
========================================================================
[root@ngdb ~]# echo "helllo-world-comms"| wc -w
1
[root@ngdb ~]#
[root@ngdb ~]#

[root@ngdb ~]# echo "helllo-world-comms"|tr "-" " "| wc -w
3
[root@ngdb ~]#
[root@ngdb ~]#
TO CHANGE THE CASE
echo "hello" | tr "[a-z]" "[A-Z]"
[root@ngdb ~]# echo "hello" | tr "[a-z]" "[A-Z]"
HELLO
[root@ngdb ~]#
TO COUNT THE VOWELS
-d--> delete
-c--> compliment
echo " hellow world of unix" | tr -dc "[aeiou]" | wc -c
[root@ngdb ~]# echo -n " hellow world of unix" | tr -dc "[aeiou]" | wc -c
6
[root@ngdb ~]#
========================================================================
paste command
========================================================================
[root@ngdb ~]# seq 1 6 > 1.txt
[root@ngdb ~]# seq 7 10 > 2.txt
[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]# cat 1.txt 2.txt
1
2
3
4
5
6
7
8
9
10
[root@ngdb ~]# paste 1.txt 2.txt
1       7
2       8
3       9
4       10
5
6
[root@ngdb ~]#
[root@ngdb ~]# paste -d" " 2.txt 1.txt
7 1
8 2
9 3
10 4
11 5
12 6
13
14
15
16
17
18
19
20
[root@ngdb ~]# paste -s 2.txt 1.txt
7       8       9       10      11      12      13      14      15      16     17       18      19      20
1       2       3       4       5       6
[root@ngdb ~]#
========================================================================
SORT COMMAND
========================================================================
SORT the file based on first column

[root@ngdb ~]# cat > emp.txt
guru pmp blt 789
tan  cem blr 098
mike OS mun  890890
Reeema Fin Mum 87123
Lagoo Fin Mum 1241



[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]# vi emp.txt
[root@ngdb ~]# sort emp.txt


guru pmp blt 789
Lagoo Fin Mum 1241
mike OS mun  890890
Reeema Fin Mum 87123
tan  cem blr 098
[root@ngdb ~]#
*********************************************************************************
SORT the  file based on second column
*********************************************************************************
[root@ngdb ~]# sort -k2 emp.txt


tan  cem blr 098
Lagoo Fin Mum 1241
Reeema Fin Mum 87123
mike OS mun  890890
guru pmp blt 789
[root@ngdb ~]#

sort -r emp.txt  --> Reverse order
sort -n -k4 emp.txt. --> numeric sort
sort -t ":" -k2 emp.txt --> with delimeter
sort -f emp.txt --> ignore case

sort emp.txt -o emp.txt --> To store the sorted details
*********************************************************************************
Command to find the employee who is drawing highest salary in emp.txt
*********************************************************************************
[root@ngdb ~]#
[root@ngdb ~]# sort -n -k4 emp.txt | tail -1 | cut -d" " -f1,4
mike
[root@ngdb ~]#

[root@ngdb ~]# cat emp.txt
guru pmp blt 789
tan  cem blr 098
mike OS mun  890890
Reeema Fin Mum 87123
Lagoo Fin Mum 1241



[root@ngdb ~]#
[root@ngdb ~]# sort -nr -k4 emp.txt | head -1 |tr -s " " | cut -d " " -f1,4
mike 890890
[root@ngdb ~]#
*********************************************************************************
For Unique values
*********************************************************************************
[root@ngdb ~]# sort -u emp.txt | tr -s " " | cut -d " " -f2

pmp
Fin
OS
Fin
cem
[root@ngdb ~]#
*********************************************************************************
COUNT THE NUMBER OF FILES IN THE DIRECTORY WITH FILE TYPES
*********************************************************************************
[root@ngdb ~]# ls  *.* | cut -d"." -f2 | sort | uniq -c
      1 1
      3 3
      1 5
      1 cfg
      1 iso
      2 log
      1 noarch
      4 txt
      1 xml
[root@ngdb ~]#
*********************************************************************************
TO LIST NUMBER OF FILES AND DIRECTORIES IN A FILE
*********************************************************************************
[root@ngdb ~]# ls -la   | cut  -c1 |sort | uniq -c
     28 -
     20 d
      1 t
[root@ngdb ~]#
[root@ngdb ~]#
*********************************************************************************
TO CHECK HOW MANY USERS HAVE LOGGED IN THE SYSTEM
*********************************************************************************
[root@ngdb ~]#
[root@ngdb ~]# who |cut -d" " -f1| sort | uniq -c
      2 ngdbprod
      5 saiUser
[root@ngdb ~]#
uniq -c --> Count
uniq -d --> display only duplicates
uniq -u --> display only uniques
========================================================================
cmp command Compare two files byte by byte and sstops when it encounters a difference
========================================================================

comm COMMAND--> Compare two sorted columns
=======================================================================================
comm file1 file2
[root@ngdb ~]# comm 1.txt 2.txt
1
        1
                2
                3
                4
5
6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
[root@ngdb ~]#
Column 1--> Colmn1-Colmn2
Column 2--> Colmn2-Colmn1
Column 3--> Colmn1 Intersection Colmn2
==================================================================================
tee command
==================================================================================
==================================================================================
grep command
==================================================================================
grep --> Supports only Basic REGEX expressions
egrep --> Support both Basic REgular expression and EXTENDED reg expression
fgrep --> Doesn't support REGEX

Basic REGEX                                 EXTENDED REGEX
    .   -->A char ? --> zero/one
[ ] --> Group +      --> one or more
^ --> Line starts with {m,n}  --> repeatative
$ --> Line ends with |    -->Alteration
\<  -->Words start with
\> -->Words ends with ( )  -->Overrode precedence
*   -->zero/more
*********************************************************************************
Select all blank lines in file
*********************************************************************************
--> "^ *$"
*********************************************************************************
Find the folders in folder
*********************************************************************************
--> ls -l | grep "^d"
[root@ngdb ~]#  ls -l | grep "^d"
drwxrwxrwx 2 root root       4096 Sep 10  2012 Desktop
drwxr-xr-x 2 root root       4096 Dec  9 16:24 old_sysfs_files
[root@ngdb ~]#

grep OPTIONS
 -i ignore case
 -c count no of matching lines
 -n display the matching numbers
 -v display the lines that doesn''t match
 -l display matching file names

 ----------------------------------------
 diplay emps working for sales and accounts
 egrep "(sales|accounts)" emp.txt
 --------------------------------------------
 ========================================================================
 find COMMAND --search a file based on its attributes

 syntax: find path -attributes -action
 attributes:
name
permission
mtime
size
type
actions:
print
ls
exec  (non-interactive execution)
ok   (interactive execution)
*********************************************************************************
==>Delete all the .tmp files in my login without my interaction
*********************************************************************************
[root@ngdb ~]#find ~ -type f -name "*.tmp" -exec rm {} ;
rm `find ~ -type f -name "*.tmp"`
*********************************************************************************
==> Command to move all the files whose size is greater than 1 GB to /tmp/BIG
*********************************************************************************
[root@ngdb ~]#find / -size +1G -type f -exec mv {}/tmp/BIG
=====================================================================================
&&  and ||
(CONDITIONAL COMMAND GROUPING)
*********************************************************************************


DELETE THE FILE IF IT EXISTS
*********************************************************************************
ls tmp.txt && rm tmp.txt



*********************************************************************************
CHECK THE FILE, CREATE IT IF DOESN'T EXITS
*********************************************************************************

ls tmp.txt && touch tmp.txt

****************************** LOOPS IN SHELL
******************************** COMPARE
|
       /\
while loop               /  \       String  Number
for loop     $a = $b   $a -eq $b     $a!= $b -ne
nested for loop -ge                                 -gt
                                                                                -le
-lt WHILE LOOP
SYNTAX:
while test CONDITION
do
statements
done
OR
while [condition] UNTIL LOOP do                         until [condition]
  statement do done                       statement
done
FOR LOOP
SYNTAX:

for i in 10 20 30 40
do
statement
done


IF ELSE
if test CONDITION
then
statement
else
statement
fi
OR
if [ CONDITION ]
then
statement
else
statement
fi
---------------------------------------------
Command substitution



DAY2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

FUNCTIONS
========================================================================All the variables in shell script by default are GLOBAL

Local variables can be declared with keyword "local"

/* TO DEMO LOCAL AND GLOBAL VARIABLES*/
testing()
{
local str="Hello I M in this world"
echo "$str"
}
str="Helooi Who are you"
echo $str
testing
echo $str


OUTPUT
[root@ngdb VISHWA]# sh  file4.sh
Helooi Who are you
Hello I M in this world
Helooi Who are you
[root@ngdb VISHWA]#

========================================================================
ways of executing the script
========================================================================
1> sh script.sh
2> ./script.sh (make the script executable by adding permissions)
3> . script.sh (source script.sh)

========================================================================
Special variables of shell script
========================================================================
1> Command line arguments

$$ --> Gives the pid of the current process
$! --> Gives the pid of the last executed background process (previously completed process)
$? --> Gives the exit status of the last executed command( 0 for success, non-zer for failure)
$1-$9 --> Command line arguments
$# --> Total count of arguments
$*/$@ --> List of arguments


#PROGRAM TO DEMO COMMAND LINE ARGUMENTS */
#PROGRAM SHOULD DO */
#TESTCASE1***/
#SHOULD COMAPRE BOTHE THE FILES< PRINT WHETHER SAME OR DIFFERENT, CHECK WHETHER BOTH THE FILE EXISTS ALSO CHECK IF SUFFICIENT ARGUMENTS ARE PASSED */



if [ $# -lt 1 -o $# -gt 2  ]
then
        echo "Enter the valid number of the arguments"
        exit;
else
        if [ -e $1 -a -e $2 ]
                then
                        echo "Comparing the two files"
                        cmp $1 $2 > /dev/null
                        if [ $? -eq 0 ]
                                then
                                        echo "Two files are same"
                                else
                                        echo "Two files are different"
                        fi
                else
                        echo "Error: One of the files doesn't exists"
        fi
fi

shift is used to traverse through the command line arguments
========================================================================
SIGNAL HANDLING
========================================================================
#PROGRAM TO HANDLE THE SIGNAL like ctrl+c ctrl+d
# trap is used to handle any signal
#syntax is " trap 'action' <signal>
#NOTE:
$ SIGSTOP, SIGKILL SIGGSTP can not be trapped and can not be handled by trap command as they are non-maskable interrupts

trap ' echo "Invalid signal:sleep 2"' SIGINT
for i in `seq 1 10`
do
        echo $i
        sleep 2
done
================================================================================
getopts command
================================================================================
================================================================================
sed command
================================================================================
  stream editor
  u can execute major editor commands
 
  how to print first 5 lines of file
  head -5 one.txt | tail -1
  or
  sed -n '5p' one.txt
 
  how to print 21 to 25 lines of a file
  sed -n '21,25p' one.txt
  sed -n '20,$p' one.txt
 
  how to print last five lines
 
  how to delete blank lines of files
  sed '^$/d' one.txt
 
  How to search and replace
  sed '/s/hello/bye/gi' one.txt
 
ADD TEXT
----------------
sed 'a\what to be add\'

CHANGE THE LINE
----------------
sed 'c\what to be changed\'

INSERT THE TEXT
----------------
sed 'i\what to be inserted\'
===============================================================================
awk
===============================================================================
-->Advanced filter
--> Useful for tabular data
--> Reporting Language
--> Pseudo "C" compiler
--> No need of trimming
--> Can check in specific column search
syntax:

awk -options '{cmds}' filename

How to print first two columns of a file
awk -F" " '{print $1 $2}' <filename.txt>

to print first n last line of ls -l command
ls -l | awk '{print $1 $9}'

How to check in perticular column
awk '{}$2~/sales/{print}' emp.txt
OR


BEGIN{----------}
END{---}
awk '{sum=sum+$4}END{print sum}' emp.txt


awk Special Variables

NR-->Number of ROWS
NF--> NUMBER of Fields
RS--> ROW seperator
FS--> FIeld Seperator
ORS-->o/p row seperator
OFS--> o/p field seperator

awk 'NR==5{print}' num.txt ---->Prints the fifth line
awk 'NR>=1 && NR<=5{print}' num.txt ------------->Prints the first five lines
awk '{print $NF}' num.txt -------> Prints the last column of hte file

=====================================================================
awk scripts
=====================================================================
Syntax:

BEGIN {
BEGINBLOCK
}
{
BLOCK
}
END{
ENDBLOCK
}

My file has 20 lines
how many time BEGINBLOCK gets executed = 1
how many time ENDBLOCK gets executed = 1
how many time BLOCK gets executed = 20

No comments:

Post a Comment