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.

Slides:



Advertisements
Similar presentations
UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
Advertisements

CIS 118 – Intro to UNIX Shells 1. 2 What is a shell? Bourne shell – Developed by Steve Bourne at AT&T Korn shell – Developed by David Korn at AT&T C-shell.
1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview.
1 The Shell and some useful administrative Unix Commands How Unix works along with some additional, useful administrative Unix commands you might need.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
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.
Introducing the Command Line CMSC 121 Introduction to UNIX Much of the material in these slides was taken from Dan Hood’s CMSC 121 Lecture Notes.
Linux+ Guide to Linux Certification, Second Edition
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
2000 Copyrigths Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 3.
CS 497C – Introduction to UNIX Lecture 20: - The Shell 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.
Chapter 4 The UNIX Shells (Bourne shell, Korn shell, C shell)‏ Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice.
Xuan Guo Chapter 4 The UNIX Shells Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Original Notes.
Shell Script Examples.
7/17/2009 rwjBROOKDALE COMMUNITY COLLEGE1 Unix Comp-145 C HAPTER 2.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”
CHAPTER 1 UNIX FOR NONPROGRAMMERS By U ğ ur Halıcı.
Linux in More Detail Shirley Moore CPS5401 August 29,
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.
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.
Shrinivas R. Mangalwede, GIT, Belgaum UNIX and Shell Programming (06CS36) Unit 3 Shrinivas R. Mangalwede Department of Computer Science and Engineering.
Unix Shells Based on Glass & Abels’ Book CS240 Computer Science II.
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 8 Shell.
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.
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.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
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.
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.
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.
LINUX programming 1. INDEX UNIT-III PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Problem solving approaches in Unix,Using.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Agenda Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Review next lab assignments Break Out Problems.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Lesson 2 1.Commands 2.Filename Substitution 3.I/O Redirection 4.Command Grouping 5.Shell Responisibilites Review of the Basics.
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.
Lesson 2-Touring Essential Programs. Overview Development of UNIX and Linux. Commands to execute utilities. Communicating instructions to the shell. Navigating.
Introduction to Programming Using C An Introduction to Operating Systems.
Week 9 - Nov 7, Week 9 Agenda I/O redirection I/O redirection pipe pipe tee tee.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
THE BOURNE SHELL. Shell : the agency that sits between the user and the UNIX system. The BOURNE SHELL named after its founder Steve Bourne, is one of.
Linux+ Guide to Linux Certification, Second Edition
Chapter 4 The UNIX Shells (Bourne shell, Korn shell, C shell)‏ Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice.
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.
Linux Administration Working with the BASH Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
CS 350 Lecture 3 UNIX/Linux Shells by İlker Korkmaz and Kaya Oğuz.
Introduction to Shells
System Programming and administration CS 308
UNIX System Overview.
INTRODUCTION TO UNIX: The Shell Command Interface
Basic UNIX OLC Training.
John Carelli, Instructor Kutztown University
Introduction Paul Flynn
Linux Shell Script Programming
Chapter 4 The UNIX Shells (Bourne shell, Korn shell, C shell)‏
Chapter 3 The UNIX Shells
Presentation transcript:

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 programs (shell, editors, compilers, etc.) Standard library (open, close, read, write, fork etc.) Standard library (open, close, read, write, fork etc.) UNIX operating system (process scheduling, memory management, file system, I/O etc.) UNIX operating system (process scheduling, memory management, file system, I/O etc.) Hardware (CPU, memory, disks, terminals etc.) Hardware (CPU, memory, disks, terminals etc.) User mode kernel mode User interface library interface System call interface

shell 2  A shell is a program that is an interface between a user and the raw operating system (directly or through standard library).  It makes basic facilities such as multitasking and piping easy to use, as well as adding useful file specific features such as wildcards and I/O redirection.  There are three common shells in use. The Bourne shell The Korn hell The C shell

relation among shells 3 Common core Bourne Shell Korn Shell C Shell

basic shell functions 4 built in commands scripts variables local environment redirection wildcards pipes conditional sequences unconditional subshell Background processing command substitutions shell functions

SELECTING A SHELL: chsh 5  Even though it depends on the system, usually the pathnames for shells are as follows: Bourne shell: /bin/sh Korn shell: /bin/ksh C shell: /bin/csh

SELECTING A SHELL: chsh 6  The command chsh is used to change the current shell $ chsh changing login shell for halici Old shell: bin/sh... displayed New shell: bin/csh... you enter the pathname for the new shell $ ^D... terminate login shell login: halici password: ********* %... the prompt of the C shell $ chsh changing login shell for halici Old shell: bin/sh... displayed New shell: bin/csh... you enter the pathname for the new shell $ ^D... terminate login shell login: halici password: ********* %... the prompt of the C shell

