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

Post on 12-Jan-2016

215 views 0 download

Transcript of Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History...

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

Different Shells sh

Bourne Shell csh

C shell tsch

Turbo C Shell ksh

Korn shell bash

Bourne Again Shell

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

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

environment variable SHELL printenv SHELL echo $SHELL

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

Startup Files .login .profile .bashrc .tcshrc

What’s In a Configuration File

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

Checking Out The Shell Environment

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

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

Special Prompt Symbols These only work when interpreting

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

Examples: What Do These Mean?

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

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

Subprompt Examples

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

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

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

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

commands Assigns a number to each of them

Command number

History In Action

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

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

Examples

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

Examples

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

Aliases Making another name for some

other command Useful for making sure certain

flags are always used Could cause trouble as well…

Setting Aliases Usage: alias NAME=“COMMAND”

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

Safe cp

Usage: unalias NAME Example: unalias cp

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

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

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

grep

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

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

Coming Up… Midterm and lab practical this

Thursday Next week

Job control Beginning shell programming

Jobs

The Foreground and the Background

Running Jobs In The Background

Suspending Jobs

Switching Jobs To The Foreground

Switching Suspended Jobs To The Background

The nohup Command

Stopping Rogue Processes

Signals And What They Mean

The kill Command

The ps Command