Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)
Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

linux script to replace

replaces a list of properties in a directory /root/source

for y in `ls /source`;
do sed -f replace.sed /root/source/$y > ./tmp/$y;
done


replace.sed
---------------
s/__CLUSTERNAME__/grapesClus/g
s/__Place2__/Value2/g 



Shell Script System info

#!/bin/sh
echo "###############################"
echo "       Hardware Details        "
echo "###############################"

echo "HostName:`uname -n`"
echo "Machine Name:`uname`"
echo "Processor Name:`uname -p`"
echo "##############################"
echo   "     Software details       "
echo "##############################"
echo "Os Name:`uname -o`"
echo "Os Release:`uname -r`"
echo "Od version:`uname -v`"

Shell Script to find out if a rectangle type

 

echo "Enter Angle A:"
read A
echo "Enter Angle B:"
read B
echo "Enter Angle C:"
read C

if [ $A -eq 90 -o $B -eq 90 -o $C -eq 90 ]
then
    echo "This is Right angled Triangle"
elif [ $A -gt 90 -o $B -gt 90 -o $C -gt 90 ]
then
   echo "This is Obtuse angled Triangle"
elif [ $A -lt 90 -a $B -lt 90 -a $C -lt 90 ]
then
echo "This is acute angled Triangle"
fi

Shell Script Linux

the first line in the shell script should be the interpreter location to be used

#!/bin/bash ----> this interpreter is taken to execute the file

if this is not there the shell which launched this will be taken.

 

echo –n no new line

eco –e take special

Double Quotes does interpolation , substitutes with var value
Single Quotes no manipulation directly prints
Back Quotes executes a command and gives us output.

Special variables in shell

program to print

  • program name
  • number of paramerters
  • all arguments

echo "program name is $0"
echo "argument one dollar 1 $1"
echo "argument two dollar two $2"
echo "argument three Dollar three $3"
IFS=":"
echo "all args $@ "
echo "all args with delimiter $* "

 

If in Shell Script:

if test $x –lt $y

OR

if [$x –lt $y]

Check the file properties

root@v11> if [ -e shell.sh ]; then echo yes ; else echo no; fi
yes
root@v11> if [ -f shell.sh ]; then echo yes ; else echo no; fi
yes
root@v11> if [ -d shell.sh ]; then echo yes ; else echo no; fi
no
root@v11> if [ -L shell.sh ]; then echo yes ; else echo no; fi
no
root@v11> if [ -r shell.sh ]; then echo yes ; else echo no; fi
yes

Case in Shell Script:

case $1 in
  a) echo "this is a";;
  b) echo "this is b";;
  c) echo "this is c";;
  *) echo "this is default";;
esac

While in Shell Script:

num=0;
while [ $num -lt $# ]
do
echo $num
      num=$[num+1]
done

Until in Shell Script:

For in Shell Script:to print all the command line arguments passed using for

for i in $@
do
  echo $i
done

 

Handling Signals

trap "echo trapped; exit;" SIGINT

while [ 1 ]
do
echo "hi"
done

Linux Set and Tar commands

SET

shows the list of environment variables

[trainee@localhost purna]$ set
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="1" [2]="17" [3]="1" [4]="release" [5]="i686-redhat-linux-gnu")
BASH_VERSION='3.1.17(1)-release'
COLORS=/etc/DIR_COLORS.xterm
COLUMNS=80

tar

to tar

tar –cvf <file.tar> input files list

untar

tar –xvf <file.tar>

tar –zxvf will unzip and untar it.

At,Batch and Cron

crontab:

l list

e edit

r remove

format of the file

minutes hour day month days of week Command

       0             *                 *               *             “mon-sat”     echo “Hello”    

every hour  you get a hello message.

the output of the above is sent to mail.

Processes in Linux

f

Program  ?

  • process if different from a program.
  • Program under execution is a process.
  • Program is set of instructions.

INIT: Mother of all the processes.

  • Fork: if there is process P1 can fork out into multiple processes which are identical,Independent P1 and P2 with different PID’s.
  • Exec:
  • Wait:

Rights of Process

  1. Who am i ? what is its pid
  2. who is my parent ? what is my parent process id
  3. what happened to my child ?

Orphan Process :

the process whose parent died,when a parent process is killed the INIT process takes the responsibility that's the reason its called mother of all the processes.

Zombie Process :

A process

 

PS

processes running on your terminal

[trainee@localhost purna]$ ps
  PID TTY          TIME CMD
6188 pts/14   00:00:00 bash
6507 pts/14   00:00:00 ps