SHELL variable 7  SHELL is a system variable whose value is the pathname of the active shell.  To access the value of SHELL, precede it by $ symbol as $SHELL.  To see the value of a variable the echo command can be used. $ echo these are arguments these are arguments $ echo SHELL SHELL $ echo these are arguments these are arguments $ echo SHELL SHELL echo is a built in shell command that displays all its arguments to the standard output device.

SHELL variable 8  SHELL is a system variable whose value is the pathname of the active shell.  To access the value of SHELL, precede it by $ symbol as $SHELL.  To see the value of a variable the echo command can be used. $ echo these are arguments these are arguments $ echo SHELL SHELL $ echo $SHELL /bin/sh $ echo these are arguments these are arguments $ echo SHELL SHELL $ echo $SHELL /bin/sh echo is a built in shell command that displays all its arguments to the standard output device. the value of the SHELL variable is displayed

Shell operation 9  When a shell is invoked 1. It reads a special start-up file, typically located user’s home directory, that contains some initialisation information 2. It displays a prompt and it waits for a user command 3. If the user enters a control-D character on a line of its own, this is interpreted by the shell as meaning “end of input” and causes the shell to terminate; otherwise the shell executes the user command and returns to step 2.

shell command 10  A shell command may be simple as $ ls  or complex-looking pipline sequences as $ ps | sort | lpr

metacharacters 11  Some charcters are processed specially by shell and are known as metacharacters.  Redirection > : Output redirection; writes standard output to a file >>: Output redirection; appends standard output to a file < : Input redirection; reads standard input from a file << lbl : Input redirection; reads standard input from script up to label “ lbl ”

metacharacters 12  File substitution * : File substitution wildcard; matches zero or more characters ? : File substitution wildcard; matches any single characters [...]: File substitution wildcard; matches any single character between brackets  Command substitution `cmd`: Command substitution; replaced by the output from command  pipe | : Pipe symbol, sends the output of one process to the input of another

metacharacters 13  Sequencing ; : Used to sequence commands || : Conditional execution; executes a command if the previous one fails && : Conditional execution; executes a command if the previous one succeeds  Other (...): Group of commands & : Runs a command in the background # : All characters that follow up to a new line are comment $ : Access a variable

metacharacters 14  When you enter a command, the shell scans it for metacharacters and processes them specially.  When all metacharacters have been processed, the command is finally executed.

metacharacters 15  To turn off the special meaning of a metacharater, precede it by a “\” character. $ echo hi >file $ cat file hi $ echo hi \>file hi >file $ $ echo hi >file $ cat file hi $ echo hi \>file hi >file $ output redirection output redirection is inhibited... So the output is not stored to file but displayed at screen output redirection is inhibited... So the output is not stored to file but displayed at screen

OUTPUT REDIRECTION: >, >> 16 $ cat >myfile Ali Ahmet Can ^D $ cat myfile Ali Ahmet Can $ cat >myfile Ali Ahmet Can ^D $ cat myfile Ali Ahmet Can myfile Ali Ahmet Can

OUTPUT REDIRECTION: >, >> 17 $ cat >myfile Ali Ahmet Can ^D $ cat myfile Ali Ahmet Can $ cat >myfile Cem Nil ^D $ cat myfile Cem Nil $ cat >myfile Ali Ahmet Can ^D $ cat myfile Ali Ahmet Can $ cat >myfile Cem Nil ^D $ cat myfile Cem Nil myfile Ali Ahmet Can myfile Cem Nil

OUTPUT REDIRECTION: >, >> 18 $ cat >>myfile Canan ^D $ cat myfile Cem Nil Canan $ $ cat >>myfile Canan ^D $ cat myfile Cem Nil Canan $ myfile Cem Nil myfile Cem Nil Canan Cem Nil Canan

INPUT REDIRECTION: < 19  $ mail halici <myletter  “halici” is the destination address to which the mail is to be sent.  The file myletter contains the mail body that should be entered using standard input device if input redirection was to be not used. $ mail halici <myletter

$ ls –R #recursively list my current directory a.c b.c cc.c dir1 dir2 dir1: d.c e.e dir2: f.d g.c $ ls –R #recursively list my current directory a.c b.c cc.c dir1 dir2 dir1: d.c e.e dir2: f.d g.c FILENAME SUBSTITUTION: *, ?, [...] 20 halici a.c Current directory b.c cc.c dir1 dir2 d.c e.e f.d g.c

