System Programming. Session 6 Shell Scripting

Similar documents
System Programming. Unix Shells

Lecture 8. Introduction to Shell Programming. COP 3353 Introduction to UNIX

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Vi & Shell Scripting

Shell script. Shell Scripts. A shell script contains a sequence of commands in a text file. Shell is an command language interpreter.

5/20/2007. Touring Essential Programs

Assignment clarifications

Bourne Shell Reference

COMP 4/6262: Programming UNIX

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Mauro Ceccanti e Alberto Paoluzzi

CENG 334 Computer Networks. Laboratory I Linux Tutorial

Here redirection. Case statement. Advanced Unix Tools Lecture 6 CS214 Spring 2004 Friday March 5, 2004

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Esercitazione Introduzione al linguaggio di shell

PROGRAMMAZIONE I A.A. 2015/2016

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

CS Unix Tools. Lecture 3 Making Bash Work For You Fall Hussam Abu-Libdeh based on slides by David Slater. September 13, 2010

CSCI2467: Systems Programming Concepts

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University

Chapter 9. Shell and Kernel

UNIX Essentials Featuring Solaris 10 Op System

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

CHAPTER 2 THE UNIX SHELLS

Shell Programming (bash)

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

EECS2301. Lab 1 Winter 2016

Bash scripting basics

A shell can be used in one of two ways:

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

S E C T I O N O V E R V I E W

CMPS 12A Introduction to Programming Lab Assignment 7

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Linux shell programming for Raspberry Pi Users - 2

The Unix Shell & Shell Scripts

LINUX Scripts de Bash Shell. Exercise 1 - Working with Files

Introduction to Linux Part 2b: basic scripting. Brett Milash and Wim Cardoen CHPC User Services 18 January, 2018

EECS2301. Example. Testing 3/22/2017. Linux/Unix Part 3. for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done

Topic 2: More Shell Skills

UNIX shell scripting

Processes and Shells

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

Last Time. on the website

Shells and Shell Programming

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

Chapter 5 The Bourne Shell

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Review of Fundamentals

Shells and Shell Programming

Sub-Topic 1: Quoting. Topic 2: More Shell Skills. Sub-Topic 2: Shell Variables. Referring to Shell Variables: More

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Lecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions?

Examples: Directory pathname: File pathname: /home/username/ics124/assignments/ /home/username/ops224/assignments/assn1.txt

Prof. Navrati Saxena TA: R. Sachan

BASH and command line utilities Variables Conditional Commands Loop Commands BASH scripts

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

Introduction to Linux. Fundamentals of Computer Science

Shell programming. Introduction to Operating Systems

Introduction: What is Unix?

Useful Unix Commands Cheat Sheet

Operating System Interaction via bash

UNIX System Programming Lecture 3: BASH Programming

Linux & Shell Programming 2014

Environment Variables

bash Data Administrative Shell Scripting COMP2101 Fall 2017

CS Unix Tools & Scripting

12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Shells & Shell Programming (Part B)

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

Mills HPC Tutorial Series. Linux Basics II

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

More Linux Shell Scripts

CSC UNIX System, Spring 2015

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

Shell script/program. Basic shell scripting. Script execution. Resources. Simple example script. Quoting

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

Topic 2: More Shell Skills. Sub-Topic 1: Quoting. Sub-Topic 2: Shell Variables. Difference Between Single & Double Quotes

Why Bourne Shell? A Bourne Shell Script. The UNIX Shell. Ken Wong Washington University. The Bourne Shell (CSE 422S)

Script Programming Systems Skills in C and Unix

Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

CISC 220 fall 2011, set 1: Linux basics

Chapter 4. Unix Tutorial. Unix Shell

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Operating Systems. Copyleft 2005, Binnur Kurt

Computer Systems and Architecture

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

Linux Systems Administration Shell Scripting Basics. Mike Jager Network Startup Resource Center

DATA 301 Introduction to Data Analytics Command Line. Dr. Ramon Lawrence University of British Columbia Okanagan

Why learn the Command Line? The command line is the text interface to the computer. DATA 301 Introduction to Data Analytics Command Line

Scripting OS X. Armin Briegel. Mac Admin, Consultant and Author

9.2 Linux Essentials Exam Objectives

Shell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala

