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.

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.
Introduction to Unix Shell & Scripting with csh/tcsh  Brief Unix History  Unix Shell & Flavor  CSH/TCSH Scripts.
CS 497C – Introduction to UNIX Lecture 32: - Shell Programming 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.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
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.
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.
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.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
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.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
1 Overview The shell is one of the main interfaces to UNIX that a Systems Administrator uses. Interpreter  Process the commands you enter Programming.
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 Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux+ Guide to Linux Certification, Third Edition
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
Linux Operations and Administration
Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where.
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)
Unix/Linux cs3353. The Shell The shell is a program that acts as the interface between the user and the kernel. –The shell is fully programmable and will.
BY A Mikati & M Shaito Awk Utility n Introduction n Some basics n Some samples n Patterns & Actions Regular Expressions n Boolean n start /end n.
Writing Scripts Hadi Otrok COEN 346.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Chapter 8: The Bourne Again Shell It’s a command interpreter, it’s a programming language, and it makes a mean martini.
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.
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.
Agenda Customizing a Unix/Linux account Environment Introduction to Start-up Files (.bash_profile,.bashrc,.profile,.kshrc) Safe Methods for Changing Start-up.
Implementation of a simple shell, xssh
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Writing Shell Scripts ─ part 1
Implementation of a simple shell, xssh (Section 1 version)
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Implementation of a simple shell, xssh
CSC 352– Unix Programming, Fall 2012
CAP652 Lecture - Shell Programming
Writing Shell Scripts ─ part 3
Writing Shell Scripts ─ part 3
What is Bash Shell Scripting?
CHAPTER 8 (skip , ) CHAPTER 10
Writing Shell Scripts ─ part 1
Linux Shell Script Programming
Writing Shell Scripts ─ part 1
CSC 352– Unix Programming, Fall, 2011
Introduction to Unix Shell & Scripting with csh/tcsh
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Presentation transcript:

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 Process groups of commands stored in a file called shell scripts. Like other languages, shells have –Variables –Control flow commands

More about Shells1-2 Create a shell script  Creating a simple shell script m A shell script is a file that contains commands that the shell can execute. Any commands you enter in response to a shell prompt. –A utility –A compiled program –Another shell script Control flow commands  Run a shell script m Enter the script filename on the command line m The shell interprets and execute the commands one after another  Why shell script? m Simply and quickly initiate a complex series of tasks or a repetitive procedure.

More about Shells1-3 Shell programming  Make the file executable m When you create a shell script using a editor, does it have execute permission typically? Example ~/cs3451]$./test./test: Permission denied. ~/cs3451]$ ls -l test -rw ruihong csdept 22 Jan 28 09:33 test ~/cs3451]$ chmod +x test ~/cs3451]$./test this is a test

More about Shells1-4 Invoking a Shell script  Give the shell a command on the command line m The shell forks a process Which creates a duplicate of the shell process ( subshell) m The new process attempt to exec the command If the command is a executable program –Exec succeeds –System overlays the newly created subshell with the executables programs The the command is a shell script –Exec failed –The command is assumed to be a shell script –The subshell runs the commands in the shell.

More about Shells1-5 Invoking a Shell script  Shell itself is program, m It can be run as a command in a shell m It accepts arguments  To run a shell script m Which does not have executable permission Ex: $sh filename m Run the script with different shell other than your interactive shell Ex: $ksh filename

More about Shells1-6 Invoking a Shell script  Put special characters on the first line of a shell script m To tell OS checks what kind of file it is before attempting to exec it m To tell which utility to use (sh, csh, tcsh, …)  Special sequence m The firsts two character of a script are #! m Then followed by the absolute pathname of the program that should execute the script Ex: sh-2.05b$ more /etc/init.d/sshd #!/bin/bash # # Init file for OpenSSH server daemon #

More about Shells1-7 Make a comment #  Comments make shell scripts easier to read and maintain  Pound sign (#) start a comment line until the end of that line, except m #! In the first line. m Or inside quotes

More about Shells1-8 Startup files  Bourne shell m System wide /etc/profile m.profile in your home dir m Example: /etc/profile in undergrad lab.  Make the change take effect m Log out and log back in m Running.profile with. (DOT) built-in Ex: $..profile. Command runs the script as the part of the current process Changes will affect the login shell. If without the first., the new variable would be in effect only in the subshell running the script.

More about Shells1-9 Parameters and Variables  A shell parameter is associated with a value that is accessible to the user. m Shell variables Names consist of letters, digits and underscore –By convention, environment variables uses uppercase User created variables ( create and assign value) Keyword shell variables –Has special meaning to the shell –Being created and initialized by the startup file m Positional parameters Allow you to access command line arguments m Special parameters Such as –The name of last command –The status of most recently executed command –The number of command-line arguments