$ ls *.c #any number of characters followed by “.c” a.c b.c cc.c $ ls ?.c #one character followed by “.c” a.c b.c $ ls *.c #any number of characters followed by “.c” a.c b.c cc.c $ ls ?.c #one character followed by “.c” a.c b.c FILENAME SUBSTITUTION: *, ?, [...] 21 halici a.c Current directory b.c cc.c dir1 dir2 d.c e.e f.d g.c

$ ls [ac]* #either “a” or “c” followed by any number of characters a.c cc.c $ ls [A-Za-z]* # an alphabetic character followed by any number of characters a.c b.c cc.c dir1 dir2 $ ls [ac]* #either “a” or “c” followed by any number of characters a.c cc.c $ ls [A-Za-z]* # an alphabetic character followed by any number of characters a.c b.c cc.c dir1 dir2 FILENAME SUBSTITUTION: *, ?, [...] 22 halici a.c Current directory b.c cc.c dir1 dir2 d.c e.e f.d g.c

$ ls dir*/*.c # all files ending in “.c” in directories starting with “dir” dir1/d.c dir2/g.c $ ls */*.c # all files ending with “.c” in a subdirectory dir1/d.c dir2/g.c $ ls *2/?.? ?.? a.c b.c dir2/f.d dir2/g.c $ ls dir*/*.c # all files ending in “.c” in directories starting with “dir” dir1/d.c dir2/g.c $ ls */*.c # all files ending with “.c” in a subdirectory dir1/d.c dir2/g.c $ ls *2/?.? ?.? a.c b.c dir2/f.d dir2/g.c FILENAME SUBSTITUTION: *, ?, [...] 23 halici a.c Current directory b.c cc.c dir1 dir2 d.c e.e f.d g.c

PIPES: | 24  The shell allows you to use the standard output of one process as the standard input of another one by connecting them using the pipe “|” metacharacter. $ command1 | command2  The above sequence causes the standard output of command1 to flow through to the standard input of command2.  Any number of commands may be connected by pipes. A sequence of commands chained together in this way is called a pipeline.  The standard error channel is not piped through a standard pipeline, although some shells support this capability.

PIPES: | 25 $ ls a.c b.c cc.c dir1 dir2 $ ls |wc –w 5 $ $ ls a.c b.c cc.c dir1 dir2 $ ls |wc –w 5 $ Note that the output of ls command is not displayed when it is piped to wc. ls wc pipeterminal a.c b.c cc.c dir1 dir2 5

PIPES: | 26 If you desire to see the output of intermediate commands use “tee”. tee –a {fileame} + a: append input to the files rather than overwriting them.

PIPES: | 27 The tee utility copies its standard input to the specified files and to its standard output. cmd 1 tee pipe std output device cmd 2 std input device pipe file std error device std error device std error device

PIPES: | 28 $ ls a.c b.c cc.c dir1 dir2 $ ls |tee myfile|wc –w 5 $cat myfile a.c b.c cc.c dir1 dir2 $ ls a.c b.c cc.c dir1 dir2 $ ls |tee myfile|wc –w 5 $cat myfile a.c b.c cc.c dir1 dir2 ls tee a.c b.c cc.c dir1 dir2 wc myfile a.c b.c cc.c dir1 dir2 5 std output devic e

PIPES: | 29 ls tee a.c b.c cc.c dir1 dir2 myfile myfile2 $ ls |tee myfile2 a.c b.c cc.c dir1 dir2 myfile $ cat myfile2 a.c b.c cc.c dir1 dir2 myfile $ ls |tee myfile2 a.c b.c cc.c dir1 dir2 myfile $ cat myfile2 a.c b.c cc.c dir1 dir2 myfile a.c b.c cc.c dir1 dir2 myfile std output devic e

CONDITIONAL SEQUENCES 30  Every UNIX process terminates with an exit value.  By convention, exit value 0 means that the process completed successfully, and a non-zero exit value indicates failure.  All built in shell commands return 1 if they fail.

$ cc myprog || echo compilation failed $ cc myprog && echo compilation succeeded CONDITIONAL SEQUENCES 31  Above, the C compiler named cc compiles a program called myprog and if compilation succeeds echo command will be executed.  In the following case, echo command will be executed if compilation fails.

GROUPING COMMANDS 32  Commands may be grouped by placing them between parantheses.  This causes them to be executed by a child shell (subshell).  The group of commands shares the same standard input, standard output and standard error channels.  They may be redirected as if it were a simple command.

