Shell programming. Introduction to Operating Systems

Similar documents
Conditional Control Structures. Dr.T.Logeswari

Bourne Shell Reference

bash Execution Control COMP2101 Winter 2019

CS Unix Tools & Scripting

COMP 4/6262: Programming UNIX

Control Structures. CIS 118 Intro to LINUX

Computer Systems and Architecture

INd_rasN SOME SHELL SCRIPTING PROGRAMS. 1. Write a shell script to check whether the name passed as first argument is the name of a file or directory.

Bash scripting basics

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

Week 6 Lesson 1: Control-Flow Statements (Continued)

Preview. Review. The test, or [, command. Conditions. The test, or [, command (String Comparison) The test, or [, command (String Comparison) 2/5/2019

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

Computer Systems and Architecture

CS214 Advanced UNIX Lecture 4

Lab 4: Shell scripting

Shell Programming (bash)

EECS2301. Special variables. $* and 3/14/2017. Linux/Unix part 2

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

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

Bash shell programming Part II Control statements

A shell can be used in one of two ways:

Title:[ Variables Comparison Operators If Else Statements ]

Useful Unix Commands Cheat Sheet

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Lab 5a Shell Script Lab 4 Using Arithmetic Operators in shell script

Shell Control Structures

Shells & Shell Programming (Part B)

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Shell Control Structures

UNIX shell scripting

CSC 2500: Unix Lab Fall 2016

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

While Statement Examples. While Statement (35.15) Until Statement (35.15) Until Statement Example

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

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

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

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

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

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

Prof. Navrati Saxena TA: R. Sachan Bharat Jyoti Ranjan

Програмиранев UNIX среда

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement)

LOG ON TO LINUX AND LOG OFF

Practice 5 Batch & Shell Programming

Assignment clarifications

There are some string operators that can be used in the test statement to perform string comparison.

Shells and Shell Programming

Last Time. on the website

UNIX Programming Laboratory. Subject Code: 10MCA17

Vi & Shell Scripting

CS1101: Lecture 9 The Shell as a Programming Language

Shells and Shell Programming

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

Shell Programming (Part 2)

Shell Start-up and Configuration Files

Csci 132 Spring 13. Assignment 2 Due: Tuesday, March 5 (by 11:59PM)

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

Scripting. More Shell Scripts. Adapted from Practical Unix and Programming Hunter College

Linux shell programming for Raspberry Pi Users - 2

Chapter 5 The Bourne Shell

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

What is Bash Shell Scripting?

Basic Linux (Bash) Commands

More Scripting Techniques Scripting Process Example Script

BASH Programming Introduction

Practical 04: UNIX / LINUX Shell Programming 1

Bourne Shell Programming Topics Covered

Lecture 02 The Shell and Shell Scripting

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2018

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

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2017

Lecture 5. Essential skills for bioinformatics: Unix/Linux

UNIX Shell Scripts. What Is a Shell? The Bourne Shell. Executable Files. Executable Files: Example. Executable Files (cont.) CSE 2031 Fall 2012

Introduction to Shell Scripting

CSE 390a Lecture 6. bash scrip'ng con'nued; remote X windows; unix 'dbits

Review. Preview. Functions. Functions. Functions with Local Variables. Functions with Local Variables 9/11/2017

Essentials for Scientific Computing: Bash Shell Scripting Day 3

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

Linux 系统介绍 (III) 袁华

9.2 Linux Essentials Exam Objectives

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

Introduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Essential Skills for Bioinformatics: Unix/Linux

Answers to Even-numbered Exercises

Assignment 3, Due October 4

CS251 Programming Languages Handout # 29 Prof. Lyn Turbak March 7, 2007 Wellesley College

Linux Bash Shell Scripting

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

Advanced bash scripting

Multiple Choice - 42 Questions - 10 of 20%

RHCE BOOT CAMP. System Administration

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

CSE 390a Lecture 6. bash scripting continued; remote X windows; unix tidbits

Unix Shell scripting. Dr Alun Moon 7th October Introduction. Notation. Spaces

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

Shell Programming 1/44

Transcription:

Shell programming Introduction to Operating Systems

Environment variables Predened variables $* all parameters $# number of parameters $? result of last command $$ process identier $i parameter number i $0 name of the script Positional variables $0, $1, $2,..., $9 $0 name of the script, $1 rst parameter, $2 second,... shift (useful when there are more than 10 parameters) Local variables variable=value (value is a string. NOT a number!) unset variable (variable is removed) Examples message= Hi, how are you Hi, how are you text counter=100 100 text message=hi_how_are_you Hi_how_are_you text Introduction to Operating Systems Shell programming 2

Commands echo writes in the standard output the text passed as argument Examples: echo hello # writes hello echo $x # writes the value of the variable x Special characters (used with the -e option): \c No return at the end \n New line \f New page \t Tabulator \r writes Return (returning to the start of the line) read allows reading values from the keyboard, assigning the values to the variables passed as parameters Example: echo e Insert the price: \c read price Introduction to Operating Systems Shell programming 3

Commands expr Used for basic arithmetic operations, by default writes the result in the standard output The following operators are possible: + Addition example: expr 3 + 2 - Subtraction example: expr 5-2 * Multiplication example: expr 6 \* 4 / Integer division example: expr 18 / 7 % Rest of integer division example: expr 18 % 7 The * character being special (meta-character), it is necessary to put the \ character before in order not to be considered as special Operator priority: %, *, /, +, - Examples: expr 3 + 4 # 7 will be written in the standard output x=`expr 3 + 4` # accents are necessary to evaluate as an operation expr `expr $x + $y` / `expr $a + $b` Introduction to Operating Systems Shell programming 4

Commands test Condition-expression: Parameters can be variables, constants, strings or les / directories. Syntax: test arg1 -option arg2 # [ ] can be used instead of test Options: Numbers Strings Files / Directories -eq equal = equal -a exists -ne not equal!= not equal -s exists, not empty -gt greater than -z null string -f regular le -ge greater than or equal -n not null string -d directory -lt less than -le less than or equal Condition-expressions can be combined via logic operators: -a AND -o OR -r read permission -w write permission -x exec. permission Introduction to Operating Systems Shell programming 5

if Conditional statement Syntax: a) if condition instructions b) if condition else Conditional statement: if instructions1 instructions2 condition: program / command TRUE when result is 0 FALSE when result is not 0 c) if condition1 instructions1 elif condition2 instructions2 Introduction to Operating Systems Shell programming 6