number of processes running

[trainee@localhost purna]$ ps -e|wc -l
164

processes running on a terminal pts/3

[trainee@localhost purna]$ ps -tpts/3
  PID TTY          TIME CMD
4300 pts/3    00:00:00 bash
6612 pts/3    00:00:00 cat

DAEMON

Disk And Execution Monitor

JOBS

will print all the jobs which are stopped or paused .

to resume these jobs one can use fg or bg (foreground or background) %JOB_NUMBER

KILL

list all signals

[trainee@localhost purna]$ kill -l
1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
13) SIGPIPE     14) SIGALRM     15) SIGTERM     16) SIGSTKFLT
17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU
25) SIGXFSZ     26) SIGVTALRM   27) SIGPROF     28) SIGWINCH
29) SIGIO       30) SIGPWR      31) SIGSYS      34) SIGRTMIN
35) SIGRTMIN+1  36) SIGRTMIN+2 37) SIGRTMIN+338)SIGRTMIN+4}Dummy signals
39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6
59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

Kill A Process

[trainee@localhost purna]$ kill -s SIGKILL 4524

where 4524 is the process id

Nice

the lesser the Nice value the higher the priority

only the root can make the priority higher or nice value to low.

Regular Expressions in Linux

Regular Expression:

[.] DOT dot matches for a single character
[abcd] matches one of the list ,say [abcd] matches one of a,b,c,d
[a-d] matches the range of characters a to d
[^a-i] should not contain the range a to i
[a*] ~= {0,} match zero or more a’s
[a+] ~= {1,} match one or more a’s
[a?] ~= {0,1} zero or one occurance of letter a
wh(at|en|ere} what when where , you can specify group of letters and also a pipe to mark OR operator
^not start with string not,notify,notice etc……
not$ end with not like knot

 

GREP: search Globally for Regular Expression and Print

grep option[s]
  -n number in the file
  -c count the number of occurrences, unlike wc –l it gives the file wise count
  -A context selection After
-B Context selection Before
you can specify number with A or B
  -l List the names of files
  -r recursively Grep
  -s , if there are permission errors it suppresses
  -e used to search multiple regular expressions

Egrep: Extended GREP { More operators +,?,(),|}

Fgrep: Fixed length Grep , does not support operators

Filters in Linux


VI Editor

VI – Visual Editor.

Normal no sign
Command Line : colon
Input INSERT/REPLACE

 

Normal –> Insert mode a,i,o,r,s A,I,O,R,S

Esc/Enter –>  to normal mode.

: to command line mode.

H

-{minus}     k

0<----h       *        l---->$ ----------------------->M

Enter         j

L

 

GG= go to line number

x delete command

X replacement for back space

u undo char by char

U undo at once
DW delete a word

DD delete a whole line

repeat a command 5x will delete 5 chars meaning number specifies number of times the command has to be executed.

q quit, vi wont allow if there are any changes done.
q! quit without saving
wq write and quit
wq <filename> save as file name and quit,if file not exists
wq! <filename> save as file name and quit,if file exists
r! <Command> run the command and put  the output at the cursor position
sh takes u to shell from vi ,where you can execute commands and come back.

Some more linux Commands

file:  give the type of the file , which is determined using the extension of the file.

[trainee@localhost purna]$ file *
as:     directory
f2:     empty
f3:     ASCII text
f4:     ASCII text
f5:     ASCII text

 

WC: gives the word count in a file or some stream of data.

c=character count

l = line count

L = length of the longest line

w= word count

split:  split a file into many parts , the default is 1000 lines but you can use –l option and specify number of lines.

[trainee@localhost purna]$ split -l 5 f5
[trainee@localhost purna]$ ls
as  f3  f5      g1  xaa  xac  xae  xag  xai
f2  f4  fruits  g2  xab  xad  xaf  xah  xaj

splits f5 file to many files like

xaa  xac  xae  xag  xai
xab  xad  xaf  xah  xaj

cmp:  check if two files are same

when they are equal there is no output

else it looks like this

[trainee@localhost purna]$ cmp xaa xab
xaa xab differ: byte 1, line 1
[trainee@localhost purna]$ cmp -l xaa xab
1  40  61
2  40  70
4  40  61
5  40  71

Inode and Linux file system

DISK TYPES

  • IDE 
  •         /dev/hda
  •         /dev/hdb
  •          ……
  • SCSI
  •       /dev/sda
  •       /dev/sdb

Linux Partition

Boot block Super Block I node table Data Block