GROUPING COMMANDS 33 $ date; ls; pwd >out.txt # execute a sequence Mon, May 3 15:35:12 MEDT 2005 a.c b.c $ cat out.txt # only pwd was redirected /home122/halici $ (date; ls; pwd) >out.txt # execute a group $ cat out.txt # all the commands were redirected Mon, May 3 15:35:12 MEDT 2005 a.c b.c /home122/halici $ date; ls; pwd >out.txt # execute a sequence Mon, May 3 15:35:12 MEDT 2005 a.c b.c $ cat out.txt # only pwd was redirected /home122/halici $ (date; ls; pwd) >out.txt # execute a group $ cat out.txt # all the commands were redirected Mon, May 3 15:35:12 MEDT 2005 a.c b.c /home122/halici

BACKGROUND PROCESSING: & 34  If you follow a simple command, pipeline, sequence of pipelines or group of commands by the & metacharacter, a subshell is created to execute the commands as a background process.  The background processes run concurrently with the parent shell, but does not take the control of the keyboard. Parent shell Subshell to execute background process

BACKGROUND PROCESSING: & 35  To explain background processing we will use the find command as example.  In the following form it is used to list the filenames which matches the given pattern. find pathlist –name pattern –print  Since it searches all the subdirectories its execution takes time.  When a process executes in background, since it is executing concurrently with the parent shell, its output will interleave with foreground process’ outputs.

BACKGROUND PROCESSING: & 36 find find & $ find. –name a.c –print... search for a.c in the current & sub directories, foreground execution./wild/a.c... output from find./reverse/a.c... some more output as it continues its exec $ find. –name a.c –print &... run the same command in background process id of the background process $ date... date is executing in foreground./wild/a.c... some output from background find Mon May 3 15:40:10 MEDT output from foreground date $./reverse/a.c... foreground prompt and more output from background find $ find. –name a.c –print... search for a.c in the current & sub directories, foreground execution./wild/a.c... output from find./reverse/a.c... some more output as it continues its exec $ find. –name a.c –print &... run the same command in background process id of the background process $ date... date is executing in foreground./wild/a.c... some output from background find Mon May 3 15:40:10 MEDT output from foreground date $./reverse/a.c... foreground prompt and more output from background find date

BACKGROUND PROCESSING: & 37  You may specify several background commands on a single line by seperating them by &. x$ date & pwd &... run date and pwd in background process id of the background process date process id of the background process pwd /home122/halici... output from background pwd $ Mon May 3 15:42:11 MEDT foreground prompt and output from background date x$ date & pwd &... run date and pwd in background process id of the background process date process id of the background process pwd /home122/halici... output from background pwd $ Mon May 3 15:42:11 MEDT foreground prompt and output from background date

REDIRECTING BACKGROUND PROCESSES 38  Outputs of background processes can be redirected in the usual way. $ find. –name a.c –print >find.txt &... background process, output redirected $ ls –l find.txt... look at find.txt -rw-r--r-- 1 halici 0 May 3 15:45:21 find.txt $ ls –l find.txt... watch it as it grows -rw-r--r-- 1 halici 29 May 3 15:46:32 find.txt $ cat find.txt./wild/a.c./reverse/a.c $ $ find. –name a.c –print >find.txt &... background process, output redirected $ ls –l find.txt... look at find.txt -rw-r--r-- 1 halici 0 May 3 15:45:21 find.txt $ ls –l find.txt... watch it as it grows -rw-r--r-- 1 halici 29 May 3 15:46:32 find.txt $ cat find.txt./wild/a.c./reverse/a.c $ file size grows

REDIRECTING BACKGROUND PROCESSES 39  Some utilities also produce output on the standad error channel, which must be redirected in addition to the standard output channel.  Note that 0 represents standard input channel, 1 represents standard output channel and 2 represents standard error channel.

REDIRECTING BACKGROUND PROCESSES 40 $ man ps >ps.txt &... save documentation about command ps in background $ Reformatting page, wait... std error output from “man” done... more std error output from “man $ man ps >ps.txt 2>&1 &... redirect std error channel to the place redirected for std output $ man ps >ps.txt &... save documentation about command ps in background $ Reformatting page, wait... std error output from “man” done... more std error output from “man $ man ps >ps.txt 2>&1 &... redirect std error channel to the place redirected for std output

REDIRECTING BACKGROUND PROCESSES 41  When a background process attempts to read from a terminal, the terminal automatically sends it an error signal which causes it terminate.  To overcome the problem, use input redirection. $ mail halici &... run mail, which needs input, in background $ No message !?!... it does not wait for keyboard and returns $ mail halici <letter.txt &... run mail in background with input redirection, no problem $ $ mail halici &... run mail, which needs input, in background $ No message !?!... it does not wait for keyboard and returns $ mail halici <letter.txt &... run mail in background with input redirection, no problem $