Conditional statement: if Examples: if test $n -gt 10 echo $n is greater than 10 if [ $# -lt 1 ] # error echo error: at least two parameters are needed exit 1 else # correct. Introduction to Operating Systems Shell programming 7

Conditional statement: case case multiple option sequence Syntax: a) case expression in pattern1) instructions1 ;; pattern2) instructions2 ;; pattern3) instructions3 ;;... *) instructions esac Introduction to Operating Systems Shell programming 8

Conditional statement: case Example: read var1 case $var1 in A) echo A has been chosen ;; B) echo B has been chosen ;; C) echo C has been chosen ;;... *) echo Invalid option esac Introduction to Operating Systems Shell programming 9

Iterative statement: while Set of instruction than will be repeated while the condition holds while Syntax: while condition instructions condition: it is a program TRUE when result is 0 FALSE when result is not 0 Introduction to Operating Systems Shell programming 10

Set of instruction than will be repeated until the condition holds until Iterative statement: until Syntax: until condition instructions condition: it is a program TRUE when result is 0 FALSE when result is not 0 Introduction to Operating Systems Shell programming 11

for Iterative statement: for Set of instructions that will be executed for each of the values passed Syntax: for variable in value_list instructions Introduction to Operating Systems Shell programming 12

Examples: Iterative statement: for for i in $* echo ===== $i parameter ===== for i in Ane Kol Miren echo Hello $i for number in 1 2 3 4 5 6 7 8 9 10 echo Message: $number Introduction to Operating Systems Shell programming 13

1.- Write the name of the script echo $0 Examples 2.- Write the number of users that are currently connected echo number of users: `who wc -l` 3.- Check if a given user (whose name is passed as parameter) is connected or not if who grep $1 >/dev/null echo $1 is connected Introduction to Operating Systems Shell programming 14

4.- Echo of the script name and its parameters echo $0 for i in $* echo $i Examples 5.- Write the rst 10 lines of les in the current directory whose name ends by.h, putting a header before each le for i in *.h echo ===== $i le ===== head $i Introduction to Operating Systems Shell programming 15

Examples 6.- Similar to the previous exercise, bat endings are passed as parameters and verifying that les are regular. It veries also that there is at least one parameter if [ $# -lt 1 ] echo "error: no parameters" exit 1 for type in $* for i in *.$type if [ -f $i ] echo "===== $i le =====" head $i Introduction to Operating Systems Shell programming 16

Examples 7.- Read a number, and if it is greater than 10 write a message and loop wn until 10 echo e Give me a number:\c read n if test $n -gt 10 #also: if [ $n gt 10 ] echo It is greater than 10" while [ $n ge 10 ] echo $n n=`expr $n 1` Introduction to Operating Systems Shell programming 17

Examples 8.- Write the last 10 lines of the les passed as parameters, putting a header before. It veries the existence of parameters, as well as the existence of the les passed if [ $# -lt 1 ] echo "error: parameters are required" exit 1 for i in $* if [ -f $i ] echo "===== $i le =====" tail $i Introduction to Operating Systems Shell programming 18

Examples 9.- Analyze the current directory and its subdirectories (only one level wn), writing the names of the les and directories encountered for i in * if [ -d $i ] echo Directory: $i/" for j in $i/* if [ -d $j ] echo " Sub-directory: $j/" if [ -f $j ] echo " Sub-le: $j" if [ -f $i ] echo "File: $i" Introduction to Operating Systems Shell programming 19