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.

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.
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.
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.
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.
CS 497C – Introduction to UNIX Lecture 20: - The Shell Chin-Chih Chang
Information Networking Security and Assurance Lab National Chung Cheng University 1 What Linux is? Free Unix Like Open Source Network operating system.
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.
1 UNIX essentials (hands-on) the directory tree running programs the shell (using the T-shell) → command line processing → special characters → command.
Shell Script Examples.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
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”
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.
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.
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.
An Introduction to Unix Shell Scripting
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.
CS 2061 Shells Also known as: Unix Command Interpreter.
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.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
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.
System Administration Introduction to Unix Session 2 – Fri 02 Nov 2007 Reference:  chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
OPERATING SYSTEMS DESIGN UNIX BASICS & SHELL SCRIPTING.
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.
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.
Workbook 6 – Part 2 The Bash Shell
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.
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Contents Why an Operating System (OS) How.
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.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Week 9 - Nov 7, Week 9 Agenda I/O redirection I/O redirection pipe pipe tee tee.
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.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Chapter 5: The Shell The Man in the Middle. In this chapter … The command line Input, output, and redirection Process management Wildcards and expansion.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
Lecture 02 File and File system. Topics Describe the layout of a Linux file system Display and set paths Describe the most important files, including.
Linux+ Guide to Linux Certification, Second Edition
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.
Learning Unix/Linux Based on slides from: Eric Bishop.
Linux Administration Working with the BASH Shell.
Shells.
Introduction to Shells
Shell Features CSCI N321 – System and Network Administration
Some Linux Commands.
John Carelli, Instructor Kutztown University
Linux Shell Script Programming
Chapter 5 Bourne Shells Scripts
UNIX and Shell Programming (06CS36)
Chapter 3 The UNIX Shells
Presentation transcript:

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 line and makes arrangements for its execution. Generally waits for command to complete execution (locking the terminal). A variety of shells to choose from: Bourne, Korn, C, Bash, Tcsh Killed on logging out.

The Shell Metacharacters Wildcard characters such as *, ?, [, ], ! Redirection characters such as >, < The pipe character, | Command substitution characters ` ` The $ as a variable prefix

cat chap* Shell expands * to match all filenames in the current directory that begin with chap. date > foo Shell sees the > first, opens the file foo and connects the date output to it. who | sort Shell understands the strings on either side of the | as two separate programs and connects them. ls`cat foo` Shell first runs cat and supplies the output as arguments to ls. echo $HOME Evaluates $HOME as a variable before running echo. Examples of Shell Behavior

Wildcards Set of metacharacters used in an expression to match multiple but similar filenames. * means any sequence of characters (including none) ? means a single character [ ] means any characters specified in the bracket ! means not - means a range of characters or numbers Shell creates list of filenames before allowing command to run. Expansion can be prevented by quoting and escaping. Filenames must not contain wild-card characters.

Some Wildcard Examples ls *.lst Lists all files with extension.lst. rm ??* Removes all files with at least 2 characters. cp *.[ch] cprogs Copies all files with.c or.h extension into cprogs. rm *[!a-zA-Z]* Removes files not containing at least one letter. cat note[0-1][0-9] Displays files note00, … note19 rm *.o Removes all files then removes all files with.o extension (an error). Watch out!

Escaping (Using a \ Before a Character) Escaping reverses the usual meaning of metacharacter following it. Example: rm \* removes a file named * Can also protect itself. Example: echo \\ displays a \ Protects space and [Enter]. Example: cd My\ Documents changes to the directory named My Documents Inconvenient to use when command line contains too many metacharacters that need to be escaped.

Quoting Quoting protects most metacharacters from interpretation by the shell. Example: echo “*” prints a * More convenient than escaping when protecting a group of metacharacters. Quoted string understood as a single argument by shell and C programs. (a.out foo “My Documents” has 2 arguments and not 3.) Double quotes and single quotes are not equivalent. Example: echo “$SHELL” is not the same as echo ‘$SHELL’ Quoting doesn’t protect the \ metacharacter. Escaping is required. Quoting

Be Careful Quoting Special Characters Like “, ‘ and \ Try these: echo ‘ ‘ ‘ echo “ “ “ echo ‘ “ ‘ echo “ ‘ “ echo ‘ \ ‘ echo “ \ “

File Handling in the Shell A file is opened by accessing it by its pathname. Opening returns a file descriptor (an integer). Subsequent read/write operations on the file use the descriptor. Kernel allocates lowest available number as descriptor. First three descriptors (0, 1 and 2) are always allocated : 0 is standard input, 1 is standard output, 2 is standard error

Standard Input Uses file descriptor 0. By default, assigned to the user’s terminal keyboard. Can also be obtained from standard output of another program (redirection). Commands using redirection continue to read descriptor 0 and have no knowledge of this manipulation.

Standard Output Uses file descriptor 1. By default, assigned to the user’s display terminal. Can also be used as input to another program (redirection). Commands using redirection continue to write to descriptor 1 and have no knowledge of this manipulation.

Standard Error Uses file descriptor 2. By default, file is assigned to the user’s display terminal. Standard error output can’t be used as input to another program but error output can be redirected to a file (redirection). Commands using redirection continue to write error messages to descriptor 2 and have no knowledge of this manipulation.

Redirection Using, and >> Redirection is a shell feature for manipulating command input and output. Most commands designed to write output to standard output. Most commands designed to take input from standard input when used without a filename as argument. Shell can assign these files to disk files through redirection: wc prog.c > countfile wc prog.c >> countfile wc < prog.c wc prog.c > countfile 2> errorfile Commands themselves have no knowledge of the reassignment.

Redirection Using a Pipe | Connects standard output on one command to standard input of another Example: who | wc –l Displays # users on system No temporary file is created. Used to solve many text manipulation problems. Commands have no knowledge that they are reading from or writing to a pipe. tee command duplicates its input and sends to both file and standard out Example : ls | tee filelist List files on screen and in a file.

Two More Special Files to Redirect Output /dev/null basically sends out nowhere. File size is always zero. Examples: cat myfile > /dev/null Command does nothing! find / -name myfile –print 2> /dev/null This command hunts for file named myfile starting at root and lists it. Send all error messages nowhere. /dev/tty is the file indicating your terminal (not the same file as standard output or standard error!) Example: who > /dev/tty Lists users at your terminal.

Command Substitution Allows command arguments to be obtained from standard output of another command. In command1 `command2`, command2 is run first and its standard output used as arguments to command1. Example: echo Today is `date` Command enclosed by ` ` (backquotes) must write to standard output. Convenient mechanism for running commands whose arguments are known only at runtime. Example: echo “There are `ls | wc –l` files in this directory.” (Use double quotes around echo, not single quotes.)

Shell Variables Users define shell variables as variable=value. Referenced with $. Environment variables such as SHELL, HOME, and PATH are shell variables. Example: echo $HOME Variables have no type and need no declaration before being used. All shell variables initialized to null ( x=“” or x=‘’ or x= ). To remove a shell variable, use unset. Example: unset x To prevent changes to a shell variable, use readonly. Example: readonly pi

Using Shell Variables Set a variable x to 5 and display the value: x=5 ; echo $x (no whitespace on either side of =) x = 5 is illegal; x is interpreted as a command to run. Can be set from command substitution: Example: directory=`pwd` Useful for storing pathnames that are repeatedly used in a shell script: Example: addressbook=$HOME/data/addressbook