unix knowledge document

56
1 Unix Knowledge Document UNIX Knowledge Document Capgemini India | Morgan Stanley|RT_SPG

Upload: swapnil-saket

Post on 27-Nov-2014

48 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UNIX Knowledge Document

1 Unix Knowledge Document

UNIX Knowledge Document

Capgemini India | Morgan Stanley|RT_SPG

Page 2: UNIX Knowledge Document

2 Unix Knowledge Document

Prepared By,

Capgemini India | Morgan Stanley|RT_SPG

Page 3: UNIX Knowledge Document

3 Unix Knowledge Document

RT SPG Group

UNIX Training – Understanding Document

UNIX basic commands:................................................................................................................................5

pwd..........................................................................................................................................................5who..........................................................................................................................................................6whoami....................................................................................................................................................6who..........................................................................................................................................................6cd.............................................................................................................................................................6ls..............................................................................................................................................................8man........................................................................................................................................................10cp...........................................................................................................................................................11mv..........................................................................................................................................................12Pipe symbol...........................................................................................................................................12Touch.....................................................................................................................................................12Removing the folder..............................................................................................................................13Cat.........................................................................................................................................................13Head......................................................................................................................................................13Tail.........................................................................................................................................................13Chmod...................................................................................................................................................14more......................................................................................................................................................14Symbolic links........................................................................................................................................15Hard link vs. Soft link in UNIX.................................................................................................................15How do I create symbolic link?..............................................................................................................15Alias.......................................................................................................................................................15Unset alias.............................................................................................................................................16vi editor.................................................................................................................................................17vi editor Key Options.............................................................................................................................17

Capgemini India | Morgan Stanley|RT_SPG

Page 4: UNIX Knowledge Document

4 Unix Knowledge Document

vi editor customization..........................................................................................................................23Grep.......................................................................................................................................................24Find........................................................................................................................................................27PS...........................................................................................................................................................27RPM.......................................................................................................................................................28

UNIX Shell scripting...................................................................................................................................30

Shells.....................................................................................................................................................30Environment Variables..........................................................................................................................31Operators..............................................................................................................................................32Shell Variables.......................................................................................................................................32Shift.......................................................................................................................................................33Conditional if.........................................................................................................................................34Conditional switch and case..................................................................................................................35while......................................................................................................................................................36until.......................................................................................................................................................37for and foreach......................................................................................................................................38

MYSQL Connection In unix........................................................................................................................40

To connect to mysql database...............................................................................................................41To know Databases present...................................................................................................................41Accessing the Database.........................................................................................................................42To know the tables present in the database..........................................................................................42Creating table........................................................................................................................................43Design of the table.................................................................................................................................43Insert command....................................................................................................................................43Update command..................................................................................................................................43Delete Command...................................................................................................................................44Select command....................................................................................................................................44Where clause.........................................................................................................................................45

Capgemini India | Morgan Stanley|RT_SPG

Page 5: UNIX Knowledge Document

5 Unix Knowledge Document

UNIX Training – Understanding Document

UNIX is the first operating system to provide multi user operating system. UNIX is most preferred for its security and platform independency. UNIX contains different type of shells like K- Shell, C-Shell, Tcsh, Bourne shell, bash shell. Every shell provides the same interface between the hardware and the user. Each one has its own specialty. For example, Korn Shell is used for Administration purpose. Bash shell is used for day to day activities. UNIX is a case sensitive.

UNIX basic commands:

Capgemini India | Morgan Stanley|RT_SPG

Page 6: UNIX Knowledge Document

6 Unix Knowledge Document

All UNIX commands will be in lower case. But options may be in any case (depends on the functionality)

pwd – stands for print working directory. It displays the present working directory.

$pwd

/home/user/

who – displays the active user who is presently working in this server

$who

root tty1 Sep 8 16:07

root :0 Sep 8 16:06

user4 pts/0 May 17 11:31 (10.138.5.167)

user3 pts/2 May 17 12:57 (10.138.5.73)

user2 pts/4 May 17 13:15 (10.138.5.76)

whoami - displays the which user is presently working in this terminal in short form

$who

user2

who am i – displays the which user is presently working in this terminal in long form

