teaching machines

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 make 2016_01_27. But tomorrow if I run the exact same command, it’ll make 2016_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:

  1. How does the shell manage abstraction of data? Is data typed?
  2. How does the shell manage abstraction? Can I hide a complex operation under a narrow interface?
  3. How can data be passed around between abstractions?
  4. How are errors handled?

Code

foozip

#!/bin/sh

zip -r $1.zip $1

crun

#!/usr/bin/env zsh

src=$1
exe=${1:r:t}

if [[ $src -nt $exe ]]; then
  gcc -o $exe $src
fi

./$exe

book

#!/usr/bin/env zsh

echo $(pwd) > $HOME/.bm

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *