CS 311 - Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.

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.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
CS Lecture 04 Outline Change your shell Sed and awk Tricks with Bash scripts Assignment 1 discussion 1CS 311 Operating SystemsLecture 04.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
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 Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
Introduction to Shell Script Programming
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.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
An Introduction to Unix Shell Scripting
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 11: Shell.
CS465 - UNIX The Bourne Shell.
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.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
Writing Scripts Hadi Otrok COEN 346.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Chapter Six Introduction to Shell Script Programming.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
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]
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Linux Administration Working with the BASH Shell.
Shell Control Structures CSE 2031 Fall June 2016.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Computer Programming Batch & Shell Programming 9 th Lecture 김현철 컴퓨터공학부 서울대학교 2009 년 가을학기.
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Lecture 9 Shell Programming – Command substitution
Writing Shell Scripts ─ part 3
What is Bash Shell Scripting?
Linux Shell Script Programming
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Review.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03

What is shell? Command interpreter Lies between user and operating system Text-based Provides access to all UNIX user-level programs – Start programs – Manage running programs (job control) – Connect together programs (pipes) – Manage I/O to and from programs (redirection) 2CS 311 Operating SystemsLecture 03

What is shell script? Collection of shell commands or programs High level programming language – Variables, conditionals, loops, etc. Dynamic – No compiling, no variable declaration, no memory management Powerful/efficient – Do lots with little code – Can interface with any UNIX program String oriented 3CS 311 Operating SystemsLecture 03

When to use and not to use shell scripts? When To Use – Automating frequent UNIX tasks – Doing complex commands – As a glue to connect together other programs When not to use – Resource-intensive tasks, especially where speed is a factor (sorting, hashing, recursion) – Cross-platform portability required (use C or Java instead) – Complex applications, where structured programming is a necessity (type-checking of variables, function prototypes, etc.) 4CS 311 Operating SystemsLecture 03

Sha-bang or the hash-bang character Shell script may have an extension “.sh” To invoke a shell script prompt> sh script_name.sh Or we can let the command line interpret the script using a sha-bang or a hash-bang character (“#!”) – Add “ #!/bin/sh ” as the first line – chmod +x script_name.sh –./find_large.sh In scripts, comments are written using “#” Example: “#This is a comment” 5CS 311 Operating SystemsLecture 03

Command line arguments to shell program Command line parameters – Example: “cut –d’:’ –f1 sample” – cut, –d: and –f1 are passed as arguments to the script – In shell script, args are accessible as $1, $2, $3, etc. $0 will be the shell command invoked (‘cut’ in the example above) $# is the number of parameters $* contain all parameters 6CS 311 Operating SystemsLecture 03

Environment variables Built-in variables – $HOME – full path of home directory – $PATH – List of directories to search – $MAIL – full path of mailbox – $USER – username – $SHELL – full path of the login shell – $TERM – type of your terminal – $PWD – print current working directory Supports all variables shown by the “env” command 7CS 311 Operating SystemsLecture 03

Wildcards Characters used anywhere (esp in regular expressions) – * - matches zero or more characters – ? – matches exactly one character – [ ] – defines a range of characters or list of characters Example – “ls file*” would print all files with filename “file” and “file…” – “ls ????” matches all files with 4 letters – “ls file[0-9]” matches filename with “file093” or “file ” etc. 8CS 311 Operating SystemsLecture 03

Using variables in shell Variables are any string that hold some value In bash scripts, variables need not be declared before using it. By default, bash treats all variable values as strings Example – value=123 (beware of spacing. No whitespaces before or after equal sign) To access the value of variable use “$” sign. – Example: “echo $value” prints 123 and “echo value” prints value 9CS 311 Operating SystemsLecture 03

Exit status Terminate a shell program using “exit” command Typically the program exits with a numbered status indicating success or failure – In UNIX, “exit 0” means success and “exit some_number” means failure Exit status useful in determining whether the command executed properly. $PIPESTATUS or $? shows the status of last executed command/process Lecture 03CS 311 Operating Systems10

Quoting Very important to understand each style of quotes and their functions. Single quote (‘ ‘): inhibit wildcard substitution, variable and command replacement. – Ex. echo '$name* is true' will print $name* is true Double quotes (“ “): inhibit wildcard replacement only – Ex. echo “$name* is true” will print John* is true if name=John Tilde quotes (` `): used for command substitution – Ex. echo `date` will print the system date and time 11CS 311 Operating SystemsLecture 03

Arithmetic operators Since all variables and its values are considered as string by default, we need to inform the shell interpreter we must use the values as numbers when doing arithmetic operations (using expr) echo “5 + 5” will print echo `expr 5 + 5` will print 10 (notice the use of tilde quotes) Other arithmetic operators – expr $value1 - $value2 – expr $value1 * $value2 – expr $value1 / $value2 Lecture 03CS 311 Operating Systems12

Conditional Statements General Structure if [ condition ]#I have put multiple spaces just to then#emphasize the spacing … elif [ condition ] then … else … fi Make note of spacing between if and [, [ and condition, condition and ] 13CS 311 Operating SystemsLecture 03

Specify conditions in bash For numeric comparisons – “-lt” – less than – “-gt” – greater than – “-le” – less than or equal to – “-ge” – greater than or equal to – “-eq” – equal to Example: if [ $value -lt 5 ]#$value must be a number then echo “true” fi Lecture 03CS 311 Operating Systems14

Conditions in bash Relational operators – “-a” – AND – “-o” – OR – “-n” – NOT Example: if [ $value –gt 5 –a $value –lt 10 ] We can also use extended conditions with “if” by using “if [[ condition ]]” – By using extended conditions we can use our well known operators like, ==, &&, || String comparisons – Example: if [ “hello” = “$value” ] – Check whether string is null – if [ -z $string ] Lecture 03CS 311 Operating Systems15

Testing files Separate operators exists to test files in bash – “-e” file exists – “-s” file exists and not empty – “-f” regular file – Check this link: Lecture 03CS 311 Operating Systems16

Looping While loop while [ condition ] do … done Lecture 03CS 311 Operating Systems17 for loop for arg in list do … done Check this link for more options on how to use the “list” in “for” - Example: for file in `ls.` will list through all files in the current directory (a hint for the assignment)

Debugging a shell script Use the command “set” within the script like shown below set –xv …#block of statements to test set – set –x - Print commands and their arguments as they are executed. Also substitutes variable values and evaluates wildcards. set –v - Print shell input lines as they are read. set – simply turns all the set flags off More info on set - Lecture 03CS 311 Operating Systems18

Getting user input “read” command is used to get user input Example: echo “Enter a number” read number#value gets stored in number Using read to read lines from file while read line < file do … done Will read the file line by line till the last line Lecture 03CS 311 Operating Systems19