Introduction to Shell Scripting

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Transcription:

System Programming Session 6 Shell Scripting

Programming C Programming vs Shell Programming

C vs Shell Programming Compilation/Direct execution C Requires compilation while shell script can be directly run Programming constructs/facilities Common constructs like variables, if conditions, loops, arithmetic Program complexities Size of program, number of files, % code in total application code Usage Installations, security checks, complex algorithms

Different Shells Bourne Shell Original shell written by Steve Bourne for Unix /bin/sh Bash shell Bourne Again shell! Superset of Bourne GNU Shell Most commonly used on Linux /bin/bash Other shells - C Shell, K Shell, t cshell

First Shell Program! shell to use Comments #!/bin/bash # My first attempt to write shell program # It lists all files in current directory ls Code line Shell script execution starts from first line unlike C where it starts from main function

Quick look at real script on Linux #!/bin/bash # # httpd Startup script for the Apache HTTP Server # Source function library.. /etc/rc.d/init.d/functions # Path to the apachectl script, server binary, and short-form for messages. apachectl=/usr/sbin/apachectl httpd=${httpd-/usr/sbin/httpd} prog=httpd pidfile=${pidfile-/var/run/httpd/httpd.pid} /etc/init.d/httpd # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; esac

Shell wildcards review All files * Example #ls code* for all files starting with code Specific character? Example #ls code?.c Any character from range Example #ls code[12].c No character from range [ijk] [!ijk] Example #ls code[!12].c Home directory name ~ Use backslash (\) to avoid replacement Example #ls code\?.c

Shell wildcards test! $ls *[i-n] $ls pr?.c $ls?i* $ls *

I/O Redirection Standard Input redirection < Standard output redirection > Standard output redirection upend >> Redirection in commands What will be outcome of this command? #cat > list.txt

Passing Arguments How do we enhance our first program to list files from any directory? Command line arguments can be passed to shell script Each argument can be accessed in script as $1,$2 $# provides number of arguments given while invoking script $* provides all arguments in one string

Second Shell Program Let us enhance our script to take directory name as argument for listing file Comments #!/bin/bash #Prints all files in specified directory Accessing argument Echo Listing of files ls $1/* exit(0) Exit statement

Variables You can define and use variables in shell program Paring of var-name>=<val> sets the variable Example OS=Linux, TODAY=26NOV Our real script example of /etc/init.d/httpd script had apachectl=/usr/sbin/apachectl You can derive values for variable using command output TODAY=`date` String quoted in tilda is interpreted as command You can set values from command line argument DIR1=$1, DIR2=$2 Variable name can t be number or start with number 10page=book

Shell Program with variables Setting value Accessing value #!/bin/bash #Prints all files in specified directories DIR1=$1 DIR2=$2 echo "Listing from $DIR1" ls $DIR1/* echo "Listing from $DIR2" ls $DIR2/*

System Variables What is differences between following methods of script execution? $sample1.sh $./sample1.sh

System Variables System defined variables which can be used directly on command line or in script Name PATH HOME LOGNAME Meaning Directories searched for executable /bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin: Home directory of user invoking script Login name of user invoking the script OSTYPE Type of Unix e.g. linux-gnu PWD Current director

System Variables $sample1.sh Searched from PATH variable value $./sample1.sh Searched in current working directory Which directory is searched Here? $../sample1.sh

Reading Arguments How do we enhance our script to prompt user to specify directory to list files? Use echo command to prompt message Use read statement to read input Store input in variables Access variables as discussed earlier

Interactive Shell Program read value use value #!/bin/bash #Prints all files in specified directories Echo Enter first directory name read DIR1 Echo Enter second directory name read DIR2 echo "Listing from $DIR1" ls $DIR1/* echo "Listing from $DIR2" ls $DIR2/*

Arithmetic Operations expr command is used to perform operation Example: expr 4 + 5 Spaces are important Supported Operations Addition (+) Subtraction (-) Multiplication (*) Division (/) Modulus (%) expr 4.5 + 2 Only numerical numbers allowed

Shell Program with arithmetic #!/bin/bash Echo Enter first directory name read DIR1 Echo Enter second directory name read DIR2 count1=`ls $DIR1/* wc l` count2=`ls $DIR2/* wc l` total=`expr $count1 + $count2` echo total file count = $total