The UNIX Shell Learning Objectives:

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
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.
LINUX System : Lecture 3 (English-Only Lecture) Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University Acknowledgement.
1 Introduction to UNIX Ke Liu
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
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.
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.
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.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
UNIX Utilities Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn Input/Output.
UNIX Filters.
1 UNIX essentials (hands-on) the directory tree running programs the shell (using the T-shell) → command line processing → special characters → command.
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Script Examples.
7/17/2009 rwjBROOKDALE COMMUNITY COLLEGE1 Unix Comp-145 C HAPTER 2.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Linux environment ● Graphical interface – X-window + window manager ● Text interface – terminal + shell.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
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.
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.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
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 Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
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.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
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.
Shell Programming. The UNIX Shell  The UNIX shell listens to what you type and executes commands at your request. User command: lpr file UNIX Shell UNIX.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Searching and Sorting. Why Use Data Files? There are many cases where the input to the program may come from a data file.Using data files in your programs.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 2a – A Unix Command Sampler (Courtesy of David Notkin, CSE 303)
Linux Commands C151 Multi-User Operating Systems.
More Shell Programming. Slide 2 Control Flow  The shell allows several control flow statements:  if  while  for.
– Introduction to the Shell 1/21/2016 Introduction to the Shell – Session Introduction to the Shell – Session 3 · Job control · Start,
Lecture 1: Introduction, Basic UNIX Advanced Programming Techniques.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
6/13/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 3 (UNIX) 6/13/2016Course material created by D. Woit 1.
Foreground and background processes
IT244 - Introduction to Linux / Unix Instructor: Bo Sheng
Lesson 5-Exploring Utilities
Introduction to Shells
Linux Commands Help HANDS ON TRAINING Author: Muhammad Laique
Part 1: Basic Commands/Utilities
Some Linux Commands.
C151 Multi-User Operating Systems
Chapter 6 Filters.
Lecture 9 Shell Programming – Command substitution
Shell Script Assignment 1.
CSE 374 Programming Concepts & Tools
INTRODUCTION TO UNIX: The Shell Command Interface
Basic UNIX OLC Training.
CS 60 Discussion Review.
UNIX Reference Sheets CSE 2031 Fall 2010.
Linux Shell Script Programming
Shell Programming.
More Shell Programming
Chapter 3 The UNIX Shells
Introduction to UNIX EECS July 2019.
Presentation transcript:

The UNIX Shell Learning Objectives: To give detailed description for using commands in Unix Shell To introduce more advanced techniques for handling complicate commands in Unix Shell To understand the the usage of background jobs & shell switching To learn more about pattern matching

How Does the Shell Find a Command? The shell searches a list of directories for an executable file with the same name. $ echo $path shows you the current path The list of directories is stored in the PATH variable for Bourne shells and in the path array for csh/tcsh $ PATH=/usr/local/bin:$PATH sh % set path=(/usr/local/bin $path) csh, tcsh If there is a match in more than one directory, the shell uses the first one it finds. It is usually set in your .cshrc file in your home directory

Alias The C Shell has the alias command, which allows you to create command shortcuts. $ alias dir "ls -F" $ alias rm "rm -i" $ alias + "chmod u+x *" $ alias - "chmod u-x *" $ alias 111 "cd ~horner/111" $ pwd /bin $ 111 /homes/horner/111 If you put the alias commands in your .cshrc file, you can use them every time you login.

Tee A special command called tee acts like a T-joint in plumbing: $ who horner pts/3 Feb 11 10:23 (csnt1.cs.ust.hk) horner pts/0 Feb 11 11:57 (csz096.cs.ust.hk) $ who | sort | tee sortedwho | wc -l 2 $ cat sortedwho $ In this example, the output of sort is placed in a file “sortedwho“ and piped to wc -l, which counts the number of lines.

Background Jobs A simple command or pipeline can be put into the background by following it with the “&” character: $ sort names > names.sort & [1] 3236 The shell will print the process ID (PID), and a job number (1, in this case). Put a job in the background by typing “CTRL-Z” $ ypcat passwd | sort >passwd.sort ^Z Suspended The job is suspended - not running - until you either place it in the background using bg: $ bg [1] ypcat passwd | sort > passwd.sort & or back to the foreground using fg: $ fg ypcat passwd | sort >passwd.sort