Capgemini India | Morgan Stanley|RT_SPG

Page 7: UNIX Knowledge Document

7 Unix Knowledge Document

$who am i

user2 pts/4 May 17 13:15 (10.138.5.76)

cd uname – change the directory to uname. cd <newfolder> will change directory from current directory to <newfolder> directory. Use pwd to check your current directory and ls to see if <newfolder> directory is there or not. You can then use cd <newfolder>to change the directory to this new directory.

cd changes to user's home directory

cd / changes directory to the system's root

cd .. goes up one directory level

cd ../.. goes up two directory levels

$pwd

/home/user

$ cd uname

$ pwd

/home/user/uname

cd .. – changes the directory to the parent directory

$pwd

Capgemini India | Morgan Stanley|RT_SPG

Page 8: UNIX Knowledge Document

8 Unix Knowledge Document

/home/user/uname

cd ..

$pwd

/home/user

cd / - changes the directory to the root directory

$pwd

/home/user2

$cd /

$pwd

/

cd – changes the directory to the home directory

$pwd

/

$cd

$pwd

/home/user

Capgemini India | Morgan Stanley|RT_SPG

Page 9: UNIX Knowledge Document

9 Unix Knowledge Document

ls – list the file and directories in the present directory

backup dev lib opt Punapp250_proc users

bin etc linux proc PuneSSIM20080610.tar usr

boot fmcd.log lost+found punapp250 root var

ch.tar home misc Punapp250 sbin

dblog initrd mnt Punapp250_cpu tmp

ls –l – displays the list of files and directory in long format

total 385

drwxr-xr-x 2 root root 4096 Jan 11 2006 backup

drwxr-xr-x 2 root root 4096 Nov 21 2006 bin

drwxr-xr-x 4 root root 1024 Sep 4 2003 boot

-rw-r--r-- 1 root root 92160 Nov 11 2003 ch.tar

drwxr-xr-x 4 root root 4096 Jun 10 2008 dblog

drwxr-xr-x 20 root root 118784 Sep 8 2009 dev

ls –a – displays all the files and directory including the hidden files

. ch.tar home mnt Punapp250_cpu users

Capgemini India | Morgan Stanley|RT_SPG

Page 10: UNIX Knowledge Document

10 Unix Knowledge Document

.. dblog initrd opt Punapp250_proc usr

.autofsck dev lib proc PuneSSIM20080610.tar var

backup etc linux .profile root

ls –S – displays the files and directories in the sorted order of size

total 385

drwxr-xr-x 20 root root 118784 Sep 8 2009 dev

-rw-r--r-- 1 root root 92160 Nov 11 2003 ch.tar

drwx------ 2 root root 16384 Sep 4 2003 lost+found

-rw-r----- 1 root root 15348 Apr 8 2005 fmcd.log

-rw-r--r-- 1 root root 13159 Apr 24 2008 punapp250

-rw-r--r-- 1 root root 13159 May 2 2008 Punapp250

ls –a: lists all files, including Hidden files

ls –l: long listing: lists the mode, link information, owner, size, last modification (time).

ls –lsa: long list of files sorted by list, including hidden files

ls –l dir name –list all files in a directory

Capgemini India | Morgan Stanley|RT_SPG

Page 11: UNIX Knowledge Document

11 Unix Knowledge Document

man – displays the manual for the a command. This is help command, and will explains you about online manual pages you can also use man in conjunction with any command to learn more about that command for example.

$man cp

CP(1) User Commands CP(1)

NAME

cp â copy files and directories

SYNOPSIS

cp [OPTION]... SOURCE DEST

cp [OPTION]... SOURCE... DIRECTORY

cp [OPTION]... ââtargetâdirectory=DIRECTORY SOURCE...

DESCRIPTION

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options

too.

Capgemini India | Morgan Stanley|RT_SPG

Page 12: UNIX Knowledge Document

12 Unix Knowledge Document

cp - copy the files from one place to other

$pwd

/home/user

$cp abc.txt uname/abc.txt

$cd uname

$pwd

/home/user/uname

$ls

abc.txt

mv – move or rename the file

$pwd

/home/user/uname

$mv abc.txt ../abc.txt

$cd ..

$pwd

/home/user

