An Introduction to Unix Shell Scripting

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
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 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.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
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.
CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science
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.
QUOTATION This chapter teaches you about a unique feature of the shell programming language: the way it interprets quote characters. Basically, the shell.
Shell Script Examples.
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.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
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.
Writing Shell Scripts ─ part 1 CSE 2031 Fall September 2015.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Chapter Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Shell The Shell The agency that.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Chapter 6: Shell Programming
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.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
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 Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux+ Guide to Linux Certification, Third Edition
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.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Shell Advanced Features. Module 8 Shell Advanced Features ♦ Introduction In Linux systems, the shells are often referred to as command line interfaces.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
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.
Using UNIX Shell Scripts Michael Griffiths Corporate Information and Computing Services The University of Sheffield
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©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.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
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.
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER FOUR – QUOTING IN DETAIL.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Linux Administration Working with the BASH Shell.
Shell scripts – part 1 Cs 302. Shell scripts  What is Shell Script? “Shell Script is series of command written in plain text file. “  Why to Write Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
CS 350 Lecture 3 UNIX/Linux Shells by İlker Korkmaz and Kaya Oğuz.
Shell Features CSCI N321 – System and Network Administration
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
CAP652 Lecture - Shell Programming
Shell Scripting March 1st, 2004 Class Meeting 7.
CSE 303 Concepts and Tools for Software Development
John Carelli, Instructor Kutztown University
Linux Shell Script Programming
Chapter 3 The UNIX Shells
Bash Scripting CS 580U - Fall 2018.
Presentation transcript:

An Introduction to Unix Shell Scripting

UNIX Shells /bin/sh –Borne Shell /bin/csh – C Shell /bin/ksh – Korn Shell /bin/bash Borne Again Shell

Unix Shell Scripting The Bourne shell is what we will use for scripting It's portable Virtually every Unix system has sh installed It has a rich set of programming constructs It is arguable that it is the most popular UNIX shell.

BASH Comments # is the comment symbol in the shell # can occur anywhere on a line The shell ignores anything following a # until the end-of-line One special exception #! on the first line is used to tell the shell what program to use to interpret the script file Examples: #!/bin/sh -tells shell to use the Bourne shell to execute the script #!/usr/bin/perl -tells shell to use Perl to execute the script

Shell Output echo is the primary way to perform output from the shell Syntax: echo [-n] arguments -n - do not append a NEWLINE (suppress the enter) arguments - may be variables or explicit strings Examples echo "Hi there, I'm a Unix whiz kid!" echo "My home directory is $HOME" echo $PATH echo To suppress output from shell commands in a script, redirect the output to /dev/null

Assigning Shell Variables To store values in a shell variable, write the name of the variable followed by an = followed by the value count=1 my_dir=/home/bobo Note that spaces are NOT allowed on either side of the = Also, the shell has no concept of data types No matter what assignment you make, the shell considers the value as a string of characters Variables don't need to be declared, they're simply assigned values when you want to use then

Referring to Variables In order to refer to the value of a variable, preface the variable name with a $ echo $count ¥ displays 1 echo count ¥ displays count echo $my_dir ¥ displays /home/bobo echo my_dir ¥ displays my_dir If you want to ensure a variable is not accidentally modified, specify it as readonly. Further attempts to modify it will result in an error from the shell. my_dir=/home/bobo readonly my_dir

File Name Substitution & Variables If you define a variable as x=* ls $x will produce a directory list of all files Did the shell store the * in x or the list of files in your current directory?

File Name Substitution & Variables The shell stored the * in x, the shell does not perform filename substitution when assigning values to variables What actually happens is: The shell scans ls $x, substituting * for $x It then rescans the line and substitutes all the files in the current directory for the *

Environment Create a file called firstscript that contains: #!/bin/bash x=123 echo the variable x equals $x Save it and make it executable (chmod 744 firstscript) Then execute firstscript - what happens? Make sure not to name your scripts the same as unix commands this may cause yor scripts not to function correctly. The most common mistake is to name your script “test”

Variables in the Environment When variables are assigned, they are local to the current shell Since scripts are executed in a sub-shell, these local variables aren't visible to the script To make them visible (inherited by) subsequent sub-shells, they must be exported export my_dir The env command lists all currently defined exported variables

Advanced echo The echo command allows for special support of control characters. These special control features include such functions as new line, tab and bell To Enable the interpretation of special control(escape) characters. One must use the –e switch echo –e “This will \n be on two lines”

echo escaped characters \a alert (bell) \b backspace \c suppress trailing newline \f form feed \n new line \r carriage return \t horizontal tab \v vertical tab \\ backslash \nnn The character whose ASCII code is nnn(octal)

Debugging Scripts with -x When developing scripts, it is often difficult to debug them In order to get a trace of what is happening, you can invoke your script by using the shell's -x option sh -x case.scr This will trace(show the commands) the statements in the script as they are being executed (similar to leaving echo on in batch files)

Command Line Arguments Supposing you ran the scripted called big, using the following command. big red sea You would be providing your program with two variables. (red and sea) The program can use these variables.

Command Line Arguments When the shell invokes your script, it assigns the command line arguments to a set of variables $0, $1, $2,…$9 $0 is the script name $1 is the first argument, $2 the second, up through $9 You can then refer to these variables in your script.

What if you have more then 9 command line arguments? If more than 9 arguments are used, you must access them using the shift command shift simply does a left shift on all the arguments, discarding $1 and making $1 = $2, $2 = $3, etc Note, this means that if you still need $1 you must save it in another variable BEFORE performing the shift

Other Pre-defined Variables $# - number of arguments on command line $* - collectively references all of the positional parameters ($1, $2, $3) $0 - name of program being executed $$ - PID of current process $? - exit status of last command not executed in background

Interactive Input Aside from command line arguments input can also be provided by the user as the program in running. to accomplish this simply use the read command

The read Command Syntax: read var1 var2 var3 … Example: read text This would wait for the user to input a string then the variable text would be set to it. read gets input from STDIN so it can be redirected from a file read text < data

Quoting Quotes in shell scripting have special meaning. There are four types of quoting; Single Quote ‘ (next to the enter key) removes the special meaning of all enclosed characters Double Quote " removes the special meaning of all enclosed characters EXCEPT $, `, and \

Quoting \<character> removes the special meaning of the character that follows the \; inside double quotes it removes the special meaning of $ ` " NEWLINE and \ but is otherwise not interpreted Back Quote ` (matilda on the ~ key) executes command inside the quotes and inserts standard output at that point.

Examples of Back Quoting echo Your current directory is `pwd` Outputs "Your current directory is /home/bobo" echo There are `who | wc -l` users logged on Outputs "There are 13 users logged on"

Single Quotes Since single quotes protect everything, the following output should make sense: echo ‘`who | wc –l` tells how many users are logged on’ Outputs  `who -| wc –l` tells how many users are logged on But back quotes are interpreted inside “ echo "You have `ls | wc -l` files in your directory" Outputs  You have 24 files in your directory

Shell Arithmetic The Bourne shell has no idea how to do arithmetic For example number=2 number=$number + 4 echo $number 2 + 4 That makes shell scripting pretty useless as a programming language doesn't it?

expr Fortunately, there is a Unix command that will allow us to perform arithmetic within a script The expr command "evaluates" it's arguments and writes it's output on STDOUT Example: expr 1 + 2 3 expr 6 / 2 + 5 8

expr Note, since it is evaluating arguments, they must be separated by spaces Also, expr only works with integer arithmetic expressions a=1, b=2, c=3 expr $a / $b + $c = 3