Boot Block : has information to boot

Super Block: has all summary of the parition

  • has list of free inode numbers
  • has list of blocks
  • a copy of super block is put in ram is called super surrogagate block. this is periodically updated this is called sync, this happens during shutdown.

Inode : all properties of files are stored {except this everything else is present in INODE file number,name,data}

Inode table is list of Inodes

Inode looks like this

Type
permissions
uid/gid
timestamp
links Count--->when more than file names which refer to the inode then this is incremented.
size
Pointer to the disk Block

Data Block: is the place where actual data is stored.[Data Block Pointers]

PTR 1
PTR 2
 
 
 
 
PTR 11 – > pointer to a list of pointers
PTR 12 – > double indirection
 

 

Hard Links:

has the same i-node number with different file name

when original file is deleted the link file is still present until the hard link count is 0

ln <target name> <link name>

ln <targets> <dest dir>

hard links are not allowed for directories

Every time a directory is created it has 2 hard links the path and the [.]

and when a sub dir is created the .. becomes another link

Soft Links:

the size of the file is the path length.

when the original file is deleted this symbolic link becomes invalid.

permission of symbolic link is 777 which is of no use.

when user tries to change permission on soft link it might affect the original file.

same as hard link except that you need to give an option –s

soft links are allowed for directories

Check Inode number:

[trainee@localhost purna]$ ls -li
total 1

Inode number---------hardlink count
3702945 drwxrwxr-x 2 trainee trainee 4096 Jun 14 15:03 file1

Linux File Permissions

  Text Binary Directory
Read cat <file> cp /bin/ls ls
Write cat > <file> cc a.c rm <directory>
eXecute ./<file> ls cd <directory>

 

Only 2 users can change the permissions of a file

  • the Owner
  • super user or root

ls -l
Owner-Group-Others HardLinks username groupName bytes date filename

drwxrwxr-x 2 trainee trainee 4096 Jun 14 15:03 as
-rw-rw-r-- 1 trainee trainee    0 Jun 14 14:27 f2
-rw-rw-r-- 1 trainee trainee  148 Jun 14 14:28 f3
-rw-rw-r-- 1 trainee trainee  148 Jun 14 14:28 f4
-rw-rw-r-- 1 trainee trainee  888 Jun 14 14:29 f5
-rw-rw-r-- 1 trainee trainee    0 Jun 14 15:10 fruits
-rw-rw-r-- 1 trainee trainee    0 Jun 14 15:10 g1
-rw-rw-r-- 1 trainee trainee    0 Jun 14 15:10 g2

Change permissions on a file

  • Numerical: use chmod 777 filename all permissions to a file.
  • Symbolic: use chmod a+x,g-w <filename>    [User Group] [+/-] [r/w/x]

UMASK:

  • specifies what has to be denied
  • its only for newly created files

you can find out umask by typing umask

[trainee@localhost purna]$ umask
0002 the last three specify what permission to deny to the owner group and others

Linux Files and Directories

input----> Command ----> Output

input redirection <

output redirection >

cat f1 > f2

to concatenate

to display file contents

copy files

cat without any arguments ….takes from the terminal and writes to the terminal so reads from keyboard and displays on screen this will be infinite loop .

cat f1 >> f2  [APPEND]

Less:

read from standard input and show page by page on standard output.

Touch:

  • is a easy way to create a file which does not exist
  • if it exists its access time is updated unlike cat it does not display

ls- lists the files/directories in a directory

  1. color has a meaning which can be found out using –color option.
  2. files starting with [.]DOT are hidden files.

image

 

/[Slash]- Root directory

dev: all devices

lib: library's for every one.

lost+found: specific to redhat recovered files

Misc: miscellaneous.

proc: mapping to memory.

tmp : temporary files

var : variety of log files

bin: binaries of commands like ls mkdir etc.

boot: boot loader files, kernel etc

sbin: system binaries

mnt: any mountable devices like cdrom pendrive are mounted under this.

./ : current working directory

..  : parent directory

: Home directory (available only in few shells)

PWD : print working directory

CD: change directory, cd without any arguments takes to the home directory

        to go to home u can  use cd $HOME , cd ~ or just cd.

mkdir:  <dir name …..>make a directory, on success does not give any message ,, instead u can use –V for verbose.

mkdir –p make parents as necessary.

rmdir <dir name ….> removes the specific dir

ls *:  all the files

ls f?: list files which start with f and have one more leter.

ls f[1234]/ls f[1-4]: f followed by one of 1-4

