2000 Copyrigths Danielle S. Lahmani UNIX Tools G22.2245-001, Fall 2000 Danielle S. Lahmani Lecture 3.

Slides:



Advertisements
Similar presentations
CHAPTER 2 THE UNIX SHELLS by U ğ ur Halıcı. layers in a unix system 1 Users Standard utility programs (shell, editors, compilers, etc.) Standard utility.
Advertisements

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.
Linux+ Guide to Linux Certification, Second Edition
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
CS 497C – Introduction to UNIX Lecture 36: - Customizing the Environment Chin-Chih Chang
The UNIX Shells 1. What is a Unix shell? 2. A few common shells in the Unix & Linux. A. Bourne shell B. Korn shell C. C shell D. Bash-the default shell.
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.
2000 copyright Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 2.
2000 Copyrigths Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 4.
Shell Script Examples.
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.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Introduction to UNIX / Linux - 11
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Unix Shells Based on Glass & Abels’ Book CS240 Computer Science II.
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.
Shell Features CSCI N321 – System and Network Administration Copyright © 2000, 2005 by Scott Orr and the Trustees of Indiana University.
Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
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.
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
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
Workbook 6 – Part 2 The Bash Shell
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
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.
Univ. of TehranDistributed Operating Systems1 Advanced Operating Systems University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh.
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.
Customizing the Shell Environment. UNIX Shells Two characteristics of shells –Interactive: prompts ($) and waits for your response/requests –Noninteractive:
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Environment After log in into the system, a copy of the shell is given to the user Shell maintains an environment which is distinct from one user to another.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
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.
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
Linux Administration Working with the BASH Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
CIRC Winter Boot Camp 2017 Baowei Liu
Shell Features CSCI N321 – System and Network Administration
Shell Programming.
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Chapter 5 Bourne Shells Scripts
Chapter 3 The UNIX Shells
Presentation transcript:

2000 Copyrigths Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 3

2000 Copyrigths Danielle S. Lahmani Overview Review of process creation shell core functionality /bin/sh /bin/ksh /bin/csh project discussion

2000 Copyrigths Danielle S. Lahmani Example : Program that creates a new process to copy files Reference: M.Bach, "The Unix Operating system", p 11. main(argc,argv) int(argcl char *argv[]; {/* assumes 2 args, source and target files */ if ( fork() == 0) { /* child process */ execl("cp"."cp",argv[1],argv[2],0); } /* parent process */ wait(int *) 0); printf("copy done\n"); }

2000 Copyrigths Danielle S. Lahmani Fork operation reference Unix Network programming - W. R. Stevens

2000 Copyrigths Danielle S. Lahmani After exec of prog2 in child (prog2 is cp in example)

2000 Copyrigths Danielle S. Lahmani Shell Core Features Simple and complex commands redirection of input/output pipes wildcards command substitution background processes shell variables here documents built-in cmds programming constructs

2000 Copyrigths Danielle S. Lahmani Complex commands Multiple commands –example: $ls ; pwd background processes –example: sleep 40& Command groupings –(cmd1; cmd2; cmd3) Conditional command execution

2000 Copyrigths Danielle S. Lahmani File name expansion Wildcards * matches any string of characters ?matches any single character [list] matches any character in list [lower-upper] matches any character in range lower-upper inclusive

2000 Copyrigths Danielle S. Lahmani Command substitution A command can be placed with grave accents ` ` to capture the output of command often used with shell variables

2000 Copyrigths Danielle S. Lahmani Shell Scripts A shell script is a regular text file that contains shell or UNIX commands Before running it, it must have execute permissions ( see chmod +x filename) Very useful for automating repetitive task and administrative tools and for storing commands for later execution

2000 Copyrigths Danielle S. Lahmani Shell Scripts (continued) When a script is run, kernel determines which shell it is written for by examining the first line of the script –If 1 st line is just #, then it is interpreted by a C shell –If 1 st line is of the form #!pathname, then the executable –Pathname is used to interpret the script –If neither rule 1 nor rule 2 applies, the script is interpreted by a Bourne shell.

2000 Copyrigths Danielle S. Lahmani Here Documents Shell provides alternative ways of supplying standard input to commands Shell allows in-line input redirection using << called here documents format command [arg(s)] << arbitrary-delimiter command input : arbitrary-delimiter arbitrary-delimiter should be a string that does not appear in text

2000 Copyrigths Danielle S. Lahmani Shell Variables Shell has several mechanisms for creating variables. A variable is a name Representing a string value –Shell variables can save time and reduce typing errors, variables Allow you to store and manipulate information two types : local and environmental –local are set by the user of by the shell itself –Positional parameters variables are normally set only on a command line

2000 Copyrigths Danielle S. Lahmani Environmental Variables NAMEMEANING $HOMEabsolute pathname of your home directory $PATHa list of directories to search for $MAILabsolute pathname to mailbox $USERyour user id $SHELLabsolute pathname of login shell $TERMtype of your terminal

2000 Copyrigths Danielle S. Lahmani Positional parameters when a shell procedure is invoked, the shell implicitly creates positional parameters. The name for a positional parameter is a number. Positional parameters are used mainly in scripts. –$0 is the argument in position zero on the command line –$1 is the first argument –$1.. $9 $n refers to the nth argument on the command line if applicable –$# the number of positional parameters, not counting 0 –$* the list of all arguments

