AR

CMPSC 311 - Introduction to Systems Programming UNIX Essentials

CMPSC 311 - Introduction to Systems Programming UNIX Essentials

The Unix Philosophy

  • Write programs that do one thing and do it well.
  • Write programs to work together.
  • Write programs to handle text streams, because that is a universal interface. -- Doug McIlroy, Unix patriarch
  • More general programming principles:
    • KISS – “Keep it simple, stupid”
    • Modularity – thinking in terms of components
    • Composability – thinking in terms of interacting components
    • Transparency – making inspection and debugging easier

Command Line Interface

  • Command line: Efficient, powerful, scriptable, simple, and reliable.
  • Always works, even if everything else is broken.
  • What is it?
    • Shell program (“bash” on Linux)
    • Interprets built-in commands
    • Runs other programs
    • Runs shell scripts

Standard Filesystem Layout

  • Grouped by type
    • / root directory of the entire filesystem
    • /usr installed software
    • /usr/bin, /usr/lib, …
    • /etc configuration
    • /home users’ own files
    • /dev devices
    • /tmp temporary files

Root (administrator)

  • Files in Unix have owners
  • Users can (usually) only touch their files
  • Root (Super User) can do anything
  • "Becoming root"
    • Administrative privileges: su
    • Temporary privileges (per command): sudo (su “do”)
  • Important principles for administrators:
    • Respect the privacy of others.
    • Think before you type.
    • With great power comes great responsibility.
  • Files
    • Listing: ls
    • Moving/copying: mv
    • Deleting: rm
    • Reading: cat
    • Creating: touch
  • Directories
    • Print working dir (“You are here”): pwd
    • Change dir ("moving around"): cd
    • Creating/removing: mkdir, rmdir
  • Special directory names
    • . means the current directory
    • .. means the parent directory
    • ~ means home

Help!

  • Built-in option to most commands: help
  • Everything else: man
    • Standard layout
    • man operator
    • man ascii
    • man man!
  • Get used to reading these!
  • Keyword search: man -k
  • (Also Google)

Editing Files

  • Unix philosophy again: text is important!
    • Configuration? Text files.
    • Scripting? Text files.
    • Programming? Text files.
  • Vim (and vi)
    • Do one thing and do it well
  • Graphical Editors (i.e. Notepad, gedit)
  • More advanced editors
    • Sublime Text
    • Visual Studio Code

Why Learn A CLI Editor?

  • It’s fun!
  • Debugging systems remotely
  • Complex things break
  • Embrace the command line
  • Vim: Run vimtutor at the command line
  • GVim for Windows, MacVim for Mac
  • Many, many, many features/extensions
  • GNU Nano: Run nano at the command line

Shell Scripting

  • Getting tired of typing the compile command yet?

  • Shell script = list of commands to run

  • Put this in a file called build.sh:

    #!/bin/sh
    make clean
    make
    ./cmpsc311-f21-assign1 < test-input1.txt
    
  • Now just run (or ./build.sh if you gave execution permission first: sudo chmod +x build.sh)

  • Example execution:

    $ sh build.sh