Note for copy and move

  • Use option -i to make it interactive along with v so that you have a say whether you have to overwrite or not .
  • use -r to move or copy recursively.
  • use –v to make it verbose.
  • -f to make it silent.
  • -b to take a back up before overwrite as we have bvf option is safest.

Copy:

cp <source file> <dest file>     

cp <source files…….> <dest file>

Move:

mv <source file> <dest file>

mv <source files…….> <dest file>

Delete:

rm: removes files completely very hard to recover.

rm –rf ~ [ most dangerous command ]

Linux Notes Part 1

root@v11geonode2> cat /etc/passwd
root:x:0:0:Super-User:/root:/sbin/sh
daemon:x:1:1::/:
bin:x:2:2::/usr/bin:

USERNAME : PASSWORD:USERID:GROUPID

the password encrypted is stored at /etc/shadow

vin:$1$3vD2BLH4$ZEFSMbnDYrJ4YGEqzKDR0/:14734:7:91:7:::
user2:$1$19HkQt88$DBFe7VgQpGdd7osh9Oe8m1:14735:7:91:7:::
vin1:$1$0YwoL0i8$/Jg7ZnPs50lxlSax3Nc5C0:14742:7:91:7:::

logout

  1. logout
  2. exit
  3. control D

Shutdown

  1. halt :  shut down and halt.
  2. Init 0/6 : run level 0 is halt and 6 is reboot.
  3. shutdown –r/h h=halt r=reboot
  4. poweroff
  5. reboot
  6. restart

Shells

  1. bourne shell sh
  2. csh-c shell csh
  3. ksh-korn shell(is the most powerfull shell) ksh
  4. bash bourne again. bash

 

COMMAND OPTIONS ARGUEMENTS
internal/external character or string optional
     

 

Who Am I [aka who –m] : tells who the logged in user is

root@v11geonode2> who am i
root       pts/2        Jun 14 12:18    (10.142.208.146)

user      terminal    Login Time     IP-Address

actually who with any two options will return the same

you can actually type who mom likes / who dad likes tooo

WHO : all the users

root@v11geonode2> who
rtp99      pts/1        Jun 14 11:16    (10.142.216.148)
root       pts/3        Jun 14 12:20    (10.142.216.164)
root       pts/4        Jun 14 12:27    (10.142.216.158)
root       pts/7        Jun 14 10:34    (10.142.216.158)

-U gives idle time

id

[trainee@localhost /]$ id
uid=500(trainee) gid=500(trainee) groups=500(trainee) context=user_u:system_r:unconfined_t
[trainee@localhost /]$ id -u
500
[trainee@localhost /]$ id -g
500
[trainee@localhost /]$ id -G
500
[trainee@localhost /]$ id -nu
trainee
[trainee@localhost /]$ id -ng
trainee
[trainee@localhost /]$ id -nG
trainee

about Us

root@v11geonode2> logname
root
root@v11geonode2> tty
/dev/pts/2

Hardware Information

[trainee@localhost /]$ uname -s
Linux
[trainee@localhost /]$ uname -o
GNU/Linux
[trainee@localhost /]$ uname -p
i686
[trainee@localhost /]$ uname -m
i686
[trainee@localhost /]$ uname -n
localhost.localdomain
[trainee@localhost /]$ uname -r
2.6.18-8.el5
[trainee@localhost /]$ uname -v
#1 SMP Fri Jan 26 14:15:21 EST 2007
[trainee@localhost /]$ uname -a
Linux localhost.localdomain 2.6.18-8.el5 #1 SMP Fri Jan 26 14:15:21 EST 2007 i686 i686 i386 GNU/Linux

Date

Mon Jun 14 13:03:43 IST 2010

Changing password

root@v11geonode2> passwd
passwd: Changing password for root
New Password:

root can set some stupid and simple passwords.

Calculator(powerful but not user friendly)

[trainee@localhost /]$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2+3
5

x=2
y=3
z=x+y
print z
5

quit

Linux has 10k commands so …learn to use the manual[Manual pages]

man [Command]

  • man kill
  • man -k kill : lists all the sections where the command is there.[trainee@localhost /]$ man -k kill
    kill                 (1p)  - terminate or signal processes
    kill                 (1)  - terminate a process
    kill                 (2)  - send signal to a process
  • man 2 kill      where 2 is the section ie the one in the bold above

info

more user friendly help

it has hyper links in the form of *[Star]

--help

only few commands support this , brief explanation

whatis kill

lists the sections

Search 24 Bytes