IT 244: Introduction to Linux/Unix
The reading assignment for this week is chapter 5 of Sobell,
I have posted homework 5 here.
It is not due this coming Sunday.
Instead is due on the Sunday of the following week, July 8th.
This is to allow you time this weekend to study for the mid-term.
The mid-term exam for this course will be held on Monday, July 2nd.
It will consist of 25 questions like those on the quizzes.
You will have the entire class period to work on the exam.
60% of the questions will come from the Ungraded Class Quizzes.
The last class before the exam, Thursday, June 28th, will be a review session.
You will only be responsible for the material in the Class Notes for that class
on the exam.
The Mid-term is a closed book exam.
Let's look at the answers to
Syntax of the Command Line
- Options modify the behavior of the command
- Options are usually preceded by one or two dashes, -
- GNU programs frequently have options that are preceded by two dashes, --
- The options in GNU programs are usually words
- The options in other Unix programs are usually a single letter
- When a command uses a single dash, -, before an option ...
- you can usually combine options following the dash
- An example of this is
- Options using two dashes, -- , usually cannot be combined
- In this case, each option must be written separately ...
- and preceded by two dashes
- Sometimes the option can have it's own argument
- Utilities that report the size of files usually do so in bytes
- Such utilities often have a -h, or --human-readable, option
- With this option, the file size will be displayed in kilobytes, megabytes or gigabytes, as appropriate
- Many commands display a help message when run with the --help option
- Most GNU utilities accept this option
- All operations that need to access any hardware on the machine ...
- must do so through the
- The kernel controls access to
- RAM (short term memory)
- disk (long term memory)
- Every manufacturer of a device that works with a computer ...
- must furnish software ...
- that allows the kernel to talk to the device
- This software is called a device driver
- In the very early days of Unix ...
- people interacted the the machine ...
- through a device called a teletype ...
- teletypewriter or teleprinter
- This machine consisted of a keyboard ...
- and a printer
- A user would type a command on the keyboard ...
- and the output of the command ...
- would printed on a continuous flow of paper ...
- that ran through the printer part of the device
- The name of this device was soon shortened to tty
- The printer part of these devices ...
- was soon replaced with a video monitor ...
- Any device that allows you to send text to a computer ...
- and see the resulting output ...
- we usually call a terminal
- But Unix still refers to it as tty
The Unix tty Device Driver
- The device driver for tty ...
- is built into the kernel
- As you type at the command line ...
- the characters you type are collected by the tty device driver
- This program looks at each character as you type ...
- and takes appropriate action
- Most of the time, it places the character in a
- that holds characters until you are done
- But tty device driver responds differently
- to certain special characters
- When the character you type is the backspace ...
- it erases the previous character from the buffer
- When the character is the Control U ...
- the driver erases the buffer from the current insertion point ...
- to the beginning of the line
- The device driver is responsible for all
command line editing
- When the tty device driver gets a newline character ...
- it passes the contents of the buffer to the shell
- Newline is the character you get from hitting Enter on a Windows machine ...
- or Return on a Mac
Parsing the Command Line
- The shell takes the command line and breaks it up into
- Tokens are strings of characters that can be printed ...
- and are separated from each other by spaces
- The act of breaking up text into tokens is called
- Next, the shell looks for the name of the command
- Usually, the command name is the first string on the command line
- The command can be specified by a simple filename
- Or by using a
The PATH System Variable
Running a Program in the Current Directory
- For security reasons, it is never advisable to put the current directory, . ...
- in the PATH list
- Then how do you run a program inside your current directory?
- You can do this using the following construction
- This will always work ...
- regardless of the contents of PATH
Running the Command Entered on the Command Line
Using Computers More Efficiently
- In the late 60s computers were big expensive machines
- They were so expensive that people you used them ...
- were charged for their time
- Writing programs and getting them to work ...
- can take a great deal of time
- You write the program ...
- run it ...
- then try to find the bugs
- Finding can take a great deal of time
- Since only one person could use the computer at one time ...
- the programmer had to take the output ...
- go to his or her office to think about the problem ...
- then wait for their next chance to use the machine
- This made made writing working programs ...
- take a very long time
- It was not very efficient ...
- since if you had a good idea ...
- it would be best to try it out right away ...
- instead of waitig a day or two
- There was a need for a better way ...
- to use these expensive machines
- Computer scientists came up with a better idea
- Why not let many people connect to the same computer ...
- as the same time?
Computers are Fast But People are Slow
- Most of the time you and I spend in front of a machine ...
- is doing one of two things
- Typing or thinking
- Thinking requires nothing of the machine
- and typing make take us several minutes
- but reading what we type takes the machine microseconds
- Some most of the time you and I are using a machine ...
- the computer hardly notices
- The only time we make a significant demand on the machine ...
- is when we run a program
- Computer scientists realized tha if we gave each user ...
- a little bit of time ...
- called a time slice ..
- before moving on to the next person
- people would never notice
- The computer would still be working with one person at a time ...
- but the switching was so fast that people never noticed
- To make this work a new type of operating system was needed
- A multiuser operating system
The Birth of Unix
- Creating such an operating system was not easy ...
- it took special hardware and software
- When the computer switched from one user to another ...
- it had to store the memory allocated to each running process ...
- in a special memory where it could be called back quickly
- If the machine was running a program when the swtich occurred ...
- it had to remember where it was in the program ...
- when the switch occurred
- Because people knew how efficient multiuser operating systems would be ...
- there was a big push to make this happen
- This lead to the creation of a project called Multics
- It was a huge project involving government ...
- industry ...
- and many Universities
- The project was so big and unwieldy ...
- that it was never finished
- Bell Labs was working on Multics ...
- but eventually decided to pull out
- Two very talented programmers ...
- Ken Thompson and Denis Ritchie ...
- decided that Multics was trying to do too much
- They simplified the specification inherited from Multics ...
- made some very clever design decisions ...
- and created Unix
- Computers work with information
- They take information in ...
- and they send information out
- We can think of these flows of information as data streams
- When we run a command that produces some result ...
- the characters that we see ...
- are a flow of information sent to the screen
- This is an output stream
- When we save a file ...
- this a a flow of information from the program
- to the disk
- It is an output stream
- When we type something into a word processor ...
- this is a flow of information ...
- from the keyboard ...
- into the program
- This is an input stream
- When we open a file in a word processor ...
- this is a flow of information from the disk ...
- into the program
- This is also an input stream
- Devices are pieces of electronic equipment ...
- that communicate with the CPU ...
- through data streams
- They do this using device drivers ...
- which are software created by the manufacturer ...
- that allows the device to talk to the CPU ...
- through the kernel
- Most device drivers support two way communication ...
- allowing both input streams ...
- and output streams
- Since the device drivers handle the details ...
- of data streams coming into or out of a device ...
- the kernel can treat input and output to any device ...
- in the same way
- as devices that can provide input streams
- This makes things simple
Unix Devices Are Files
- One of the reasons why Unix is so powerful ...
- is that decisions were made when it was designed ...
- to keep it simple and elegant
- One such decision was how Unix treats devices
- Unix treats a device ...
- the same way it treats file on disk
- You can read both read from ...
- and write to ...
- a file on disk
- You can do the same with devices
- You can read from them ...
- and write to them
- So Unix thinks of everything it can read information from ...
- or send information to ...
- as a file
- If you think of this it makes sense
- The hard drive is a device ...
- that can provide both input ...
- and output
- You can read a file as input ...
- and save information to disk as a file ...
- for output
- Instead of creating some new thing ...
- to represent a device ...
- Unix just thinks of then as a special kind of file
- A device file
- This allows programs that run on Unix ...
- to send output to a device ...
- and take input from a device ...
- in the same way it does for a file
The Monitor and Keyboard
- We tend to think of the computer screen ...
- and the keyboard ...
- as separate devices ...
- since we can buy and replace them separately
- But this is a recent development
- In the 1970 when Unix was created ...
- multiuser operating systems allowed several people ...
- to be connected to one very expensive computer
- This was done by connecting terminals ...
- the combination of a keyboard and a screen ...
- directly to the machine
- The terminal is a device ...
- so it needs a device driver ...
- and that is the tty device driver
- It handles both input and output ...
- to the terminal
- Although we no longer use physical terminals ...
- the tty device drier is still used ...
- to talk to the keyboard and screen ...
- connected to a Unix machine
ssh and Pseudo-terminals
Standard Input, Standard Output and Standard Error
- Every Unix process always has access to three different data streams
- Standard Input
- Standard Output
- Standard Error
- The programmer who creates the program running in the process ...
- does not have to use these streams ...
- but there are always given to a process
- Standard input
is where the program gets input ...
- unless the programmer decides to use another source
- By default, standard input is the keyboard
- Standard output
is where the program prints the results ...
- if it is not told specifically where to send it
- By default, standard output is the screen
- Standard error
is where the program sends error messages
- By default, standard error is the same as standard output ...
- the screen
- The end point of each of these data streams ...
- can be changed by the user ...
- using a Unix feature called
The Keyboard and Screen as Standard Input and Standard Output
Redirecting Standard Output
- To redirect output use the greater than symbol, >
- followed by a filename
- This tells Unix to send the output from the command ...
- to the file or device that appears after the symbol
- The format for output redirection is
COMMAND [ARGUMENTS] > FILENAME
- For example, to save a list of everyone currently logged on, you could use
$ who > current_logins.txt
$ cat current_logins.txt
bmt11989 pts/1 2011-10-02 16:43 (c-24-147-18-10.hsd1.ma.comcast.net)
vtran pts/0 2012-09-26 17:34 (c-76-119-98-173.hsd1.ma.comcast.net)
abutawha pts/1 2012-09-26 17:36 (188.8.131.52)
ghoffman pts/2 2012-09-26 18:19 (dsl092-066-161.bos1.dsl.speakeasy.net)
- If you redirect to a file that does not exist ...
- the file will be created for you
- If you redirect to a file that exists ...
- the contents of that file will be replaced ...
- by the output of the command
Redirecting Standard Input
- When redirecting standard output, we were sending output ...
- to something other than the screen
- When we redirect standard input, we take input ...
- from something other than the keyboard
- To do this, we use the less than symbol, <
- Here is the format
COMMAND [ARGUMENTS] < FILENAME
- repeat.sh is a shell script ...
- that repeats the the text the user enters
Enter line 1: 1
Enter line 2: 2
Enter line 3: 3
Enter line 4: 4
Enter line 5: 5
- But I can also take input from a file ...
- by redirecting standard input
$ cat five_lines.txt
$ ./repeat.sh < five_lines.txt
Redirecting Standard Output Can Destroy a File
- If you redirect standard output to a file that already exists ...
- you will overwrite the contents of that file
- You will replace the original contents of the file ...
- with the output of the new command
- There is a "noclobber" option in Bash to prevent this from happening
- But it is best to simply be careful about the file ...
- to which you redirect standard output
Adding Output to an Existing File
- Sometimes a program will do something useful ...
- but produce output you don't want
- For situations like this, Unix provides /dev/null
- Any output you send to /dev/null will disappear
- It will never appear on the screen
- If you redirect input to come from /dev/null ...
- the command will receive an empty string
- /dev/null is most useful when dealing with error messages
- Since error message normally go to the screen
- they will be mixed up with the regular output
- Redirecting standard error to /dev/null ...
- will prevent this from happening
- You can use /dev/null to test your scripts ...
- for errors
- The following script has a command that will cause an error
$ cat script_with_error.sh
# this script has an error
cd XXXXXX # line with error
ls -l /
- But if you run it, you probably won't spot the error ...
- because it scrolls off the top of the screen
./script_with_error.sh: line 3: cd: XXXXXX: No such file or directory
drwxr-xr-x 2 root root 4096 Oct 15 06:45 bin
drwxr-xr-x 3 root root 4096 Oct 15 06:45 boot
-rw------- 1 root root 75390976 Oct 8 00:06 core
drwxr-xr-x 102 root root 0 Oct 6 07:06 courses
drwxr-xr-x 46 root root 0 Oct 6 07:06 data
drwxr-xr-x 15 root root 4160 Oct 6 07:06 dev
drwxr-xr-x 119 root root 12288 Oct 15 13:32 etc
drwxr-xr-x 8 root root 0 Oct 6 07:06 groups
drwxr-xr-x 1943 root root 0 Oct 14 14:42 home
drwxr-xr-x 3 root root 4096 Aug 25 16:18 home.ORIG
lrwxrwxrwx 1 root root 33 Sep 29 06:40 initrd.img -> boot/initrd.img-3.16.0-50-generic
lrwxrwxrwx 1 root root 33 Sep 11 06:49 initrd.img.old -> boot/initrd.img-3.16.0-49-generic
drwxr-xr-x 21 root root 4096 Aug 26 22:46 lib
drwxr-xr-x 2 root root 4096 Aug 26 22:46 lib32
drwxr-xr-x 2 root root 4096 Aug 26 06:50 lib64
drwxr-xr-x 2 root root 4096 Aug 26 22:46 libx32
drwx------ 2 root root 16384 Aug 25 16:11 lost+found
drwxr-xr-x 3 root root 4096 Aug 25 16:12 media
drwxr-xr-x 2 root root 4096 Apr 10 2014 mnt
drwxr-xr-x 11 root root 0 Oct 6 07:06 nobackup
drwxr-xr-x 2 root root 4096 Feb 18 2015 opt
dr-xr-xr-x 522 root root 0 Oct 6 07:06 proc
drwx------ 4 root root 4096 Sep 9 14:22 root
drwxr-xr-x 19 root root 1020 Oct 15 13:31 run
drwxr-xr-x 2 root root 12288 Oct 15 06:45 sbin
drwxr-xr-x 174 root root 0 Oct 6 07:06 sources
drwxr-xr-x 5 root root 0 Oct 6 07:06 spool
drwxr-xr-x 2 root root 4096 Feb 18 2015 srv
dr-xr-xr-x 13 root root 0 Oct 6 22:29 sys
drwxrwxrwt 40 root root 4096 Oct 15 13:32 tmp
drwxr-xr-x 2 root root 4096 Sep 21 16:28 TMP
drwxr-xr-x 269 root root 0 Oct 6 07:06 tools
drwxr-sr-x 2 root root 4096 Aug 26 23:30 users
drwxr-xr-x 12 root root 4096 Aug 26 22:46 usr
drwxr-xr-x 12 root root 4096 Oct 15 05:45 var
lrwxrwxrwx 1 root root 30 Sep 29 06:40 vmlinuz -> boot/vmlinuz-3.16.0-50-generic
lrwxrwxrwx 1 root root 30 Sep 11 06:49 vmlinuz.old -> boot/vmlinuz-3.16.0-49-generic
- But if you run the script and redirect standard output to /dev/null ...
- all you will see will be the error messages
$ ./script_with_error.sh > /dev/null
./script_with_error.sh: line 3: cd: XXXXXX: No such file or directory
- I use this trick in my testing scripts ...
- to check your Class Exercises
- You should use this trick to test all your scripts