Essential Shell Programming

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS 497C – Introduction to UNIX Lecture 32: - Shell Programming Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Introduction to Unix (CA263) Decisions, Decisions By Tariq Ibn Aziz Dammam Community College.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Lab 8 Shell Script Reference:
Shell Control Structures CSE 2031 Fall August 2015.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
Essential Shell Programming by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Awk- An Advanced Filter by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
If condition1 then statements elif condition2 more statements […] else even more statements fi.
Group, group, group One after the other: cmd1 ; cmd2 One or both: cmd1 && cmd2 Only one of them: cmd1 || cmd2 Cuddling (there):( cmd1 ; cmd2 ) Cuddling.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Awk- An Advanced Filter by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Lecture 9 Shell Programming – Command substitution
Shell Programming (ch 10)
Essential Shell Programming
Agenda Control Flow Statements Purpose test statement
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
awk- An Advanced Filter
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Linux Shell Script Programming
Essential Shell Programming
Shell Control Structures
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Essential Shell Programming
Chapter 5 Bourne Shells Scripts
awk- An Advanced Filter
Introduction to Bash Programming, part 3
In the last class, The concept of file system
Review.
Review The Unix Shells Graham Glass and King Ables,
Essential Shell Programming
Presentation transcript:

Essential Shell Programming by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore-560085 shylaja.sharath@pes.edu

Session 2 We will be learning exit Logical operators test if

exit and Exit Status of Command To terminate a program exit is used. Nonzero value indicates an error condition. Example 1: $ cat foo Cat: can’t open foo Returns nonzero exit status. The shell variable $? Stores this status.

exit and Exit Status of Command Example 2: grep director emp.lst > /dev/null:echo $? Exit status is used to devise program logic that branches into different paths depending on success or failure of a command

The logical Operators && and || Two operators that allow conditional execution, the && and ||. Usage: cmd1 && cmd2 cmd1 || cmd2 && delimits two commands. cmd 2 executed only when cmd1 succeeds.

The logical Operators && and || Example1: $ grep ‘director’ emp.lst && echo “Pattern found” Output: 9876 Jai Sharma Director Productions Rohit Director Sales Pattern found

The logical Operators && and || Example 2: $ grep ‘clerk’ emp.lst || echo “Pattern not found” Output: Pattern not found Example 3: grep “$1” $2 || exit 2 echo “Pattern Found Job Over”

The if Conditional Form 1 Form 2 Form 3 if command is successful if command is successful if command is successful then then then execute commands execute commands execute commands fi else elif command is successful execute commands then... fi else... fi If the command succeeds, the statements within if are executed or else statements in else block are executed (if else is present).

The if Conditional #! /bin/sh if grep “^$1” /etc/passwd 2>/dev/null Example: #! /bin/sh if grep “^$1” /etc/passwd 2>/dev/null # ^ matching at the beginning of the line then echo “Pattern Found” else echo “Pattern Not Found” fi

The if Conditional $ emp3.sh ftp Output1: $ emp3.sh ftp ftp: *.325:15:FTP User:/Users1/home/ftp:/bin/true Pattern Found Output2: $ emp3.sh mail Pattern Not Found

Using test and [ ] to Evaluate Expressions Test statement is used to handle the true or false value returned by expressions. Test uses certain operators to evaluate the condition on its right Returns either a true or false exit status Is used by if for making decisions.

Using test and [ ] to Evaluate Expressions Test works in three ways: Compare two numbers Compares two strings or a single one for a null value Checks files attributes Test doesn’t display any output but simply returns a value that sets the parameters $?

Using test and [ ] to Evaluate Expressions Numeric Comparison:

Using test and [ ] to Evaluate Expressions Numeric Comparison: Ex: $ x=5;y=7;z=7.2 1. $ test $x –eq $y; echo $? 1 Not equal 2. $ test $x –lt $y; echo $? 0 True

Using test and [ ] to Evaluate Expressions Shorthand for test [ and ] can be used instead of test. The following two forms are equivalent Test $x –eq $y and [ $x –eq $y ]

Using test and [ ] to Evaluate Expressions String Comparison

Using test and [ ] to Evaluate Expressions Example: #!/bin/sh #emp1.sh checks user input for null values finally turns emp.sh developed previously # if [ $# -eq 0 ] ; then echo “Enter the string to be searched :\c” read pname

Using test and [ ] to Evaluate Expressions if [ -z “$pname” ] ; then echo “You have not entered the string”; exit 1 fi echo “Enter the filename to be used :\c” read flname if [ ! –n “$flname” ] ; then echo “ You have not entered the flname” ; exit 2

Using test and [ ] to Evaluate Expressions grep “$pname” $flname fi Output1: $emp1.sh Enter the string to be searched :[Enter] You have not entered the string

Using test and [ ] to Evaluate Expressions Output2: $emp1.sh Enter the string to be searched :root Enter the filename to be searched :/etc/passwd Root:x:0:1:Super-user:/:/usr/bin/bash

File Tests test can be used to test various file attributes like its type (file, directory or symbolic links) its permission (read, write. Execute, SUID, etc). Example: $ ls –l emp.lst -rw-rw-rw- 1 kumar group 870 jun 8 15:52 emp.lst $ [-f emp.lst] ; echo $? 0  Ordinary file

File Tests $ [-x emp.lst] ; echo $?  Not an executable. 1 $ [ -w emp.lst] || echo “False that file is not writeable” False that file is not writable.

File Tests

Conclusion In this session we have learnt Termination status of a program Command being combined using logical operators Numeric,string and file test operations using test Decision making structure if