View
60
Download
4
Category
Preview:
Citation preview
Shell ScriptingPresentation by V.SANTOSH
1
Agenda We need to know the vi editor before we learn shell scripting. Use vi editor to: create text files edit text files
Our Goal is to create and run a shell scripts2
Vi Editor Vi is a powerful editor Vi stands for visual editor
3
Creating a file with vi Type vi at the shell prompt After pressing enter the command prompt disappears and you see tilde(~) characters on all the lines Tilde characters indicate that the line is blank
4
3 modes in vi1. Command mode (Default mode) 2. Insert mode = to Insert Text 3. Ex mode = To save to save and quit quit without saving etc.
5
Command mode in vi Command mode is the default mode in vi The moment you open the file with vi , you are in command mode In command mode is for : i. Cursor movement ii. Copy iii. Paste (yank) iv. Cut or delete v. Go to etc.6
Cursor movement in vi Cursor movement is possible only in commandmode in vi Arrow keys may work Standard keys for cursor movement are: i. h - for left ii. l - for right iii. j - for down iv. k - for up7
Cursor movement (continued)w - to move one word forward b - to move one word backward $ - takes you to the end of line takes the cursor the the beginning of next line - (Minus) takes the cursor to the beginning of the current line 8
Cursor movement continued ) - moves cursor to the next sentence } - move the cursor to the beginning of next paragraph ( - moves the cursor backward to the beginning of the current sentence { - moves the cursor backward to the beginning of the current paragraph % - moves the cursor to the matching parentheses9
Manipulating text in Command Mode Change (replace) Delete (cut) Yank(copy) Letter cl dl yl Word cw dw yw Line cc dd yy Sentence c) d) y) after Sentence c( d( y( before Paragraph c{ d{ y{ above Paragraph c} d} y} below
10
Put in cursor mode p - Paste the yanked lines from buffer to the line below P - Paste the yanked lines from buffer to the line above (the paste commands will also work after the dd or ndd command (cut a line and paste or cut no. of lines and paste respectively)
11
Navigating in the screen (Command mode) H - takes the cursor to the beginning of the current screen (Home) L - moves to the last line of the screen M - moves to the middle line on the current screen
12
File Positioning (Command Mode) G = takes cursor to last line of file (Go to last line) 1G= takes cursor to first line of file (Go first line) XG=takes cursor to that line of the file (where X is the line no.)
13
Insert Mode Insert Mode = to insert the text Press key to enter insert mode after which you can type the text. a = append after cursor A = Append after line I = insert before cursor I = Insert before line o = opens a new line O = opens new line above14
Undo and redo . u undo most recent change U undo all changes to the current line Ctrl-r redo last "undone" change
15
Search in Command Mode Search as in less or in man pages / to search, n next search in that order N previous search in that order
16
Search/Replace in vi editor Search/Replace as in sed Affects current line by default Use m,n ranges (where m is first line and n is the last line in the range) or % for whole file :1,10s/find string/replace with string/g :%s/find string/replace with string/gi s = search, g=more than one instance per line , i = case insensitive17
Ex mode :q to exit, if no changes are made to the file :q! to quit forcibly abandoning changes :wq to save and Exit :x same as above ZZ to save and Exit (uppercase)
18
Configuring viFew configuration options : :set number :set autoindent :set textwidth=65 (vim only) :set showmatch :set ignorecase
19
Making configurations in vi permanent To make the configurations permanent, you have to add this in ~/.vimrc and ~/.exrc as follows : # vi ~/.vimrc : se nu
20
Types of variables Shell Variables or Local variables Shell variables or local variables exist only in the current shell instance. Environmental variables or global variables. These variables can be passed on to the subshells by exporting the variables.
21
System Variables System Variable Purpose BASH=/bin/bash shell name BASH_VERSION=1.14.7(1) shell version name COLUMNS=80 No. of columns for our screen HOME=/home/student Our home directory LINES=25 No. of columns for our screen LOGNAME=student login name OSTYPE=Linux Our Os type
22
System Variables continued PATH=/usr/bin:/sbin:/bin:/usr/sbin Our path settings PS1=[\u@\h \W]\$ Our prompt settings PWD=/home/student Current working directory SHELL=/bin/bash Our shell name USERNAME=student User name who is currently login to this system23
Examples of System Defined Variables PS1, PS2 PS1=System Prompt 1 ex: PS1= [\u@\h \W]\$ is the default bash prompt PS2=System Prompt 2 Default value of PS2= >
24
Types of varaiables User defined variables = variables defined by the user. Is case sensitive, can be defined in any case. Ex: #c=6 # echo $c 6 System defined variables =Defined by the system used to configure the shell environment System defined variables are in upper case25
Examples of System Defined Variables PS1, PS2 PS1=System Prompt 1 ex: PS1= [\u@\h \W]\$ is the default bash prompt PS2=System Prompt 2 Default value of PS2= >
26
Assigning the output of a command to a variable Using backquotes, we can assign the output of a command to a variable:
filelisting=`ls -l` echo $filelisting
27
Examples of System defined variables (Continued) PATH = Path variable points to the path or directory to search for the executables or command ex: /bin,/sbin LOGNAME=Login name of the user HOME=Home Directory of the user=Default Working directory of the user MAIL=Path where the mail of the user is stored Default path in RHEL for mail /var/spool/mail/28
Examples of System defined variables (Continued) SHELL=Defines the default working shell TERM=Defines the name of the current working terminal
29
Rules for defining UDV and system variable h
UDV and System Variable) (1) Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character. For e.g. Valid shell variable are as follows HOME SYSTEM_VERSION30
Rules for defining UDV and system variable (continued) (2) Don't put spaces on either side of the equal sign when assigning value to variable. For e.g. In following variable declaration there will be no error # num=15
31
Rules for defining UDV and system variable (continued) (3) Variables are case-sensitive, just like filename in Linux. For e.g. #num=15 # NUM=25 # NUM=15 # nUM=21 Above all are different variable name, so to print value 25 we have to use $ echo $NUM32
Rules for defining UDV and system variable (continued) (4) You can define NULL variable as follows (NULL variable is variable which has no value at the time of definition) For e.g. # new= # new="" Try to print it's value by issuing following command $ echo $new Nothing will be shown because variable has no value i.e. NULL variable. (5) Do not use ?,* etc, to name your variable names.33
Exit Status used to check whether command or shell script executed is successful or not. (1) If return value is zero (0), command is successful. (2) If return value is nonzero, command is not successful or some sort of error executing command/shell script. This value is know as Exit Status. echo $? Displays the exit status.34
SHELL SCRIPTING Shell scripts are like batch files Shell scripts are used to : automate the tasks. for repetitive tasks Customising the work environment Executing various administrative tasks
35
Creating a shell script # vi samplescript.sh #!/bin/bash = Magic sh bang sequence which indicates interpreter to use(not compulsory if You are running a bash script in a bash shell) echo hello world
36
Executing a shell script sh samplescript = to execute the script or chmod 700 samplescript (gives full permissions rwx for owner of the file) ./samplescript = to execute the script
37
Shell keywords echo = for output prints whatever is mentioned after that word on the screen ( can be redirected) Ex: echo hello = displays hello read = for input Ex: read name takes input from keyboard and stores the value in the variable name. (other keywords will be discussed throughout This presentation)38
Shell Keywordsecho read if fi else set do case shift esac export break then continue for exit while return until unset readonly umask eval39
Handling Output echo = Used to display on the screen Ex: echo hello world
40
Handling Input read = to take input from keyboard and store the value in the variable Ex: The following script takes the name as input from keyboard and displays the name and welcome statement # vi inout.sh echo n enter your name: read name echo $name,welcome to the Shell Scripting class41
Arithmetic Calculations in Shell Script expr = Expression=For evaluating an arithmetic expression. Ex: #vi calc a=2 b=3 echo sum=`expr $a + $b` echo sum=$sum42
Arithmetic Operators in Shell ScriptingThe following operators can be used with expr in Shell Scripting for calculations 1. Addition + 2. Substract 3. Multiplication \* 4. Division / (Quotient) 5. Modular Division % (Remainder)43
Assignment 2 Write a program to input 2 nos., Calculate and print the following : 1. Sum 2. Difference 3. Product 4. Quotient 5. Remainder44
45
Positional Parameters Positional parameters is a convenient way to pass data to a program. can be considered as variables defined by the shell. These are named $1 to $9 While $0 indicates the program name.
46
Example of Positional Parameters You can write a program to add 2 nos. and pass values to the program. #vi adder echo usage: sh adder $1 $2 ex: sh adder 2 3 sum=`expr $1 + $2` echo $sum
47
Assignment 3 Write a program to pass values to a program from command line and perform the following: Input studentname marks in 5 subjects , Calculate and print the following: Studentname Phy Chem Maths Eng Hin (Marks) Total marks and average marks
48
Using Shift in Positional Parameters shift is a shell builtin that operates on the positional parameters. Each time you invoke shift, it "shifts" all the positional parameters down by one. $2 becomes $1, $3 becomes $2, $4 becomes $3, and so on. By default , moves the positional parameters to the right by one We can shift by more than one By giving the command shift n Where n is the no. of times to shift Ex: shift 3 ,shifts position by 349
shell script example for Shift in positional parameters#vi ppshift #!/bin/bash echo Usage: sh ppshift par1 par2 par3 par4 echo We start with $# positional parameters echo First Parameter is $1" echo Second Parameter is $2 echo Third Parameter is $3 # Shift parameters by one (Default is 1 you can use shift n where #n is the no. of parameters you want to shift) shift echo "You now have $# positional parameters echo First Parameter now is $1" echo Second Parameter now is $2 echo Third Parameter now is $350
51
Structures in Shell Scripting There are 3 types of structures in Shell Scripting : 1. Sequential Structure 2. Conditional Structure 3. Repetitive Structure
52
Sequential Structure In Sequential structure, commands are executed in sequence one after the other. Ex: All the shell scripts we discussed so far are sequential structures.
53
Conditional Structures In conditional structures, Commands are executed based on a condition. Similar to conditional control structures in programming languages, there are some differences. 1. The if condition tests the success of a Linux command, not an expression. 2. The end of an if-then command must be indicated with the keyword fi, and 3. the end of a case command is indicated with the keyword esac.54
Syntax for if ..then Syntax: If [Condition]; then statement elif [Condition]; then statement else statement fi Note: 1) If ,then is used in next statement, ;(semicolon) is not required 2) elif is similar to else if in other programming languages55
Assignment 41) Write a shell script to input 2 nos. and print whether they are equal or not. If not Equal, print the greatest of the 2 nos. 2) Write a shell script to input 2 strings and display whether they are equal or not.
56
case in shell scripts Case is generally used to select from a no. of alternatives We can print a menu and ask the user to choose from a no. of alternatives.
57
Syntax of caseSyntax : Case choice in 1) command ;; 2) command;; 3) Command;; *) echo invalid entry;; Esac Note: 1) * indicates a value other those mentioned in the menu. 2) every option is terminated with ;;
58
Repetitive Structures In repetitive structures, commands are executed repetitively in a loop Ex: 1) for Loop 2) while Loop 3) until Loop
59
Syntax of Syntax: for variable in list of values do statement done Ex: for i in 1 2 3 4 5 do echo $i done Output : 1 2 3 4 5
for loop
60
More Examples of for loop# print nos. from 1 to 10 using for for i in {1..10} do echo $i done Output: Prints nos. from 1 to 10 using for Note: # is used for comment in any shell script statements or commands in a line beginning with # will not be executed.61
For loop with increment other than 1 Example: for i in $(seq 1 3 10) do ||| echo $i (first no, increment, last no.) done Output: 1 3 7 10 will be displayed one below the other.62
For Loop with a decrement of 1 Example: for i in $(seq 10 -2 2) do ||| echo $i (first no, increment, last no.) done Output: 10 8 7 6 4 2 will be displayed one below the other.63
Assignment 51) Write a shell script to print the nos. from 1 to 10 and their squares using for. 2) Write a shell script to print the even and odd nos. from 1 to 10 using for
64
Nested for loops Nested for loops are loops within a loop Generally used for printing patterns etc. The first pass of the outer loop starts the inner loop, which executes to completion. Then the second pass of the outer loop starts the inner loop again. This repeats until the outer loop finishes.
65
Examples of Nested for loops# vi nestedfor for i in {1..5} do for j in (seq 1 1 $i)v do echo n $j done echo Done Output: prints the following pattern 1 12 123 1234 12345
66
Assignment 6 1. Write a program to print the following Patterns : i) 12345 ii) * 1234 ** 123 *** 12 **** 1 *****67
While Loop The commands within a while loop are executed repetitively as long as the condition is true. Syntax : while [ condition] do Command 1 Command 2 done68
While loop example # Program to print the nos. from 1 to 10 using while c=1 while [ $c le 10 ] do echo $c c=`expr $c + 1` done69
Examples of Decrement in While Loop#Program to print the nos. from 1 to 10 In reverse order using while c=1 while [ $c -le 10 ] do echo $c c=`expr $c + 1` done70
Continue,Break and Exit While loop can be interrupted during execution : Continue : Reexamines the loop and restarts the loop Break : Exits the loop Exit : Exits the program itself
71
Until Loop The commands within an until loop are executed repetitively as long as the condition is false. Syntax : until [ condition] do Command 1 Command 2 done72
Until loop Example # Program to print the nos. from 1 to 10 using until c=1 until [ $c gt 10 ] do echo $c c=`expr $c + 1` done73
Assignment 7 Write a program to print the even and odd nos. between 1 and 10 using while and until
74
Assignment 8 Write a shell script to print the nos. from 1 to 10 in reverse order using until.
75
Functions in Shell Scripting Functions are used in Shell Scripting to: 1. Improve program readability 2. Remove repetitive code from the scripts. Note: Shell functions must be declared first
76
Functions syntax Declaring the Function : Function Name () { Command 1 Command 2 } Calling the Function : Function Name77
Example of Functions in Shell Scripting hellofn() { read -p Enter Your Name: name echo $name, Welcome to the Shell Scripting Class } hellofn78
Passing Arguments to functions Passing argument to the functions in shell script is easy. Use $1, $2, .. $n variables that represent arguments in the function just like the positional parameters we discussed earlier. Note: $* indicates all arguments together $# indicates the total no. of arguments79
Examples of Passing arguments to the functionadderfn() { echo value 1=$1 echo value 2=$2 sum=`expr $1 + $2` echo sum=$sum } adderfn 2 380
Assignment 9 Write a shell script using functions and passing arguments to functions for : 1. Copy 2 files 2. Move a file or rename filea to fileb
81
Getopts This command is used to check valid command line argument are passed to script. Usually used in while loop. Syntax: getopts {optstring} {variable1} getopts is used by shell to parse command line argument.82
Getopts continued "optstring contains the option letters to be recognized; if a letter is followed by a colon, the option is expected to have an argument, which should be separated from it by white space. Each time it is invoked, getopts places the next option in the shell variable variable1, When an option requires an argument,83
Getopts continued getopts places that argument into the variable OPTARG. On errors getopts diagnostic messages are printed when illegal options or missing option arguments are encountered. If an illegal option is seen
84
Example of getopts Consider a script : System details Sys -d -c -os -h Options: Optional arguments -d date -c calendar -os Operating system -h hostname -d demo values (if any of the above options are used their values are not taken)85
getopts exampleecho "Usage: Scriptname -n -a -s -w d echo -d date echo -c calendar echo -os Operating system echo -h hostname echo " x if any of the above options are used "86
87
Recommended