2000 Copyrigths Danielle S. Lahmani QUOTING Quoting restores the literal meaning to characters that are processed specially by the shell. The literal quotes are not passed on to the command Single quotes ( ' ) inhibit wildcard replacement, variable substitution, and command substitution Double quotes ( " ) inhibit wildcard replacement only When quotes are nested, only the outer quotes have any effect

2000 Copyrigths Danielle S. Lahmani BUILT-IN commands commands that are internal to the shell Faster to execute and more efficient than other commands –Shell does not have to fork to execute the command –Trade-off: redirection of input/output not allowed for most of these

2000 Copyrigths Danielle S. Lahmani Built-in commands (continued) built-in commands common to the 3 shells: echoexec cdshift waitumask exiteval

2000 Copyrigths Danielle S. Lahmani Subshells When a parent shell forks a child to execute a command, the new child shell is sometimes called a subshell. This happens when: –a group command is executed ( $(cmd1; cmd2; cmd3) ) –a shell script is executed( $myscript ) –a background job is executed ( cmd1&) A shell inherits the parent's environment but not the parent's local variables.

2000 Copyrigths Danielle S. Lahmani The Bourne Shell: /bin/sh Startup file:.profile Variables: –Assignment: var = value; –Access: $var –Exporting variable: $export variable

2000 Copyrigths Danielle S. Lahmani /bin/sh:BUILT-IN VARIABLES $#number of cmd lines args $- options currently in effect $? exit value of last executed cmd $$process num of current process $! Proc num of last background proc $*all arguments on command line arguments on command line individually quoted "$1" "$2" ….

2000 Copyrigths Danielle S. Lahmani /bin/sh: Arithmetic No arithmetic support in /bin/sh expr expression –Evaluates expression and sends the result to standard output –yield a numeric or string result test expression for conditional expression

2000 Copyrigths Danielle S. Lahmani /bin/sh: CONTROL STRUCTURES Case... in... esac For … do … done If … then … fi Until … do … done While … done

2000 Copyrigths Danielle S. Lahmani /bin/sh: trap command trap specifies command that should be executed when the shell receives a signal of a particular value. Trap [ [command] {signal}+] –If command is omitted, signals are ignored

2000 Copyrigths Danielle S. Lahmani /bin/sh: Other commands Debbuging options for scripts: –set -vx –-v : echo shell commands as they are read –-x : echo shell commands as they are executed sequenced commands –{ cmd1; cmd2; cmd3 …cmdn} executed by the parent, can redirect output, etc…

2000 Copyrigths Danielle S. Lahmani /bin/sh:Redirection using file Descriptors cmd >&n send cmd output to fd n cmd <&n take input for cmd from fd n cmd >&- close standard output cmd <&- close standard input

2000 Copyrigths Danielle S. Lahmani /bin/sh:multiple redirection cmd 2>filesend standard error to file standard output remains the same cmd > file 2>&1 send both standard error and standard output to file (cmd > file1) 2>file2send standard output to file1, send standard error to file2

2000 Copyrigths Danielle S. Lahmani The Korn Shell: /bin/ksh Supports all features described in the Bourne shell (/bin/sh) Alias mechanism History mechanism for access of previous commands Functions Enhanced job control Arithmetic Tilde substitution

2000 Copyrigths Danielle S. Lahmani The Korn shell: /bin/ksh STARTUP FILES: –/etc/profile –$HOME/.profile ALIAS: –alias [-tx] [word[=string]] –alias -x : to export alias to child shell –unalias aliasname: to remove an alias

2000 Copyrigths Danielle S. Lahmani /bin/ksh: History Mechanism Numbered commands $ PS1='! $’ /* set prompt to contains a ! */ $HISTSIZE default is 128 using the built-in vi editor –declare VISUAL=vi or EDITOR=vi –to edit current line, press ESC key to enter the editor –vi cmds to edit line, when done, press ESC key again, –additional movement: cursor up (k or - ) cursor down (j or +) –additional searching /string or ?string : searches backward and forward through history, respectively.

2000 Copyrigths Danielle S. Lahmani /bin/ksh (continued) ARITHMETIC: Using let expression TILDE SUBSTITUTION –~$HOME –~userhome directory of user –~/pathname$HOME/pathname –~+$PWD –~-$OLDPWD

2000 Copyrigths Danielle S. Lahmani /bin/ksh: FUNCTIONS Allows functions that may be invoked as shell commands function name { list of commands } or name() { list of commands }

2000 Copyrigths Danielle S. Lahmani /bin/ksh: Functions (continued) can use parameters returning from a function local variable using typeset functions can be recursive

2000 Copyrigths Danielle S. Lahmani /bin/ksh: ENHANCED JOB CONTROL jobslist your jobs bgplaces a specified job in the background fgplaces a specified job in the foreground killsends an arbitrary signal to a process or job ^zto stop a job

2000 Copyrigths Danielle S. Lahmani /bin/ksh: coprocess PIPES |& operator supports a simple form of concurrent processing cmd |& cmd run as a background process whose standard input and output channels are connected to the original parent shell via a two way pipe.