CS 330 Lecture 2 – Shell Scripting
Agenda
- what ?s
- mass programming
- zip a directory to directory.zip
- run a C source file
- safe rm
- top10 big files
- bookmark a directory
- upload file to www share directory
- re-extension
TODO
- Read Shell Programming with Bash.
- On a 1/4 sheet, write some shell command lines to accomplish the following tasks. Be sure to consult man pages!
- Replace all uppercase letters in a file with their lowercase equivalents.
- Make a directory and all of its contents readable. Also make them executable if the owner/user has execute permissions.
- Make a directory with a name like
YYYY_MM_DD
. Do not hard code the date. That is, if I run your command today, it will make2016_01_27
. But tomorrow if I run the exact same command, it’ll make2016_01_28
. - Write a function
cnd
that makes a directory and also moves into it.
Note
Today we’re going to write shell scripts together to accomplish some tasks that have come up often enough in my own computing that I think you might benefit as well. I’m trying to find a happy ground between me doing a lot of coding in front of you and me pushing problems at you without discussion. The approach we’ll take today is we’ll write the code together on the whiteboard, each of you contributing a line or comment.
As we write these, we can start to think about the shell as a language. Questions that might arise include:
- How does the shell manage abstraction of data? Is data typed?
- How does the shell manage abstraction? Can I hide a complex operation under a narrow interface?
- How can data be passed around between abstractions?
- How are errors handled?
Code
foozip
/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError) Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec' from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem' from ./coderay:24:in `'
crun
/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError) Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec' from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem' from ./coderay:24:in `'
book
/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError) Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec' from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem' from ./coderay:24:in `'