Capgemini India | Morgan Stanley|RT_SPG

Page 13: UNIX Knowledge Document

13 Unix Knowledge Document

$ls

abc.txt

Pipe symbol - Passing one command output to other command using |pipe symbol

$ ls -l|wc -l

5

$ ls -l|wc -c

261

Touch - Touch will be creating a ‘0’ byte file. It will be used in shell scripting a lot.

$ touch touchsamplefile

-rwxrw---x 1 user5 user5 0 May 17 16:10 touchsamplefile

Removing the folder - rmdir directory name – Deletes the folder. The folder should never contain files or folders inside.

rm filename - Deletes the filename

rm –r directory name - Deletes the folder

rm –rf directory name – Force deletes the folder, even though it contains files inside it

Cat - Cat filename – displays the filename

Capgemini India | Morgan Stanley|RT_SPG

Page 14: UNIX Knowledge Document

14 Unix Knowledge Document

$cat sample.txt

This is my sample program.

My first program.

Head - Displays the first 10 lines of file by default

$head -5 abc

sgh

szdfg

asrf

ewrt

wrt

Tail - Displays the last 10 lines of file by default

$tail -5 abc

iyt

gtuy

gvyu

cfgh

drtb

Chmod - Change mode is to change the permission of the file

Capgemini India | Morgan Stanley|RT_SPG

Page 15: UNIX Knowledge Document

15 Unix Knowledge Document

read=4, write=2, execute=1

read=r, write=w, execute=x

$chmod 777 abc

-rwxrwxrwx 1 user5 user5 0 May 17 16:10 abc

$chmod u=rwx,g=rwx,o=rwx abc

-rwxrwxrwx 1 user5 user5 0 May 17 16:10 abc

more – This command will display a page at a time and then wait for input which is spacebar. For example if you have a file which is 500 lines and you want to read it all.

$more filename

Symbolic links refer to: A symbolic path indicating the abstract location of another file. That is, it creates a link or a additional way to access another file (similar to shortcut in Windows).

Hard links refer to: The specific location of physical data.

Hard link vs. Soft link in UNIX

Hard links cannot links directories

Cannot cross file system boundaries

Soft or symbolic links are just like hard links. It allows associating multiple filenames with a single file. However, symbolic links allows:

Capgemini India | Morgan Stanley|RT_SPG

Page 16: UNIX Knowledge Document

16 Unix Knowledge Document

To create links between directories

Can cross file system boundaries

These links behave differently when the source of the link is moved or removed.

Symbolic links are not updated.

Hard links always refer to the source, even if moved or removed.

How do I create symbolic link?

You can create symbolic link with ln command:

$ ln -s /path/to/file1.txt /path/to/file2.txt$ ls –ali

Above command will create a symbolic link to file1.txt.

If you delete the soft link itself, the data file would still be there.

Alias – is a command which enables a replacement of a word with another string. It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command. The alias name exist even if we logout and login again.

$ls

file1 file-A filea-A file-Aa file--Aa fileA-a first.sh my-script.sh myscript.sh sample second xyz

$rm file1