More about Shells1-10 Positional Parameters  The command name and arguments are the positional parameters. m Because you can reference them by their position on the command line m $0 : Name of the calling program m $1 - $9 : Command-line Arguments The first argument is represented by $1 The second argument is represented by $2 And so on up to $9 The rest of arguments has to be shifted to be able to use $1- $9 parameters.

More about Shells1-11 Positional Parameters  Example: ~/cs3451]$ more display_5args echo you are running script $0 with parameter $1 $2 $3 $4 $5 ~/cs3451]$./display_5args you are running script./display_5args with parameter

More about Shells1-12 Positional Parameters  $1-$9 allows you to access 10 arguments m How to access others?  Promote command-line arguments: shift m Built-in command shift promotes each of the command-line arguments. The first argument ( which was $1) is discarded The second argument ( which was $2) becomes $1 The third becomes the second And so on m Makes additional arguments available m Repeatedly using shift is a convenient way to loop over all the command-line arguments

More about Shells1-13 Positional Parameters  Example: ~/cs3451]$ more./demo_shift echo $1 $2 $3 shift echo $1 $2 shift echo $1 ~/cs3451]$./demo_shift

More about Shells1-14 ~/cs3451]$ more demo_shift echo $1 $2 $3 shift echo $1 $2 shift echo $1 shift echo $? shift echo $? shift echo $? ~/cs3451]$./demo_shift

More about Shells1-15 Positional Parameters  Use quote for variable reference m Example: what’s the difference if $1 is null $ display_4args $1 a b c d $ display_4args “$1” a b c d What will happen if a is null if [ $a = 3 ]; then echo a is 3 fi  Initialize command line arguments : set (sh/ksh only) m Set the positional parameters starting from $1, …

More about Shells1-16 Special Parameters  Useful values m Command-line arguments m Execution of shell commands m Can not change the value directly, like positional parameters  Value of Command-line arguments: $* and m $* and represent all the command_line arguments ( not just the first nine) m “$*” : treat the entire list of arguments as a single argument m : produce a list of separate arguments.

More about Shells1-17 sh-2.05b$ more for_test echo "using " for arg in do echo "$arg" done echo "using \$* " for arg in "$*" do echo "$arg" Done sh-2.05b$./for_test using using $* 1 2 3

More about Shells1-18 Special Parameters  The number of arguments: $# m Return a decimal number m Use the test to perform logical test on this number sh-2.05b$./num_args this script is called with 0 arguments. sh-2.05b$./num_args 1 this script is called with 1 arguments. sh-2.05b$./num_args alice in wonder land this script is called with 4 arguments. sh-2.05b$ more num_args echo this script is called with $# arguments.

More about Shells1-19 Special Parameters  PID number: $$ m Ex: sh-2.05b$ echo $$ sh-2.05b$ echo "today is `date`" >> $$.memo sh-2.05b$ more $$.memo today is Sun Jan 30 00:18:09 EST 2005  The PID number of last process that you ran in the background: $! m Ex: sh-2.05b$ sleep 1000 & [1] sh-2.05b$ echo $! 11962

More about Shells1-20 Special Parameters  Exit status: $? m When a process stops executing for any reason, it returns an exit status to its parent process. m By convention, Nonzero represents a false value that the command failed. A zero value is true and means that the command was successful m You can specify the exit status that a shell script returns by using the exit built-in followed by a number Otherwise, the exit status of the script is the exit status of the last command the script ran.

More about Shells1-21 sh-2.05b$ ls a ls: a: No such file or directory sh-2.05b$ echo $? 1 sh-2.05b$ echo tttt tttt sh-2.05b$ echo $? 0 sh-2.05b$ more exit_status echo this program will have the exit code of 8. exit 8 sh-2.05b$./exit_status this program will have the exit code of 8. sh-2.05b$ echo $? 8 sh-2.05b$ echo $? 0

More about Shells1-22 Summary  A shell is both a command interpreter and a programming language  Job control m Control-z/fg/bg/&  Variables m Local and environment variables m Declare and initialize a variable ( no type) m Export unset  Command line expansion m Parameter expansion/variable expansion/command/substitution/pathname expansion m Quote ( ‘ ‘ “ “ \ ) “ “ all but parameter, variable expansion and \ ‘ ‘ suppress all types of expansion \ escaping the following special character

More about Shells1-23 Summary  Shell parameters m HOME m PATH m PS1 m SHELL m $0 m $n m $* m m $# m $$ m $! m $?

More about Shells1-24 Summary  Special Characters m NEWLINE m ; m () m & m | m > m >> m < m <<

More about Shells1-25 Summary m * m ? m \ m ‘ m “ m ` ` m [] m $ m. m # m && m || m !