CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang

Slides:



Advertisements
Similar presentations
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Advertisements

CS 497C – Introduction to UNIX Lecture 26: - The Process Chin-Chih Chang
CS 497C – Introduction to UNIX Lecture 32: - Shell Programming Chin-Chih Chang
CS 497C – Introduction to UNIX Lecture 22: - The Shell 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
CS 497C – Introduction to UNIX Lecture 36: - Customizing the Environment Chin-Chih Chang
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
Introduction to Unix – CS 21 Lecture 13. Lecture Overview Finding files and programs which whereis find xargs Putting it all together for some complex.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
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.
2 $ command Command Line Options ls –a –l hello hi Command Arguments.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
Introduction to Shell Script Programming
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Introduction to UNIX / Linux - 11
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
An Introduction to Unix Shell Scripting
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
CS465 - UNIX The Bourne Shell.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
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)
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.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Lecture 24CS311 – Operating Systems 1 1 CS311 – Lecture 24 Outline Final Exam Study Guide Note: These lecture notes are not intended replace your notes.
©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.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
UNIX Command RTFM: trap(1p) Gilbert Detillieux October 8, 2013 MUUG Meeting.
Chapter Six Introduction to Shell Script Programming.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Linux+ Guide to Linux Certification, Second Edition
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.
CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.
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]
Agenda The Bourne Shell – Part I Redirection ( >, >>,
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
Linux Administration Working with the BASH Shell.
Bash Shell Scripting 10 Second Guide.
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
CSC 352– Unix Programming, Fall 2012
Shell Scripting March 1st, 2004 Class Meeting 7.
Lecture 9 Shell Programming – Command substitution
Shell Programming (ch 10)
Agenda Control Flow Statements Purpose test statement
What is Bash Shell Scripting?
LING 408/508: Computational Techniques for Linguists
Linux Shell Script Programming
Essential Shell Programming
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Essential Shell Programming
Chapter 5 Bourne Shells Scripts
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Presentation transcript:

CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang

Sample Scripts The script cpback.sh shown in Fig protects your files from accidental overwritting by the cp command. The script dentry1.sh shown in Fig accepts a code and its corresponding description from the terminal, performs some rudimentary validation checks, and then adds an entry to a file desig.lst. The continue statements let you reenter the data or start a fresh cycle.

Sample Scripts The break statement in the inner loop quits the loop after adding the line. You should use “ ” with a for loop when using multiword arguments. for file in basename is a command to strip directory and suffix from filenames $ basename hello.java.java hello

Sample Scripts You can use basename inside a for loop to change the extensions of files. for file in *.txt ; do leftname=`basename $file.txt` mv $file ${leftname}.doc done The cpback2.sh script accepts multiple filenames and backup them without overwritting.

The Here Document (<<) The here document (<<) provides input to a script from the script itself. It can be used with both command substitution and variables. It is often used with commands that don’t use a filename as argument or for running interactive programs noninteractively. mail cs497c << EOF Test Date is `date`. EOF

let: Computation – A Second Look (ksh and bash) Some advanced features of shell programming only happen in Korn and bash shell. Korn shell is located in /usr/bin/ksh. Bash shell is located in /bin/bash. You can compute with let statement: $ let sum= ; echo $sum There is no space after variable. If you need space, just quote the expression:

let: Computation $ let sum=“3 * / 2”; echo $sum $ x=22 y=28 z=5; z=$((x+y+z)); echo $z When a process is created by the shell, it makes certain features of its own environment to the child. The created process make use of the inherited parameters including: –The PID of the parent process. –The UID (owner) and GUID (group owner) of the process.

Sub-Shells –The current working directory. –The three standard files. –Other open files used by the parent process. –Some environment variables available in the parent process. A variable defined in the parent is visible in the child only when it is exported (export). However, when the child alters the value of the variable, the changed value is not seen by the parent.

Sub-Shells & Arrays The matching operators () run a group of commands in a sub-shell, but the {} don’t spawn one. Korn and bash support one-dimensional arrays where the first element has the index 0. $ prompt[2]=“Enter your name” $ echo ${prompt[2]}

Arrays You can use a space-delimited list enclosed with parentheses: month_arr=( ) $ echo ${month_arr[6]} In the older version of Korn, you can use the set –A statement: set –A mon_arr Using or * as subscript, you an display all the elements of the array as well as the number of elements.

Arrays & String Handling $ echo $ echo The dateval.sh script in Fig use arrays to validate an entered date. The length of string can be found by preceding the variable name with a #. $ name=“Jay Leno”; echo ${#name} You can extract a substring: $ echo ${name:4:4}

Conditional Parameter Substitution Shell variables can be evaluated in a conditional manner depending on whether they are assigned a non-empty value. This is called parameter substitution with the following format: ${variable:symbol string} where symbol can be +, -, = or ?. When the + option is used, variable is evaluated to string if it has a non-null value.

Conditional Parameter Substitution found=`ls`; echo ${found:+“Files found."} When - option is used, variable is evaluated to string if it has a null value. The = operator additionally assigns a value to the variable. while [${x:=1} -le 10] The ? option prints an error message and exits the shell. grep $pattern ${flname:?”No file.. quitting”}

Shell Functions A shell function consists of a group of statements that are executed together as a bunch. Optionally, it also returns a value: function_name() { statements return value } mainfunc.sh displays some functions.

Shell Functions anymore() { echo $option "\n$1 ?(y/n) : \c" 1>&2 read response case "$response" in y|Y) echo 1>&2 ; return 0 ;; *) return 1 ;; esac } $ anymore “Wish to continue”

eval & exec eval processes a command line twice and is used to simulate arrays and execute. With eval, you can create generalized numbered prompts and variables that significantly compact code. $ prompt1=“User Name:” $ x=1; eval echo \$prompt$x exec overlays the current shell when prefixed to a command.

exec & trap $ exec date To debug shell scripts, use set –x at the beginning of the script so that every command line is echoed to the screen. Use trap if you want your script to respond to an interrupt in a specific way. trap ‘command_list’ signal_list It is useful in removing temporary files when a script receives a signal.