Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.

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.
EMT 2390L Lecture 4 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts.
A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 4: More Command Line Interface (CLI) Chapter 7: The Linux Shell By Fred R. McClurg Linux.
Using Linux Commands Lab 4 1. Create empty files To create an empty file $ touch apple banana grape grapefruit watermelon $ ls -l Using file-matching.
Introduction to UNIX CSE 2031 Fall May 2015.
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.
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.
7 Searching and Regular Expressions (Regex) Mauro Jaskelioff.
CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
The UNIX Shell Software Tools. Slide 2 Basic Shell Syntax command [-[options]] [arg] [arg] … l The name of the command is first l Options are normally.
Linux+ Guide to Linux Certification, Second Edition
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
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
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Script Examples.
3 File Processing Mauro Jaskelioff. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash.
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.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
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.
Guide To UNIX Using Linux Fourth Edition
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.
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.
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.
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.
Workbook 6 – Part 2 The Bash Shell
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
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.
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
I/O Redirection & Regular Expressions CS 2204 Class meeting 4 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
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.
CSCI 330 UNIX and Network Programming Unit IV Shell, Part 2.
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 IV Shell, Part 2.
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
Agenda The Bourne Shell – Part I Redirection ( >, >>,
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Linux Administration Working with the BASH Shell.
CIRC Summer School 2016 Baowei Liu
Introduction to Shells
Shell Features CSCI N321 – System and Network Administration
The UNIX Shell Learning Objectives:
CIRC Winter Boot Camp 2017 Baowei Liu
CSE 303 Concepts and Tools for Software Development
John Carelli, Instructor Kutztown University
Chapter Four UNIX File Processing.
Linux Shell Script Programming
Chapter 3 The UNIX Shells
LPI Linux Certification
Presentation transcript:

Shell Basics CS465 - Unix

Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection of input and output –Pipelines –Background execution –Script-programming facility

Running Multiple Commands ; Causes commands to run in sequence. $ cmd1 ; cmd2 ( )Causes sequenced commands to be grouped $ (cmd1 ; cmd2) && Runs the next command only if the current command succeeds. $ cmd1 && cmd2 ||Runs the next command only if the current command fails. $ cmd1 || cmd2

Sequence Example Commands can be sequenced (run in sequence) using the semicolon (;): $ ls ; who ; date ; pwd file1 file2 file3 newfile small000 pts/1 Jun 4 14:41 Tue Jun 4 14:42:51 MDT 2003 /export/home/small000 $ If any one command fails, the others will still execute. NOTE: To make the command line easier to read, separate commands from the semicolon (;) with blanks.

Grouped Commands Example Commands can be grouped together using parentheses –To create a “single command” out of a group of commands (especially useful before a pipe): $ (cat letter1; head -2 names) | sort

&& Example To run the second command only if the first command succeeds (i.e. short-circuit “and” operator): BAD: $ cd /somedir; rm -r * GOOD: $ cd /somedir && rm -r *

|| Example To run the second command only if the first command fails (i.e. short-circuit “or” operator): Example: $ ls file1 || cp fileX file1

Shell Environment Your environment defines such characteristics as your user identity, where you are working on the system, and what commands you are running. Your working environment is defined by both environment variables and shell variables. –Your default login shell uses environment variables and passes them to all processes and subshells that you create. –Shell variables are valid only for your current shell and are not passed to subshells.

Environment Variables HOME Name of your login directory. Set by login program. HOST Name of host machine running your shell program. LOGNAME Your login name. Set by login program. MAIL Pathname of the file used by the mail system to detect the arrival of new mail. Set by login program.

Environment Variables PATH Directory list and order that your system uses to search for, find, and execute commands. Set by login scripts. SHELL Your default shell. Set using the shell specified in your entry in the /etc/passwd file. TERM Type of terminal you are using. Set by your login script. TZ Your current time zone. Set by the system login script.

Environment Variables To see the value of a defined variable, place a $ in front of the variable name: $ echo $HOME /home/small000 $ echo $TZ MDT $ Note the difference if you leave off the "$": $ echo HOME HOME

Metacharacters Metacharacters are used to create patterns to match filenames and command names. –For example, we often wish to apply a command to a selection of files or directories –The shell wildcard (often known as globbing) feature allows this. Each shell program recognizes a set of special characters called metacharacters: ; & ( ) | \ ^ ~ % { } $ # ´ “ * ? [ ] !

Wildcard Metacharacters ? matches any character * matches any number of characters [abc] matches any one of the characters in the brackets [a-z] matches any one character in the range of characters in the brackets [!a-z] matches any character except the range of characters in the brackets

Wildcard Examples $ ls abc?def –matches abcXdef, abc.def, abc3def but not abcdef $ ls abc* –matches abc., abcd, abcdef, abc.another.part $ ls *.* –matches any name with a “.” in it $ ls * –matches any name (except names beginning with “.”) $ ls [a-z]* –matches any name beginning with a lower-case letter

Wildcard Exercise Assume we have the following files under the current directory: a.outc22cap Doc.1Doc.2Doc.3 one.ctwo.cthree.c What would match for: ls *.cls ?2? ls D*ls [a-z]*[!0-9] ls ???ls [23]* ls *ls.*

Avoiding Wildcard Expansion Sometimes you may NOT want wildcard characters expanded. The Unix glob utility translates wildcard characters on the command line. To turn off ALL wildcard expansion: $ set noglob To turn ALL wildcard expansion back on: $ set glob

Avoiding Wildcard Expansion Sometimes you need to selectively enter a wildcard character on a command line, and not have it expanded. Method 1: Double quote the argument containing the character: $ ls “abc*def” abc*def ls the file whose name is abc*def (if the system allows such a name)

Avoiding Wildcard Expansion Method 2: “Escape” the character $ ls abc\*def –again lists the file whose name is abc*def Can use these methods in combination $ echo “Show asterisks *” and \* Show asterisks * and * $ –* inside the double quotes is not expanded –* after the backslash is not expanded

Double Quotes vs Single Quotes Within double quotes, the reserved characters: dollar sign ($) grave accent (`) backslash (\) keep their special meanings All other characters are taken displayed literally.

Double vs Single Quote Example Displaying a variable name without it being interpreted by the shell: $ echo “Value of $USER is “ $USER Value of small000 is small000 $ $echo 'Value of $USER is ' $USER Value of $USER is small000 $

Grave Accent/Backquotes Within backquotes (graves), command substitution is forced: Example: $ echo I am user whoami I am user whoami $ echo I am user `whoami` I am user small000 $

Summary of Quoting Metacharacters Single quotes –Contents are used verbatim No wildcard expansion No variable interpolation No command execution Double quotes –Only turns off wildcard expansion Backquotes –Forces execution of command in quotes –Output from command replaces quoted string

Redirection of I/O Every program run under Unix is provided with three standard I/O streams: stdin, stdout and stderr command stdin (0) stdout (1) stderr (2) By default: - the standard input (stdin) is the keyboard - the standard output (stdout) and standard error (stderr) are the display screen Your shell allows you to divert any or all of these streams.

Output Redirection Use the “>” character to redirect the standard output of a command to a file: $ ls > myfiles $ cat myfiles file1 file2 file3 $ $ echo hello $USER > greeting $ cat greeting hello small000 $ echo hi there > greeting $ cat greeting hi there $

Output Redirection Use the “>>” character after a command to redirect output to APPEND to a file: $ cat greeting hi there $ echo “How are you?” >> greeting $ cat greeting hi there How are you? $

Output Redirection $ ls x* ls: x*: No such file or directory $ ls x* 1>xfiles 2>errs $ cat xfiles $ cat errs ls: x*: No such file or directory $ echo roses are red 1> rhyme 2> errs $ echo violets are blue 1>> rhyme 2>> errs $ cat errs $ cat rhyme roses are red violets are blue $ Can separate stdout (1) and stderr (2):

Output Redirection $ cat test type line 1 type line 2 $ $ cat > test type line 1 type line 2  to end your input $ Redirecting cat’s output can create a file (no input file name given, so gets input from stdin):

Output Redirection  Typing to append to the end of a file $ cat >> test type line 3 type line 4 $ cat test type line 1 type line 2 type line 3 type line 4  Appending file1 to file2 $ cat file1 >> file2

Overwriting Files? By default, the use of ‘>’ will overwrite any existing file, without warning. Use “noclobber” to prevent this: $ set –o noclobber(+o to revoke) Gives error if you try to overwrite an existing file. Can still use >| to force overwriting of an existing file. $ ls file1 file2 $ set -o noclobber $ cat file1 > file2 ksh: cannot create file2: File exists $ cat file >| file2 $

Input Redirection Use the “<” character to redirect input from a file to a command: $ sort < names $ mail user < letter.txt Can use input and output redirection together: $ sort names.sort

Filters Most UNIX utilities (commands) are filters A filter is a program which: –reads input (text) from standard input only –writes output and errors to standard output only Filters can be combined to work together using pipes A pipe takes the stdout of one command and uses it as the stdin of another command

Pipes The standard output of one program is “piped” to the standard input of another: $ who | sort Several pipes can be connected: $ ls | sort | more No limit on number of processes that can be chained by pipes

Pipes Pipes and I/O redirection can be used together: $ ls | sort > files.sorted $ sort < names | more

Why Pipes? Could chain commands together $ ls > temp; sort < temp; rm temp Works, but requires temporary intermediate file, which must be manually removed Solution – Use a pipe: $ ls | sort –No intermediate file, processes run in parallel

Multiple Outputs What if you want the output of a command to be saved in a file AND displayed on the screen? Use the tee command Example: $ ls –la | tee filelist Displays directory listing to the screen, AND saves it to the file filelist.

Summary of Shell Command Interpretation The shell reads a line from a script file or from a user. Then: 1.Meta-characters are "handled.“ 2.Executable is found (via PATH). 3.Arguments are passed to the program. 4.File redirection is setup. 5.The program is executed.