48
Introduction to Unix – CS 21 Lecture 9

Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Embed Size (px)

Citation preview

Page 1: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Introduction to Unix – CS 21

Lecture 9

Page 2: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Lecture Overview Shell description Shell choices History Aliases Topic review

Page 3: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

The Shell – It’s That Thing You’re Typing In The command line itself is just another

Unix program that is running A program that runs other Unix

commands An interface between the user and the

system Called a shell

That’s why in emacs the command is Meta-X shell

Page 4: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Different Shells sh

Bourne Shell csh

C shell tsch

Turbo C Shell ksh

Korn shell bash

Bourne Again Shell

Page 5: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Why So Many Choices, And What Does It Mean? All shells run programs the same way

A sort executed from csh works the same way a sort executed from bash does

Shells differ in all the bells and whistles Different shells have different features

that make them appealing Shell programming is a huge difference

Page 6: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Which Shell Are You Using? Most likely you are using bash You can check by printing the

environment variable SHELL printenv SHELL echo $SHELL

Page 7: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Switching Shells Run the command and you’re in

Must exit from multiple shells then chsh – a program to change your

shell every time you login Checks the file /etc/shells

Page 8: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Startup Files .login .profile .bashrc .tcshrc

Page 9: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

What’s In a Configuration File

Page 10: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

When Do All These Files Get Executed? .profile and .login only get read

once when you login .bashrc and .tcshrc get run

everytime a new shell is created

Page 11: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Checking Out The Shell Environment

Page 12: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

What Do All Those Environment Variables Mean? HOSTNAME TERM SHELL HISTSIZE USER PWD HOME VISUAL EDITOR

Page 13: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Customizing Your Prompt The environment variable PS1

determines the appearance of your prompt

You can set the prompt to be anything you’d like Suggestion: don’t make it too

confusing or cluttered

Page 14: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Special Prompt Symbols These only work when interpreting

the prompt environment variables \H and \h \T and \t and \@ \u \w and \W \!

Page 15: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Examples: What Do These Mean?

Page 16: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

A Peek Ahead If you would like more advanced

functionality in your prompt, you can add a program to execute with the ` (backtick)

Example: export PS1=“\H: `pwd`\n>” This will print the working directory

every time the prompt is generated

Page 17: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Subprompt: PS2 \

Continue on the next line ‘

Start of a quote – looks for another ‘ “

Start of a quote – looks for another “ Our system sets up “loop $” as the

default PS2 I find this pretty confusing, as it really isn’t

looping at all

Page 18: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Subprompt Examples

Page 19: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Question: What happens if you “unset PS1”? You have no prompt! Really confusing

Page 20: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Bash History Bash will keep track of the most

recent commands you have entered This number can be set HISTSIZE

Keeps track even after you logout

Page 21: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

The .bash_history File The history is stored in a hidden file

in your HOME directory called .bash_history

It is updated after you logout Changes won’t appear until you logout If you have two terminal sessions, the

last one you logout on will be the most recent history

Page 22: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

The history Command Usage: history [NUMBER] Prints out all of your most recent

commands Assigns a number to each of them

Command number

Page 23: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

History In Action

Page 24: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

O.k., So I Can See What I’ve Done, So What? Bash gives you shortcuts to easily

redo commands in your history !!

Repeat the last command

Page 25: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Bash Shortcuts Into The History Directly execute a history

command based on: Command number

!123 First characters of the command

!m Will always match the most recent

command that matches, not necessarily the one you want

Page 26: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Examples

Page 27: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Getting More Out Of History !*

Everything but the first word of the previous command

Allows you to run a different command with the same arguments

!$ Gets you just the last word of the

previous command

Page 28: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Examples

Page 29: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Bash And Emacs Certain emacs commands work in

bash Searching backwards will search

through the history backwards ctrl-a and ctrl-e get you to the

beginning and end of a line respectively

Page 30: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Aliases Making another name for some

other command Useful for making sure certain

flags are always used Could cause trouble as well…

Page 31: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Setting Aliases Usage: alias NAME=“COMMAND”

Example: alias ll=“ls –l” Example: alias cp=“cp –i”

Safe cp

Usage: unalias NAME Example: unalias cp

Page 32: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Everything We’ve Looked At – Week 1 History of Unix

Open source effect The directory structure

What goes where How to move around

Where to get help Man pages

Page 33: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Everything We’ve Looked At – Week 2 Disk space and compression

du, gzip, zip, bzip2 Permissions and ownership

chmod Copying, moving, and deleting files

cp, mv, rm Checking out file content

file, cat, more, less

Page 34: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Everything We’ve Looked At – Week 3 Input and output streams File redirection Piping Regular Expressions

grep

Page 35: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Everything We’ve Looked At – Week 4 emacs

Basic commands Buffers and switching Searching and r.e. searching

vi Command mode versus insertion mode Various commands Searching and r.e. searching

Page 36: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Everything We’ve Looked At - Today Yes, I could ask a question

regarding today’s material Shell definition and choices The history mechanism Using aliases

Page 37: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Coming Up… Midterm and lab practical this

Thursday Next week

Job control Beginning shell programming

Page 38: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Jobs

Page 39: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

The Foreground and the Background

Page 40: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Running Jobs In The Background

Page 41: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Suspending Jobs

Page 42: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Switching Jobs To The Foreground

Page 43: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Switching Suspended Jobs To The Background

Page 44: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

The nohup Command

Page 45: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Stopping Rogue Processes

Page 46: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

Signals And What They Mean

Page 47: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

The kill Command

Page 48: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review

The ps Command