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.

Slides:



Advertisements
Similar presentations
1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview.
Advertisements

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.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Chapter Seven Unix Shell Environments1 System Programming UNIX Shell Environments.
CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
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
CS 497C – Introduction to UNIX Lecture 36: - Customizing the Environment Chin-Chih Chang
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 Script Examples.
Introduction to Shell Script Programming
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.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
Unix Shells Based on Glass & Abels’ Book CS240 Computer Science II.
Introduction to Computer Organization & Systems Topics: Intro to UNIX COMP John Barr.
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.
CS 2061 Shells Also known as: Unix Command Interpreter.
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.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
UNIX/LINUX Shells Shell is an UNIX/LINUX command interpreter. Shell command can be internal or external. The code to execute an internal command is part.
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
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.
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.
Unix Shell Environments February 23rd, 2004 Class Meeting 6.
UNIX shell environments CS 2204 Class meeting 6 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
User Environments Objectives –to provide appropriate environments for different types of users Contents –different login programs –user profiles –restricted.
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
Introduction to Programming Using C An Introduction to Operating Systems.
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:
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.
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.
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.
Chapter 8: The Bourne Again Shell It’s a command interpreter, it’s a programming language, and it makes a mean martini.
Jozef Goetz, expanded by Jozef Goetz, 2006 Credits: Parts of the slides are based on slides created by textbook authors, Syed M. Sarwar, Robert.
Linux+ Guide to Linux Certification, Second Edition
Agenda The Bourne Shell – Part I Redirection ( >, >>,
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
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.
Agenda Customizing a Unix/Linux account Environment Introduction to Start-up Files (.bash_profile,.bashrc,.profile,.kshrc) Safe Methods for Changing Start-up.
Shells.
Introduction to Shells
Shell Features CSCI N321 – System and Network Administration
Prepared by: Eng. Maryam Adel Abdel-Hady
CSE 303 Concepts and Tools for Software Development
UNIX and Shell Programming (06CS36)
John Carelli, Instructor Kutztown University
CSCI The UNIX System Shell Startup and Variables
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Presentation transcript:

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 not a programming language ▫However, most shells do support scripting The shell may: ▫Process commands (a specific action) ▫Process scripts (list of commands) Shells may be CLI or GUI

Shells Command Processor ▫Commands typed on a terminal  Executed by the shell  Boune family  C family  many others… ▫Note: examples will center on the bash shell  bash – Bourne Again SHell  Shells do vary on implementation of some features

Pattern Matching: Wild Cards FilenamesWild Card Examples Replace parts of names with arbitrary matching character(s) Combination of known and unspecifed characters ▫?, *, […], {…} Wild CardMatches *any number of chars including none ?exactly one character [aml]single character from list – either a, m, or l in this case [a-l]single character – in the range a to l [!a-l]single character – not in the range a to l { pat1, pat2…}matches specifed patterns

Standard Files: Input/Ouput/Error Standard input (std in) ▫Where the data is coming from  File  Stream ▫Usually the keyboard or terminal Standard output (std out) ▫Where the data is going to  File  Stream ▫Usually the display or terminal Standard error (std err) ▫Where error messages are sent  File  Stream ▫Usually the display or terminal  Default is same as Standard output

Data Sources/Redirection Important: In Unix/Linux everything is a file Terminal ▫Keyboard - source ▫Screen or printer – sink File redirection ▫> and >>  Send the standard data to a file ▫<  Receive the standard data from a file Piping ▫|  Send the output of one program to the input of another Different utilities and programs will handle defaults for std in and std out differently

Resume 8/27

Shell Example – wc command wc ▫word count  reads the input and counts and reports the number of:  lines  words  characters  Examples of data source and sink using wc : ▫No parameters: data will flow from std in:  wc This is a line of data This is another ▫File as a parameter  wc /dir/file /dir/file  Note it has the filename in the output  Can work on a list of files ▫Redirection from a file  wc < /dir/file ▫Piped from another program  ls | wc 6660

File Descriptor Standard files have numeric representations ▫ 0 – Standard input ▫ 1 – Standard output ▫ 2 – Standard error Example: ▫ #cat bad.file cat: bad.file: No such file or directory #cat bad.file 2> err.msg #wc err.msg 1741 err.msg #more err.msg cat: bad.file: No such file or directory

Problem: want std out and std err to go to same destination Example: file1 exists, file2 does not ▫ #cat file1 file2 file 1 data cat: file2: No such file or directory redirect std out ▫ #cat file1 file2 1> report cat: file2: No such file or directory #more report file 1 data redirect std err ▫ #cat file1 file2 2> report file 1 data #less report cat: file2: No such file or directory cannot consistanly redirect both streams to the same file, need another method ▫ Can use >> for one or replicating descriptors ▫ May show order differently or not work at all

Replicating Descriptors 1>&2 ▫send std out to destination of std err 2>&1 ▫send std err to destination of std out redirect std err, std out ▫ #cat file1 file2 2> report 1>&2 #less report file 1 data cat: file2: No such file or directory

Special files /dev/null ▫“bit bucket” /dev/tty ▫your default terminal ▫useful when different users log on ▫everyone can use /dev/tty as their terminal ▫Remember: everything in Unix/Linix is a file!

Pipes Send output of one command as the input to another command or program Examples ▫ #ls | wc  Sent the short directory listing to wc ▫ #ls –l | wc  Sent the long directory listing to wc

Tee Sends data to std out and a file ▫Not part of the shell Example: ▫ #who | tee users.txt ajk tty :14 (:0) ajk pts/ :45 (:0.0) #cat users.txt ajk tty :14 (:0) ajk pts/ :45 (:0.0)

Command substitution Command arguments can be obtained or substituted from the std out of a program: ▫Use backticks ` to denote  That is not the single quote '  AKA Accent Gravé Examples: ▫ #echo This year is `date +%Y` This year is 2013 ▫ #ls –la `cat filelist` ls: cannot access file2: No such file or directory ls: cannot access file4: No such file or directory -rw-r-r- 1 ajkombol ajkombol 14 Mar 3 16:34 file1 -rw-r-r- 1 ajkombol ajkombol 14 Mar 3 16:34 file3 #cat filelist file1 file2 file3 file4

Shell variables Can assign values to variables ▫ myvar=myvalue  Important: no spaces!  Note: no $ is needed to assign  Case sensitive Use by putting $ in front ▫ $myvar Example: ▫ #ext=.sh #name=doit #compname=$name$ext #echo $compname doit.sh

Scripts Intro Shells can execute groups of commands in a file ▫Scripts Script basics ▫They have basic control sequences  e.g. conditionals and looping  if, for, while ▫They may contain commands ▫They must have execute permission

Using the shell to modify your runtime environment

Shell variables - Environment Two shell variable types: ▫Environment  Used by a user in general  PATH  HOME  SHELL  Etc…  Note:  By convention environment variables are UPPER CASE ▫Local  Used for specific purposes  Typically available to current process only Case sensitive

Local Variables Easy to create for temporary use ▫ my_var=value Restricted in scope

Environment Variables Export to create an Environmental Variable ▫ export ENV_VAR Can apply value at creation or later Good for creating a set of common definitions

Using the shell

Setting the default shell What shell is set as your default? ▫ echo $SHELL ▫Returns something like:  /bin/bash -or-  /usr/bin/bash ▫Depends on distro Popular shells ▫bash ▫csh ▫korn ▫bourne

Environmental Variables Available in the user’s total environment ▫Sub-shells  Scripts they run  Editors  Mail commands  etc. Local variables ▫Only available to the current process set ▫displays all variables in the current shell env ▫shows only environment variables

export Turns a local variable into an environmental variable ▫Basically makes a variable visible to all child processes ▫Needs only to be done once for a session ▫Needs to be redone every time a new session is started Form: ▫ export ENV_VAR

Common Environment Variables VariableSignificance HOME users home directory PATH list of directories to search for a command LOGNAME login name of user USER login name of user MAIL absolute pathname to users mail file MAILCHECK how long to wait before checking for new mail TERM type of terminal PWD absolute name of users current directory PS1 primary prompt string PS2 secondary prompt string SHELL users login shell

Aliases Use alternate names ▫"Shorthand" for commonly used commands  #alias vi='vim'  will always run vim when vi is typed  can run vi with \vi ▫Assign common options  #alias cp="cp -i"  will change cp to always be interactive ▫Asks if duplicate names found  #alias cp  will return the current assignment  #unalias cp  will get rid of the alias

Command history Allows recalling previous commands ▫Shows all the previous commands with event numbers  holds about 500 events  Will vary by distro ▫ #history n  shows last n command ▫ #!n  executes event n ▫ #!n:p  prints event n (does not execute)

Tilde Shorthand for the current users home directory Notes: ▫ #cd ~  changes to current user’s home directory ▫ #cd ~userid  changes to userid’s home directory

Using set By default, set shows all variables Interesting options ▫ #set –o noclobber  prevents accidental overwriting of existing files with redirection (the > and >> symbols) ▫ #set –o ignoreeof  prevents accidental termination of script ( d) ▫ #set –o notify  allows completed background jobs to notify immediately when done

Initialization scripts login script ▫runs once at log in ▫varies by distro .bash_profile .profile .bash_login ▫usually in users home directory rc script ▫runs every time an interactive sub-shell is created ▫varies by distro, usually has rc in its name .bashrc Used to customize the users environment ▫The prompt is usually set in one of these