Wednesday, July 31, 2013

Operators in Bash script

1 String comparison operators

(1) s1 = s2
(2) s1 != s2
(3) s1 < s2
(4) s1 > s2
(5) -n s1
(6) -z s1

(1) s1 matches s2
(2) s1 does not match s2
(3) __TO-DO__
(4) __TO-DO__
(5) s1 is not null (contains one or more characters)
(6) s1 is null

2 Arithmetic operators

+
-
*
/
% (remainder)

3 Arithmetic relational operators

-lt (<)
-gt (>)
-le (<=)
-ge (>=)
-eq (==)
-ne (!=)

Tuesday, July 16, 2013

Splitting string using cut command in Linux

cut command can be used to split the string with specific split character

command:

echo "18:30:00"|cut -d ':' -f1
18

echo "18:30:00"|cut -d ':' -f2

30

echo "18:30:00"|cut -d ':' -f3

00

Sunday, July 14, 2013

split string in bash

here is example using sed command.

echo "my name is;chotelal" | sed -e 's/;/\n/g'
myname is
chotelal


File test option in shell

These are the options to be used in conditional statement to check file attributes.

-e
file exists

example:
      
      if [ -b "$filename" ]
      then
        echo "$filename exist."
      fi

-a
file exists
This is same in effect to –e, but now it is discouraged to use.
-f
file is a regular file (not a directory or device file)
-s
file is not zero size
-d
file is a directory
-b
file is a “block device”
-c
file is a “character device”
-p
file is a “pipe”
-h
file is a “symbolic link”

-L
file is a symbolic link
-S
file is a “socket”
-r
file has read permission (for the user running the test)
-w
file has write permission (for the user running the test)
-x
file has execute permission (for the user running the test)

-k
sticky bit set
Commonly known as the sticky bit, the save-text-mode flag is a special type of file permission. If a file has this flag set, that file will be kept in cache memory, for quicker access.  
-O
you are owner of file
-G
group-id of file same as yours
-N
file modified since it was last read
f1 -nt f2
file f1 is newer than f2
f1 -ot f2
file f1 is older than f2
f1 -ef f2
files f1 and f2 are hard links to the same file
!
"not" -- reverses the sense of the tests above (returns true if condition absent).


how to find files with specific date range

There seems to be no direct option in find command to specify the date and get the files with specified modified date, but find command provide -newer option which will find files newer than file specified. so, here is small tweak which worked for me.

Variable:
Sdate=07/02/2013
Stime=19:00:00
Edate=07/05/2013
Etime=18:00:00
WRKDIR=/data/log
TEMPDIR= /home/dba/temp

1. create a file with modified date as start date.

> touch -d "$Sdate $Stime" $TEMPDIR/start

2. create a file with modified date as end date

> touch -d "$Edate $Etime" $TEMPDIR/end

3. find command

> find $JIRDIR -newer $TEMPDIR/start -and -not -newer  $TEMPDIR/end >$TEMPDIR/tmp_jir.txt

"tmp_jir.txt" file contains file names between the specified date range.