Software I: Utilities and Internals Lecture 8 – Shell Programming Using bash.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History...
-
Upload
corey-rose -
Category
Documents
-
view
215 -
download
0
Transcript of Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History...
![Page 1: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/1.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/2.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/3.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/4.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/5.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/6.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/7.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/8.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/9.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/10.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/11.jpg)
Checking Out The Shell Environment
![Page 12: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/12.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/13.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/14.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/15.jpg)
Examples: What Do These Mean?
![Page 16: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/16.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/17.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/18.jpg)
Subprompt Examples
![Page 19: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/19.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/20.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/21.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/22.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/23.jpg)
History In Action
![Page 24: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/24.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/25.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/26.jpg)
Examples
![Page 27: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/27.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/28.jpg)
Examples
![Page 29: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/29.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/30.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/31.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/32.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/33.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/34.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/35.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/36.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/37.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/38.jpg)
Jobs
![Page 39: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/39.jpg)
The Foreground and the Background
![Page 40: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/40.jpg)
Running Jobs In The Background
![Page 41: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/41.jpg)
Suspending Jobs
![Page 42: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/42.jpg)
Switching Jobs To The Foreground
![Page 43: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/43.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/44.jpg)
The nohup Command
![Page 45: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/45.jpg)
Stopping Rogue Processes
![Page 46: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/46.jpg)
Signals And What They Mean
![Page 47: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/47.jpg)
The kill Command
![Page 48: Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.](https://reader035.fdocuments.us/reader035/viewer/2022062518/56649e915503460f94b95bf4/html5/thumbnails/48.jpg)
The ps Command