rm: remove regular file `file1'?y

$

To view the alias that are already used by this shell, use the below command

Capgemini India | Morgan Stanley|RT_SPG

Page 17: UNIX Knowledge Document

17 Unix Knowledge Document

$alias

alias l.='ls -d .* --color=tty'

alias ll='ls -l --color=tty'

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

Unset alias – This command will unset the previously set alias, if any

$alias

alias l.='ls -d .* --color=tty'

alias vi='vim'

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

$unalias vi

$alias

alias l.='ls -d .* --color=tty'

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

$

vi editor – The vi editor (short for visual editor) is a screen editor which is available on almost all Unix systems. vi has no menus but instead uses combinations of keystrokes in order to accomplish commands.

Capgemini India | Morgan Stanley|RT_SPG

Page 18: UNIX Knowledge Document

18 Unix Knowledge Document

$vi <file_name> - will open the file in editing mode.

vi editor Key Options – There are various key options available to accomplish a task at vi editor.

Positioning Cursor :

By Character :

h -> one character left

l -> one character right

$ -> Moves the cursor to the end of the current line

0 -> Moves the cursor to the beginning of the current line.

By Line :

j -> one line down

k -> one line up

G -> Moves the cursor to the beginning of the last line in the file

nG -> Moves the cursor to the beginning of the 'n'th line in the file

Capgemini India | Morgan Stanley|RT_SPG

Page 19: UNIX Knowledge Document

19 Unix Knowledge Document

By Word :

w -> Moves the cursor to the right, to the first character of the next word

b -> Moves the cursor back, to the first character of the previous word

e -> Moves the cursor to the end of the current word

In Window :

H -> Moves the cursor to the first line on the screen

M -> Moves the cursor to the middle line on the screen

L -> Moves the cursor to the last line on the screen

Scrolling :

Ctl f -> Scrolls the screen forward a full window

Ctl b -> Scrolls the screen back a full window

Commands for Inserting Text :

Capgemini India | Morgan Stanley|RT_SPG

Page 20: UNIX Knowledge Document

20 Unix Knowledge Document

a -> Appends text after the cursor

A -> Appends text at the end of the current line

i -> Inserts text at the cursor

I -> Inserts text at the beginning of the current line

o -> Opening a new line immediately below the current line

O -> Opening a new line immediately above the current line

R -> Overwrites the text

Command for Deleting Text :

x -> Deletes the character at the current cursor position

X -> Deletes the character to the left of the cursor

dw -> Deletes a word from the current cursor position

dd -> Deletes the current line

nx -> Deletes 'n' characters from current cursor position

ndw -> Deletes 'n' words

ndd -> Deletes 'n' lines

d0 -> Deletes the current line from the cursor to the beginning of the line

d$ -> Deletes the current line from the cursor to the end of the line.

n,m d -> Deletes lines from 'n' to 'm'

Capgemini India | Morgan Stanley|RT_SPG

Page 21: UNIX Knowledge Document

21 Unix Knowledge Document

Copy and Move :

:n mo p -> Moves line 'n' after line 'p'

:m,n mo p -> Moves lines 'm' to 'n' after line p

:m co p -> Copies line 'm' after line 'p'

:m,n co p -> Copies lines 'm' to 'n' after line 'p'

:m,n w <filename> -> Writes lines 'm' to 'n' to a new file

:m,n w >> <filename> -> Appends lines 'm' to 'n' to the existing file

:r <filename> -> Reads the contents of the file <filename> at the current cursor position

Search :

^str -> Will search all lines which begin with the word 'str'

Capgemini India | Morgan Stanley|RT_SPG

Page 22: UNIX Knowledge Document

22 Unix Knowledge Document

str$ -> Will search all lines which end with the word 'str'

\<str -> Will search all strings which begin with the word 'str'

str\> -> Will search all strings which end with the word 'str'

\<str\> -> Only the whole word 'str' will qualify for the search

[a-e]ing -> Will search all strings which contain only character in range 'a' to 'e' followed by 'ing'

[^a]ing -> Will search for all strings which starts other than 'a' and followed by 'ing'

n -> Repeats the last search command

N -> Repeats the search command in the opposite direction

Find and Replace :

:s/str1/str2 -> Replace just first occurrence of 'str1' with 'str2' in current line only

:s/str1/str2/g -> Replace all occurrence of 'str1' with 'str2' in current line only

:m,n s/str1/str2/g -> Replace all occurrence of 'str1' with 'str2' from line 'm' to 'n'

:1,$ s/str1/str2/g -> Replace all occurrence of 'str1' with 'str2' from line 1st line to EOF

:1,. s/str1/str2/g -> Replace all occurrence of 'str1' with 'str2' from line 1st line to current line

:.,$ s/str1/str2/g -> Replace all occurrence of 'str1' with 'str2' from current line to EOF

Capgemini India | Morgan Stanley|RT_SPG

Page 23: UNIX Knowledge Document

23 Unix Knowledge Document

Set Commands :

:set nu -> With line numbers

:set nonu -> Without any line number

:set ic -> Ignore case while searching

:set noic -> Do not ignore case while searching

:set eb -> Beep the speaker when error comes

:set noeb -> No beep any more

Undo :

u -> Undoes the effects of the last command

U -> Undoes the all changes to the current line, since cursor moved this line

Quitting :

:wq -> Writes to the file and quits

:w -> Writes to the file and keep working

:q -> Quits from vi

! adds to force all these

Capgemini India | Morgan Stanley|RT_SPG

Page 24: UNIX Knowledge Document

24 Unix Knowledge Document

vi editor customization- We can make our own customization to the vi editor by setting some options.

:set nonu - Turn line numbering off (set nonumber)

:set all - List settings of all options.

:set ai - Set autoindent.Facilitates structured insertion of text.

:set noai - Prevent the autoindent.

Grep

The grep utility is used to search for generalized regular expressions occurring in Unix files. Regular expressions, such as those shown above, are best specified in apostrophes (or single quotes) when specified in the grep utility.

Options

Capgemini India | Morgan Stanley|RT_SPG

Page 25: UNIX Knowledge Document

25 Unix Knowledge Document

Options:

-i

-c

-v

-n

-s

-l

$ cat num.list

1 15 fifteen

Capgemini India | Morgan Stanley|RT_SPG

Page 26: UNIX Knowledge Document

26 Unix Knowledge Document

2 14 fourteen

3 13 thirteen

4 12 twelve

5 11 eleven

$grep '5' num.list

1 15 fifteen

5 11 eleven

$ls –l|grep out

Above command will search for the out(file or directory) in the current directory

How To Use grep Command In Linux / UNIX

grep 'word' filename

grep 'string1 string2' filename

cat otherfile | grep 'something'

command | grep 'something'

Use grep to search file

Capgemini India | Morgan Stanley|RT_SPG

Page 27: UNIX Knowledge Document

27 Unix Knowledge Document

$ grep boo /etc/passwd

$ grep -i "boo" /etc/passwd

Use grep recursively

$ grep -r "192.168.1.5" /etc/

Use grep to search words only

$ grep -w "boo" /path/to/file

Use grep to search 2 different words

$ egrep -w 'word1|word2' /path/to/file

Count line when words has been matched

$ grep -c 'word' /path/to/file

Output with the number of the line in the text file

$ grep -n 'word' /path/to/file

Grep invert match

$ grep -v bar /path/to/file

List just the names of matching files

grep -l 'asd' *.out

Find

The find command will recursively search the indicated directory tree to find files matching a

Capgemini India | Morgan Stanley|RT_SPG

Page 28: UNIX Knowledge Document

28 Unix Knowledge Document

type or pattern you specify. find can then list the files or execute arbitrary commands based on the results.

find directory [search options] [actions]

Options:

+n more than n days

n exactly n days

- n less than n days

$find /home/user5/ sampleshell.sh

PS

ps is used to report on processes currently running on the system. The output format and valid options are very specific to the OS and program version in use.

ps [options]

Process :-a -e all processes, all users

Capgemini India | Morgan Stanley|RT_SPG

Page 29: UNIX Knowledge Document

29 Unix Knowledge Document

-e environment/everything-g process group leaders as well-l -l long format-u -u user user oriented report-x -e even processes not executed from terminals-f full listing

-w report first 132 characters per line

RPM

Rpm – package Manager

More Details “http://linux.about.com/od/commands/l/blcmdl8_rpm.htm”

rpm is a powerful Package Manager, which can be used to build, install, query, verify, update, and erase individual software packages. A package consists of an archive of files and meta-data used to install and erase the archive files. The meta-data includes helper scripts, file attributes, and descriptive information about the package. Packages come in two varieties: binary packages, used to encapsulate software to be installed, and source packages, containing the source code and recipe necessary to produce binary packages.

One of the following basic modes must be selected: Query, Verify, Signature Check, Install/Upgrade/Freshen, Uninstall, Initialize Database, Rebuild Database, Resign, Add Signature, Set Owners/Groups, Show Querytags, and Show Configuration.

GENERAL OPTIONS

These options can be used in all the different modes.

-?, --help

Print a longer usage message then normal. --version

Capgemini India | Morgan Stanley|RT_SPG

Page 30: UNIX Knowledge Document

30 Unix Knowledge Document

Print a single line containing the version number of rpm being used.

--quiet

Print as little as possible - normally only error messages will be displayed.

-v

Print verbose information - normally routine progress messages will be displayed.

-vv

Print lots of ugly debugging information.

--rcfile FILELIST

Each of the files in the colon separated FILELIST is read sequentially by rpm for configuration information. Only the first file in the list must exist, and tildes will be expanded to the value of $HOME. The default FILELIST is /usr/lib/rpm/rpmrc:/usr/lib/rpm/redhat/rpmrc:~/.rpmrc.

--pipe CMD

Pipes the output of rpm to the command CMD.

--dbpath DIRECTORY

Use the database in DIRECTORY rathen than the default path /var/lib/rpm

--root DIRECTORY

Use the file system tree rooted at DIRECTORY for all operations. Note that this means the database within DIRECTORY will be used for dependency checks and any scriptlet(s) (e.g. %post if installing, or %prep if building, a package) will be run after a chroot(2) to DIRECTORY.

INSTALL AND UPGRADE OPTIONS

Capgemini India | Morgan Stanley|RT_SPG

Page 31: UNIX Knowledge Document

31 Unix Knowledge Document

The general form of an rpm install command is

rpm {-i|--install} [install-options] PACKAGE_FILE ...

This installs a new package. The general form of an rpm upgrade command is

rpm {-U|--upgrade} [install-options] PACKAGE_FILE ...

This upgrades or installs the package currently installed to a newer version. This is the same as install, except all other version(s) of the package are removed after the new package is installed.

rpm {-F|--freshen} [install-options] PACKAGE_FILE ...

This will upgrade packages, but only if an earlier version currently exists. The PACKAGE_FILE may be specified as an ftp or http URL, in which case the package will be downloaded before being installed. See FTP/HTTP OPTIONS for information on rpm's internal ftp and http client support.

UNIX Shell scripting :

Shells

Capgemini India | Morgan Stanley|RT_SPG

Page 32: UNIX Knowledge Document

32 Unix Knowledge Document

The shell is between user and the operating system, acting as a command interpreter. It reads the terminal inputand translates the commands into actions taken by the system. The shell is analogous to command.com in DOS. When the shell starts up it reads its startup files and may set environment variables, command search paths, and command aliases, and executes any commands specified in these files.

Shells:sh Bourne shellksh Korn Shellcsh Cshellbash Bourneagain shell or advance bourne shelltsh Advanced cshell

Environment Variables

Environmental variables are used to provide information to the programs. Environment Variables can be both global environment and local shell variables. Global environment variables are set by login shell and new programs and shells inherit the environment of their parent shell. Local shell variables are used only by that shell and are not passed on to other processes.

Environment Variables:PATH Paths to be searched for commands,

e.g. /usr/bin:/usr/ucb:/usr/local/bin

Capgemini India | Morgan Stanley|RT_SPG

Page 33: UNIX Knowledge Document

33 Unix Knowledge Document

DISPLAY The graphical display to use, e.g. nyssa:0.0EDITOR The path to your default editor, e.g. /usr/bin/viGROUP Your login group, e.g. staffHOME Path to your home directory, e.g. /home/sriprasadHOST The hostname of your system, e.g. nyssaLOGNAME The name you login with, e.g. frankPS1 The primary prompt string, Bourne shell only (defaults to $)PS2 The secondary prompt string, Bourne shell only (defaults to >)SHELL The login shell you’re using, e.g. /usr/bin/cshTERM Your terminal type, e.g. xtermUSER Your username, e.g. sriprasad

Operators

Relational Operators for string:< less than<= less than or equal to== equal to>= greater than or equal to!= not equal to> greater than

Relational Operators for integers:n1 -eq n2 true if integers n1 and n2 are equaln1 -ne n2 true if integers n1 and n2 are not equaln1 -gt n2 true if integer n1 is greater than integer n2n1 -ge n2 true if integer n1 is greater than or equal to integer n2n1 -lt n2 true if integer n1 is less than integer n2n1 -le n2 true if integer n1 is less than or equal to integer n2

Capgemini India | Morgan Stanley|RT_SPG

Page 34: UNIX Knowledge Document

34 Unix Knowledge Document

Shell Variables

There are a number of variables automatically set by the shell when it starts. These allow you toreference arguments on the command line, those are called shell variables.

Shell Variables: Variable Usage $# number of arguments on the command line $- options supplied to the shell$? exit value of the last command executed$$ process number of the current process

$! process number of the last command done in background

$0 Shellprg filename$1 Parameter 1$2 Parameter 2$3 Parameter 3$4 Parameter 4$5 Parameter 5$6 Parameter 6$7 Parameter 7$8 Parameter 8$9 Parameter 9

Shift

Shift command loses the original first entry(first parameter -$1). It loops through until it has shifted all the arguments off the argument list. ($2 will be assigned to $1 – Likewise till $9, untill all the arguments are shifted we can give shift)

Shift :

$1 hello1

Capgemini India | Morgan Stanley|RT_SPG

Page 35: UNIX Knowledge Document

35 Unix Knowledge Document

$2 hello2

$3 hello3

$4 hello4

$5 hello5

$6 hello6

$7 hello7

$8 hello8

$9 hello9

Suppose if we have tenth parameter passed, we can access it simply giving shift. After shift above values will be reassigned.

Shift : $1 hello2

$2 hello3

$3 hello4

$4 hello5

$5 hello6

$6 hello7

$7 hello8

$8 hello9

$9 hello10

Conditional if

Capgemini India | Morgan Stanley|RT_SPG

Page 36: UNIX Knowledge Document

36 Unix Knowledge Document

The conditions to be tested for are usually done with the test, or [] command . The if and then must be separated, either with a <newline> or a semicolon (;).

if condition1

then

command list if condition1 is true

elif condition2

then command list if condition2 is true

else

command list if condition1 is false

fi

Sample:

#!/bin/sh

if [ $# -ge 2 ]

then

echo $2

elif [ $# -eq 1 ]; then

echo $1

else

echo No input

fi

Capgemini India | Morgan Stanley|RT_SPG

Page 37: UNIX Knowledge Document

37 Unix Knowledge Document

Conditional switch and case

To choose between a set of string values for a parameter use conditional switch case. Any valid filename meta-characters within the patterns to be matched can use conditional switch case. The ;; ends each choice and can be on the same line, or following a <newline>, as the last command for the choice. Additional alternative patterns to be selected for a particular case are separated by the vertical bar, |, as in the first pattern line in the example above. The wildcard symbols,: ? to indicate any one character and * to match any number of characters, can be used either alone or adjacent to fixedstrings.

case parameter in

pattern1[|pattern1a]) command list1;;

pattern2) command list2

command list2a;;

pattern3) command list3;;

*) ;;

esac

#!/bin/bash

case $1 in

Ajay) echo Ajay;;

Usha) echo Usha

Sresh) echo sreesh;;

S*) echo SPG;;

*) echo Default;;

esac

Capgemini India | Morgan Stanley|RT_SPG

Page 38: UNIX Knowledge Document

38 Unix Knowledge Document

while

The while commands let you loop as long as the condition is true.

while condition

do

command list

[break]

[continue]

done

#!/bin/bash

while [ $# -gt 0 ]

do

echo $1

shift

done

Capgemini India | Morgan Stanley|RT_SPG

Page 39: UNIX Knowledge Document

39 Unix Knowledge Document

until

This looping feature is only allowed in the Bourne shell.

The condition is tested at the start of each loop and the loop is terminated when the condition

is true.

until condition

do

command list while condition is false

done

#!/bin/bash

until [ $# -le 0 ]

do

echo $1

shift

done

Notice, though, that here we’re testing for less than or equal, rather than greater than or equal,because the until loop is looking for a false condition.

Both the until and while loops are only executed if the condition is satisfied. The condition isevaluated before the commands are executed.

Capgemini India | Morgan Stanley|RT_SPG

Page 40: UNIX Knowledge Document

40 Unix Knowledge Document

for and foreach

The list_of_values is optional, with $@ assumed if nothing is specified. Each value in this list issequentially substituted for variable until the list is emptied. Wildcards can be used and are applied to file names in the current directory.

for variable [in list_of_values]

do

command list

done

for name in Ajay, sri,sris,usha,sarava

do

echo $name

done

it ll print all the name as follows

Ajay, sri,sris,usha,sarava

for name in Ajay sri sris usha sarava

do

echo $name

Capgemini India | Morgan Stanley|RT_SPG

Page 41: UNIX Knowledge Document

41 Unix Knowledge Document

done

it ll print all the name as follows

Ajaysrisrisushasarava

for ((exp1;exp2;exp3){Statements}

for((i=0;i<=5;i++){echo $i}

Output will be

012345

Capgemini India | Morgan Stanley|RT_SPG

Page 42: UNIX Knowledge Document

42 Unix Knowledge Document

MYSQL Connection In unix

To Search whether mysql is installed

rpm -qa|grep -i mysql

the command will display the mysql, ifinstalled.

Eg:

mysql-server-3.23.52-3

mysql-3.23.52-3

mysql-devel-3.23.52-3

php-mysql-4.2.2-8.0.8

To connect to mysql database$ mysql -u <DBUSER> -h <DBSERVERNAME> –p <pwd>

$ mysql -u sriprasad -h localhost –p ****

Can login in to root(without password)using the following

$ mysql -u root

The prompt will change to below

Capgemini India | Morgan Stanley|RT_SPG

Page 43: UNIX Knowledge Document

43 Unix Knowledge Document

mysql>

“ mysql is connected now”

To know Databases presentmysql>show databases;

+----------+

| Database |

+----------+

| a1 |

| dawood |

| kanbay |

| mercury |

| mysql |

| perl |

| test |

| yas |

+----------+

8 rows in set (0.00 sec)

Capgemini India | Morgan Stanley|RT_SPG

Page 44: UNIX Knowledge Document

44 Unix Knowledge Document

‘show databases’ will list the databases present in current dbserver.

Accessing the Database(Switching Databases)

mysql>use test

Database changed

‘use <databasename>’ will allow the user to get access for the <database>. If we execute ‘use<databasename>’, prompt will display Database changed – it means we have access for the <databasename> now.

To know the tables present in the databaseAfter getting access to the database using use command, to know the tables present in the database, use ‘show tables’ command

mysql>show tables;

+--------------+

| Tables_in_a1 |

+--------------+

| a1_table1 |

+--------------+

1 row in set (0.00 sec)

Creating table

Create table <tablename>(<colname1><datatype1>,…<colnamen><datatypen>);

Create Table example (Kinno INT, empname VARCHAR(100) );

Capgemini India | Morgan Stanley|RT_SPG

Page 45: UNIX Knowledge Document

45 Unix Knowledge Document

Design of the table

Desc <tablename>

Desc will show the table description – structure of the table.

Insert command

insert into <tablename> values (“<col1value>”,”<col2value>”…”<colnvalue>”);

insert into a1_table1 values("1","spg");

Update command

update <tablename> set <colname>=”<value>” where <colname>=”<valule>”

update a1_table1 set id="1" where name="qw";

Delete Command

DELETE FROM <table-name>WHERE <conditions>;

DELETE FROM al_table WHERE id = 1;

Delete [IGNORE] [LOW_PRIORITY] [QUICK]FROM <table name>[WHERE <where definition>][ORDER BY <column name> [ASC | DESC] [{, <column name> [ASC | DESC]}...]][LIMIT <row count>]

Capgemini India | Morgan Stanley|RT_SPG

Page 46: UNIX Knowledge Document

46 Unix Knowledge Document

Select command

<select> [ * | <column name> | <expression>} [[AS] <alias>][{, {<column name> | <expression>} [[AS] <alias>]}...]FROM <table name> [[AS] <alias>][WHERE <expression> [{<operator> <expression>}...]][ORDER BY <column name> [ASC | DESC] [{, <column name> [ASC | DESC]}...]][LIMIT [<offset>,] <row count>]

Select table_name.* from table;

Select * from table;

Select * from a1_table;

Will retrieve all the columns from the a1_table.

Where clause

SELECT column, columnFROM tableWHERE conditions;

Show certain selected rows with the value "spg".

SELECT * FROM [table name] WHERE [field name] = "spg";

Capgemini India | Morgan Stanley|RT_SPG

Page 47: UNIX Knowledge Document

47 Unix Knowledge Document

delete a db.drop database [database name];

delete a table.drop table [table name];

Capgemini India | Morgan Stanley|RT_SPG