Jobs You can stop a job with the kill command: $ ypcat passwd | sort > passwd.sort & [1] 3414 3415 $ kill %1 [1] Terminated ypcat passwd | Exit 2 sort > passwd.sort The “%1” means “job #1”. You can also use the PID. The ps command is the main way to find out about jobs: [1] 3476 3477 $ ps PID TTY TIME CMD 3477 pts/0 0:00 sort 1401 pts/0 0:01 csh 3476 pts/0 0:01 ypcat $

Combining Commands (1) Multiple pipelines can be input on one command line by separating them with semicolons. When entering a long command, use a backslash (\) to continue the command on the next line. $ date; sort names; \ who Thu Feb 11 19:40:28 HKT 1999 Bill Clinton Bill Gates Monica Lewinski horner pts/3 Feb 11 10:23 (csnt1.cs.ust.hk) horner pts/0 Feb 11 19:11 (csz096.cs.ust.hk)

Combining Commands (2) Commands can be grouped together using parentheses There are two main reasons to group commands: To create a “single command” out of a group of commands (especially useful before a pipe): $ (cat names; head -2 names) | wc –l # if names has 8 lines, what does this line return? To run a set of commands in their own subshell (especially when trying to limit the effect of a cd command): $ (cd secret; ls | wc -l); ls | wc -l 3 25 This line has the effect of counting the files in secret, and then counting the files in the current directory. Question: what does this do? cd ; cd 111; (cd ..;cd 122); cd ..

cut and tr cut -cx-y cuts characters from position x to position y (the first position is 1). 1 2 3 4 5 6 123456789012345678901234567890123456789012345678901234567890123 -r--r--r-- 1 horner cs 155 Feb 12 16:00 letter1 What does this do? ls -l | sort +4 | tail -1 | cut -c55-70 The following command returns the user name who am i | cut -f1 -d' ‘ -f1 specifies the first field Field is defined by the delimiter ‘ ‘ The following command translates all lower case to upper case tr “[a-z]” “[A-Z]” filename tr means translate To compress all spaces tr -s '[ ]*' ' ‘ Who | tr –s ‘[ ]*’ ‘ ‘ | cut –d’ ‘ –f2

Patterns The pattern “[abcd]” matches any single one of the enclosed characters. The following “ls” command lists any file whose name start with “i” or “l” followed by any other characters $ ls [il]* it it1 ith its@ letter1 letter4 $ * matches anything The notation “[a-z]” matches any lowercase letter once. The notation “[0-9]” matches any digit character once. The notation “[0-59]” matches any the digit characters 0, 1, 2, 3, 4, 5, and 9. $ ls letter* letter1 letter4 $ ls letter[0-35] letter1 $ ls letter[0-24]

More Pattern Matching – grep (1) grep finds all matches of a pattern in a file : To build a pattern, use the following: ^ start of line $ end of line . any character \ quote next character * match zero or more + match one or more ? match zero or one [aeiou0-9] match a,e,i,o,u, and 0 thru 9 [^aeiou0-9] match anything but a,e,i,o,u, and 0 thru 9 ‘a*’ match zero or more occurrences of ‘a’ ‘a+’ match one or more occurrences of ‘a’ (egrep)

More Pattern Matching – grep (2) $ cat names Bill Gates Jacky Gs James Bill J. Jones $ grep '^Bill’ names $ grep 'Bill$’ names $ grep ’J...s’ names $ grep 'G.*s' names Bill Gates Jacky Gs $ grep 'G.+s' names $ egrep 'G.+s' names $ egrep 'G.s' names $ egrep 'G.?s' names J. Jones $ grep 'J\.' names Some versions of grep cannot process ?+

More Pattern Matching – grep (3) $ cat names Bill Gates Jacky Gs James Bill J. Jones $ grep 'Ja[cm]' names $ grep 'J[^a]' names $ grep ’[t-y]' names $ grep -in bill names 1:Bill Gates 3:James Bill $ grep -v Bill names Jacky Gs J. Jones $ grep -l Bill n* names $ grep -c Bill names 2 -i ignores case -n shows line number -v means line *not* containing pattern -l shows *filename* containing pattern -c shows matching count

How to do this? Find all lines in ‘file1’ that does not contain any vowels Hint: use –v option Find all lines in ‘file1’ that contains only vowels. Hint: use ^string$ Find all lines in file1 with length 7 and ‘a’ as the fourth character Find all lines that contain ‘a’, ‘c’, and ‘e’, in that order