CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)

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.
More about Shells1-1 More about Shell  Shells (sh, csh, ksh) are m Command interpreters Process the commands you enter m High-level programming languages.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
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
Integer variables #!/bin/csh # sum of numbers from $argv[1] to $argv[2] set low = $argv[1] set high = $argv[2] set sum = 0 set current = $low while ( $current.
Linux+ Guide to Linux Certification, Second Edition
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang
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.
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.
UNIX Shell Scripting > Echo “A quick how-to”. What is shell scripting? Interpreted (non-compiled) language Slower compared to compiled languages Much.
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.
Shell Script Examples.
Lab 8 Shell Script Reference:
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
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
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.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
1. 2 What is a Shell Script? A Text File With Instructions Executable  Why shell script? Simply and quickly initiate a complex series of tasks or a.
Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.
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.
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.
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.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Sed. Class Issues vSphere Issues – root only until lab 3.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
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]
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight Labs 7 & 8 or late ones – 8 is extra credit – Due.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Linux Administration Working with the BASH Shell.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
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.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Part 1: Basic Commands/Utilities
Shell Scripting March 1st, 2004 Class Meeting 7.
Agenda Control Flow Statements Purpose test statement
Pepper (Help from Dr. Robert Siegfried)
CHAPTER 8 (skip , ) CHAPTER 10
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Presented by, Mr. Satish Pise
CSC 352– Unix Programming, Fall, 2011
Chapter 5 Bourne Shells Scripts
CSC 352– Unix Programming, Fall 2012
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)

Highlights of Shell Programming Chapter 13 has a lot of information. Perhaps a little too much for the first reading. We will concentrate on the main programming constructs of immediate use. These ideas carry over from programming in a language like C++. The shell makes them handier because it is an interpreter. You can interact with the shell. We will use bash.

The hash-bang line # starts a comment in the shell. #! on an executable text file says that this is an interpreter’s source file, with the path of the interpreter to follow. #!/bin/bash on the first line of a Bash script. Hash-bang works for any interpreted language with # as the comment delimiter – shells, Perl, Python, etc. Use “chmod +x FILENAME” to add execute permission.

Variables in bash (review) VARIABLE=STRING, with no spaces around the =, assigns the STRING into VARIABLE. In order to make it accessible to children processes, you must export the variable from the shell process that defined it. Use unset to remove a variable binding. C library functions getenv & putenv manipulate these. -bash-3.00$ v=1 -bash-3.00$ echo $v 1 -bash-3.00$ bash -c 'echo $v' -bash-3.00$ export v -bash-3.00$ bash -c 'echo $v' 1

Shell read and built-in variables. Use read VARIABLENAME to read the terminal into VARIABLENAME. Use echo STRING or echo –e STRING to echo to stdout; -e interprets escapes such as \n \t. Special variables: $? is exitstatus of exit exitstatus; $0 is argv[0] (program name), $1 is argv[1] (first argument), etc.; shift shifts $2 down to $1, etc.; $# is number of positional arguments; $* or give all command line arguments; set - - STRINGS sets command line arguments to whatever is in STRINGS.

Boolean expressions Any successful (0) exit status is true, non-0 is false. Shell’s test primitive tests Shell variables and files. Exit status is 0 for success. [ expression ] is shorthand for test expression. -bash-3.00$ ls *.out nohup.out -bash-3.00$ test -f nohup.out ; echo $? 0 -bash-3.00$ test -f junk.out ; echo $? 1

Shell test or [ ] expressions Numerical comparisons: -eq, -ne, -gt, -ge, -lt, -le String comparisons: s1 = s2, s1 != s2, -n stg (not null), -z stg(is null), stg (assigned and non-empty), s1 == s2 (ksh and bash only). Use “$v” or “${v}” if v may be empty; otherwise, a binary comparison operator won’t see empty string. File tests: -f, -r, -w, -x, -d, -s (size > 0), -e (exists), -L (symbolic link), f1 –nt f2 (newer than), f1 –ot f2 (older), f1 -ef f2 (linked to) for ksh and bash.

Boolean connectives && is logical AND, || is logical OR Both are short-circuited. && quits on the first failure, || on the first success. -bash-3.00$ ls *.out nohup.out -bash-3.00$ [ -f nohup.out ] && echo hello hello -bash-3.00$ [ -f junk.out ] && echo hello -bash-3.00$ [ -f nohup.out ] || echo hello -bash-3.00$ [ -f junk.out ] || echo hello hello

if control construct selects an execution path Just like you are used to in C++, but Use elif instead of else if, use fi to end an if construct. -bash-3.00$ count=`ls *out |wc -l` -bash-3.00$ echo $count 1 -bash-3.00$ if [ $count -eq 0 ] > then >echo none > elif [ $count -eq 1 ] > then > echo one > else > echo other > fi one

while iterates while a text is true while uses do and done keywords, also break and continue -bash-3.00$ set -- `date` # set command line arguments -bash-3.00$ echo "COUNT $# DATA: COUNT 6 DATA: Sun Mar 27 19:45:36 EDT bash-3.00$ i=1 -bash-3.00$ while [ $# -gt 0 ] > do > echo "var $i: $1" > i=`expr $i + 1` > shift # shift position parameters to the left by one position, discard former $1, decrement $# > done var 1: Sun var 2: Mar var 3: 27 var 4: 19:49:10 var 5: EDT var 6: 2011

for loops over a list of values -bash-3.00$ ls -d s* splitstudents startup -bash-3.00$ for f in `ls -d s*` > do > echo "file: $f" > done file: splitstudents file: startup -bash-3.00$ find ~/unix -name "s*" -print /export/home/faculty/parson/unix/seeargs /export/home/faculty/parson/unix/seeargs/seeargs.c /export/home/faculty/parson/unix/seeargs.zip -bash-3.00$ for path in `find ~/unix -name "s*" -print` > do > echo "file `basename $path` in directory `dirname $path`" > done file seeargs in directory /export/home/faculty/parson/unix file seeargs.c in directory /export/home/faculty/parson/unix/seeargs file seeargs.zip in directory /export/home/faculty/parson/unix

case similar to C++ switch-case construct case expression in pattern1) commands1 ;; pattern2) commands2 ;;... *) defaultcommands ;; esac Patterns can be numeric values, regular expressions.

Here document (<<) Use << to redirect input from the script itself. Make up a delimiter that is not in the data. -bash-3.00$ wc -l << DELIM > this is a > three-line > chuck of text > DELIM 3

Shell functions They take no parameters, work on variables in the shell process. Useful for repeated code. -bash-3.00$ fcounter() { > fcnt=`find. -type f -print |wc -l` > echo "$fcnt regular files in and below `pwd`" > } -bash-3.00$ fcounter 1537 regular files in and below /export/home/faculty/parson/process_incoming

aliases Alias is useful for changing complex command strings into simple command aliases. It is also good for pointing to a specific path. -bash-3.00$ alias alias vi='vim'

eval and exec eval takes a string and evaluates it as a program fragment. -bash-3.00$ echo -e "Enter a command that I will read as data: \c" ; read cmd Enter a command that I will read as data: date -bash-3.00$ eval $cmd Sun Mar 27 20:25:09 EDT 2011 exec replaces the current shell with the exec’d program.