unix
TRANSCRIPT
© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved. 1
HSBC Global Technology
UNIX
-Kirtikumar Shinde
2© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
Topics
1. Introduction
2. Unix Filesystem
3. File Permissions
3. Advanced Commands
4. Shell
5. Shell Programming
6. AWK
7. My Learnings
8. Questions?
3© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
Introduction
Multi-user and Multitasking OS
Mostly Written in C at Bell Labs
Most of the servers are on Unix
Unix FlavorsSolaris
HP
AIX
Linux
Current DS Server – Which Unix?
4© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
Introduction
Architecture
5© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
Unix File System
An upside-down Tree/
etc bin export
home
user
ad s3910120
dev tmp
exam.txt work hobby.c proj1
date cal. . .
. . . . .
. .
. . .
. . . . . .. . .
. . .
6© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
File permissions
Changing permissionschmod g+w empl.txt
chmod 754 empl.txt
Changing ownershipchown gcdv_dev empl.txt
Changing groupchgrp develp empl.txt
reading
othersgroupowner
1 0 01 0 11 1 1-r -r - xr xw
executionwriting
7© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
Advanced Commands
SortSorts the contents of a file.
sort [-b f n r o t] [file name(s)]
Takes the contents of a file(s) and displays it in sorted order. Flags:
-b ignores blanks-f change all lower case letter to upper case before comparing-n numeric sort-r reverse usual order-o sends output of command to some file-t field delimiter
E.g. To sort the Emp.txt on 2nd field in reverse order $sort -t, -n -r +2 Emp.txt
8© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
Advanced Commands
grepSearches a file for pattern.
grep [-c i l v] <pattern> [file name(s)]
Takes the contents of a file(s) and displays it in sorted order.Flags:
-c displays count of matching lines.-i ignore the case while searching-l lists the names of files that matches pattern-v displays all lines that don’t contain the pattern
E.g. To display all lines in the ‘Emp.txt’ that match either the abc or xyz string: $grep "abc|xyz" Emp.txt
9© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
Advanced Commands
tar
Compression and decompression of files. tar -(x|c) [ v f r] <tarfile> [file name(s)]
Flags:-x extract from file.-c create new extract file.-v displays list files-f name of file follows-r append to old extract.
E.g. To compress file Emp.txt $tar -cvf Emp.tar Emp.txtTo decompress file Emp.tar $tar -xvf Emp.tar
10© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
Shell
Command interpreter that waits for commands, executes them and displays the results
Bourne shellDeveloped by Steve Bourne at AT&T
Korn shellDeveloped by David Korn at AT&T
C-shellDeveloped by Bill Joy for Berkeley Unix
11© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
Shell
Shell works as follows:Shell displays a prompt.
You type in a command.
You press the return key.
The shell interprets the commands typed and tries to find the correct programs to run.
The kernel runs the requested programs and returns the results to the shell.
The shell displays the command prompt again
12© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
Shell
Which Shell I am in ?finger –m myusername
Know your shell – Korn Shell – its featuresAliases – It allows shorthand for commands.Command history – Lets you recall previously entered commands.Command line editing – Allows us to edit commands vi style.Integrated programming features – It enables common programming tasks to be done cleanly & without creating extra processes.Support regular expressions.Advanced I/O features – Ability to carry out two way communication with concurrent processes.Increased speed of shell code execution.Has highly robust security features.
13© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
Shell system variablesPATH = Defines path shell must search in order to execute any command or file.
HOME = Indicates default working directory of the user.
PS1 = System prompt 1.
PS2 = System prompt 2, default value is “>”.
Shell
14© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
UNIX Shell Programming
Shell Script (Shell Procedure)A program written in shell programming language is known as a shell script or shell procedure.
Shell Programming LanguageThe shell programming language is a command language with a lot of features common to many computer programming languages, including the structured language constructs: sequence, selection, and iteration.
Command LanguagesCommand languages are interpreted languagesIt allows then use of Unix commands in scripts
15© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
UNIX Shell Programming
Shell scripting keywords.Please Note : Script variables shouldn’t be same as keywords.
Looping constructs, to be discussed in coming slides.
set, unset
Readonly
Exit
Ulimit
Umask
16© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
UNIX Shell Programming
Create - Simple Hello World shell script.
#!/bin/sh
echo "Hello World“
Make Files Executable$ chmod 777 HelloWorld.sh
Execute the shell scriptObserve the output ‘Hello World’ will be between two command prompts.$ HelloWorld.sh
17© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
UNIX Shell Programming
AssignmentsValue assignments.String assignment – var=“Your Name”
Think if not within double quotes.
$echo $varthink if not $
Making variable readonly.a=50readonly a
18© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
UNIX Shell Programming
echo and Escape CharactersDisplay strings as “printf” in C
The echo command recognizes escape characters. Escape characters all start with the backslash ( \ ), and you can use them as part of the echo command argument string.
E.g.$ echo “\nHello\n”
19© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
UNIX Shell Programming
Passing parameters to a Shell scriptConsider Welcome.sh, & we are sending name as parameter to a Shell Script.
Welcome.sh Vikram
think If you want to send First name & Last name as one parameter.
In above case 0th parameter value considered will be script name.1st parameter value considered will be name, i.e.Vikram.
Accessing parameters in a Shell scriptParameter to a shell script starts from index 1. Its value is retrieved by using $1 & so on. … Think how you will access parameter beyond 9th parameter.
20© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
UNIX Shell Programming
Shell Positional Variable$0 : command itself
$1 : first parameter
$n : nth parameter
$# : no. of parameters
$? : exit status of last command executed
$* : all parameters
$$ : process number of shell
$! : PID of last background process
21© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
UNIX Shell Programming
if-then and if-then-elseThe if-then Construct
if [ condition ]
then
true-commands
fi
The if-then-else Construct
if [ condition ]
then
true-commands
else
false-command(s)
fi
22© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
UNIX Shell Programming
if-then-elif
if [ condition1 ]
then
commands_1
elif [ condition2 ]
then
commands_2
else
commands_n
fi
23© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
UNIX Shell Programming
test CommandThe test command is a built-in shell command that evaluates the expression given to it as an argument and return true if the expression is true, if otherwise, false.You can use square brackets ( [ ] ) instead of the word test.Example
if test $str1 = $str2 then
echo “Something”fi
if [$str1 = $str2] then
echo “Something”fi
24© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
UNIX Shell Programming
Logical Operators-a AND Operator-o OR Operator! NOT Operator
Numeric Test Operators-eq Is number1 equal to number2 ?-ne Is number1 not equal to number2 ?-gt Is number1 great than number2 ?-ge Is number1 great than or equal to number2 ?-lt Is number1 less than number2 ?-le Is number1 less than or equal to number2 ?
E.gif test $var –lt 10
thenecho “Something”
fi
25© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
UNIX Shell Programming
String Test Operatorsstring1 = string2 Does string1 match string2?string1 != string2 Does string1 not match string2?-n string Does string contain characters?-z string Is string an empty string?
E.g.if test $str1 = $str2
thenecho “Something”
fi
Think if no space on both sides of an operator
26© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
UNIX Shell Programming
File Test Operators-s file True if File size is greater than 0-f file True if File exists & not a directory-d file True if file exists & is a directory file. -r file True if file exists & you have read permission on it.-w file True if file exists & you have write permission on it.-x file True if file exists & you have execute permission on it.
E.gif [ –f $filename ]then
echo “Something”fi
Think if no space on both sides of an option.
27© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
UNIX Shell Programming
The expr CommandArithmetic Operators+ : Addition operator
- : Subtraction operator
/ : Division operator
* : Multiplication operator
% : Remainder operator
ExpressionsArithmetic expressiona=`expr $a + $b` … think if no space both the sides of + & space both the sides of =.
Floating point arithmetic expression a=`expr $a + $b | bc`String expressionpath=$path1”/”$path2
28© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
UNIX Shell Programming
While Loop
while [ condition ]docommand(s)done
For Loop
for variable in list-of-valuedocommand(s)done
29© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
AWK
Introduction to awk.awk is a programming language designed to search for, match patterns, and perform actions on files. awk programs are generally quite small, and are interpreted.
awk scans input lines one after the other, searching each line to see if it matches a set of patterns or conditions specified in the awk program. For each pattern, an action is specified. The action is performed when the pattern matches that of the input line.
Sample awk command:awk ‘/pattern/’’{print $0}’ file1
30© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
My Learnings
When I will go for Shell scripting.Automating my regular tasks.
Customizing my work environment.
Task is pretty simple
When I will avoid Shell scripting.It needs interaction with multiple applications.
Problem is relatively complex involves more than one tool.
Lookups or finding data.
31© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
Questions ?
32© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.
Thank You