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/usrinstalled software/usr/bin,/usr/lib, …/etcconfiguration/homeusers’ own files/devdevices/tmptemporary 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”)
- Administrative privileges:
- Important principles for administrators:
- Respect the privacy of others.
- Think before you type.
- With great power comes great responsibility.
Navigating the CLI
- Files
- Listing:
ls - Moving/copying:
mv - Deleting:
rm - Reading:
cat - Creating:
touch
- Listing:
- Directories
- Print working dir (“You are here”):
pwd - Change dir ("moving around"):
cd - Creating/removing:
mkdir,rmdir
- Print working dir (“You are here”):
- 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
manoperatorman asciiman 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
vimtutorat the command line - GVim for Windows, MacVim for Mac
- Many, many, many features/extensions
- GNU Nano: Run
nanoat 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.txtNow just run (or
./build.shif you gave execution permission first:sudo chmod +x build.sh)Example execution:
$ sh build.sh