FEEG6002 - Applied Programming 4 - Terminal & Input/Output Richard Boardman, Sam Sinayoko 2016-10-31
Outline Learning outcomes Introduction The Terminal / Command Prompt Program I/O in C Program I/O in terminal Summary Appendix References 2/50
Outline Learning outcomes Introduction The Terminal / Command Prompt Program I/O in C Program I/O in terminal Summary Appendix References 3/50
Learning outcomes At the end of this lecture you should be able to I Remember and use the basic commands in the terminal I
I
I I I
I
standard input (STDIN) standard output (STDOUT) standard error (STDERR)
Redirect the I/O streams in the terminal I I I
I
cd, ls, cat, mv, cp, rm
Define the 3 key Input/Output (I/O) streams
redirect output to a file receive intput from a file send error messages to a file
Redirect the I/O streams in C programs I
fprintf, putchar, getchar
4/50
Outline Learning outcomes Introduction The Terminal / Command Prompt Program I/O in C Program I/O in terminal Summary Appendix References 5/50
Introduction What is it? A non-graphical way of working with a computer, via screen and keyboard. I An interactive programa in which: I I I
I I
Python and Matlab do this for scientific programming; A program for interacting with the operating system I I
I
a
the user types a command and presses return the command is evaluated and the results are displayed the console waits for the next command
moving to and displaying the contents of a directory copying / deleting / renaming files &c.
Can be used non-interactively using batch files or scripts to automate certain tasks.
this type of program can be called a REPL (read-eval-print loop) 6/50
Introduction Why do we need this? I
I
Writing a graphical user interface (GUI) is not easy and often adds unnecessary complexity. The user must still be able to modify the behaviour of the program I I
I I
GUIs are hard to automate Shells make it easy to combine different programs. The Unix philosphy:a I I I
a
redirect program output to a file (traditionally "Save" in our GUI) take the program input from a file ("Open" or "Load")
write programs that do one things and do it well; write programs to work together; and write programs to handle text streams (universal interface)
adapted from Doug McIlroy, and inventor of the Unix pipe. 7/50
Introduction
Terminology I I
On Windows, it is called the command prompt (cmd.exe) On Linux / OS X, it is called a terminal I
I I I
Linux: xterm, gnome-terminal, konsole (to name a handful) on Linux OS X: Terminal.app (in Applications folder) The terminal is a GUI app that runs the shell. Popular shells include sh, bash and zsh.
8/50
Windows Command Prompt How to start I
Start > Run > and type "cmd.exe"
I
Start > All Programs > Accessories > Command Prompt
Screenshot
9/50
OS X terminal How to start Applications > Utilities > Terminal
Screenshot
10/50
Linux terminal How to start I a
Applications > System Tools > Terminal (or Alt + F2)a
This may vary depending on the environment
Screenshot
11/50
Outline Learning outcomes Introduction The Terminal / Command Prompt Program I/O in C Program I/O in terminal Summary Appendix References 12/50
The Terminal / Command Prompt
File system (cd, pwd) I
On Linux/OS X, the path to a file is of the form /home/rpb/file.txt
I
On Windows, the path starts with the drive C:\Users\rpb\file.txt
Command display current directory change directory move to parent directory
Linux / OS X pwd cd /path/to/dir cd ..
Windows chdir cd \path\to\dir idem
13/50
The Terminal / Command Prompt
Absolute paths Absolute paths give the full path to a file or directory: I
Windows: C:\path\to\directory\file.txt
I
Linux/OS X: /path/to/directory/file.txt
14/50
The Terminal / Command Prompt Relative paths Relative paths are resolved relative to the current directory. Assuming the current directory is path in the above absolute path example, the relative path to file.txt is I
Windows: to\directory\file.txt
I
Linux/OS X: to/directory/file.txt
I
The dot shortcuts . (single dot) .. (two dots) ../.. ../../..
current directory one level up (parent directory) two levels up three levels up &c. . . 15/50
The Terminal / Command Prompt
Files and Directories Command list directory contents delete file.txt copy file1.txt to file2.txt move (rename) file1 to file2 create directory
Linux / OS X ls rm file cp file1 file2 mv file1 file2 mkdir
Windows dir del copy move idem
16/50
The Terminal / Command Prompt Useful commands Display command - contents of file to screen - long file one page at a time - "Hello World!" on screen
Linux / OS X cat file more echo Hello World!
Windows type idem idem
Command options Most commands take options to modify their behaviour. On Linux / OSX, options usually are of the form -x or --xyz ls -l # display one file per line with more info ls -t # sort files by time ls -lt # combine the two
On Windows, options usually take the form /x 17/50
The Terminal / Command Prompt
Getting help Command displays help summary for cmd display (reference) manual for cmd
Linux cmd --help man cmd
Windows cmd /? help cmd
18/50
Key Tips The TAB key The TAB key completes your command. This is especially useful for very long command or directory names. Use your TAB key!
Wildcards and Comments Wildcards can be used to change the behaviour of many commands. ? * #
matches any character matches any string comment: everything after # is ignored
Examples: ls *.c ls feeg6002*.c
# matches all c files # all c files starting with feeg6002 19/50
Compiling and running programs
Compiling gcc -Wall -ansi -pedantic cmd.c -o cmd
# compile
Running Need to prepend ./ to run a command on Linux / OS X. Not needed on Windows. Note also that programs do not generally end with .exe on Linux / OS X.
20/50
Compiling and running programs (continued)
Linux/OS X syntax ./cmd
"./" is needed because only programs listed in the PATH environment variable can be executed. Usually, the current directory is not in there, so the absolute path to the file must be given. A shortcut for that is "./cmd"
Windows syntax cmd.exe
21/50
Outline Learning outcomes Introduction The Terminal / Command Prompt Program I/O in C Program I/O in terminal Summary Appendix References 22/50
Standard streams
Programs I/O are typically done via streams. Linux, OS X and Windows define the following standard streams. Standard stream Input Output Error
Abbr. stdin stdout stderr
File descriptor O 1 2
Connected to keyboard screen screen
23/50
Using standard streams in C programs
Key variables and functions in stdio.h Built-in library stdio defines I
streams stdin, stdout and stderr
I
EOF : integer corresponding to End Of File.
I
function printf: prints to stdout
I
function fprintf: can print to stdout or stderr:
fprintf(stdout, "Print to stdout like printf\n"); fprintf(stderr, "ERROR: don’t panic!\n");
24/50
Using standard streams in C programs (continued)
Key variables and functions in stdio.h I
function getchar: get one character from stdin
I
function putchar: send one character to stdout
int c; /* NOTE: getchar() returns an int */ c = getchar(); /* get character from STDIN */ putchar(c) /* send character to STDOUT */
25/50
Using standard streams in C programs fprintf #include int main() { /* The usual way */ printf("printf says hello.\n"); /* Equivalent to this */ fprintf(stdout, "fprintf says hello.\n"); /* Exercise: what if we use stderr instead? */ return 0; } printf says hello. fprintf says hello.
26/50
Using standard streams in C programs
getchar and putchar: the parrot program Create program parrot.c #include int main() { int c; while ((c = getchar()) != EOF) { putchar(c); } return 0; }
27/50
Using standard streams in C programs stderr: the parrot2 program Use stderr to display additional information. Save program to parrot2.c #include int main() { int c; fprintf(stderr, "Enter some characters (Ctrl-D quits)\n"); while ((c = getchar()) != EOF) { putchar(c); } fprintf(stderr, "Goodbye\n"); return 0; }
28/50
Outline Learning outcomes Introduction The Terminal / Command Prompt Program I/O in C Program I/O in terminal Summary Appendix References 29/50
Redirecting stdout to a file
Syntax cmd > output.txt cmd 1> output.txt
Example 1.1 # add some numbers to file numbers.txt echo 0 1 2 3 4 > numbers.txt # check that it worked cat numbers.txt 0 1 2 3 4
30/50
Appending output to file
Syntax cmd >> output.txt
Example 1.2 echo 5 6 7 8 9 >> numbers.txt cat numbers.txt 0 1 2 3 4 5 6 7 8 9
31/50
Compile parrot and parrot2
gcc -Wall -pedantic -ansi -o parrot parrot.c gcc -Wall -pedantic -ansi -o parrot2 parrot2.c
Remark The examples in the rest of the presentation use our parrot and parrot2 programs. The parrot program behaves in a similar way to the cat program on Linux/OS X or the type program on Windows: the ./parrot command can also be replaced by cat (or type) without breaking the example.
32/50
Redirecting stdin
Syntax cmd < input.txt cmd output.txt
Example 3 ./parrot < digits.txt > output.txt cat output.txt 0 1 2 3 4 5 6 7 8 9
34/50
Understanding stderr Syntax cmd 2> errors.txt cmd 2> /dev/null
Example 4.1 Note that the extra informations sent to stderr do not appear in output.txt. This is nice: they give feedback to the user but don’t pollute the output file which remains very regular (numbers only) ./parrot2 < digits.txt > output.txt cat output.txt 0 1 2 3 4 5 6 7 8 9 35/50
Redirecting stderr
Example 4.2 We can still redirect stderr to a file if we want to keep track of the messages. ./parrot2 < digits.txt > output.txt 2> digits.log cat digits.log Enter some characters (Ctrl-D to quit) Goodbye
36/50
Redirecting stderr
Best practise I
Programs should by default run quietly and output only the results they need. This is to make the result as regular as possible so it can be combined easily with other programs.
I
If additional information is sent to stderr, they do not pollute the results in stdout (see previous slide).
I
Standard error can be silenced by redirecting to a file (see previous slide) or to /dev/null
I
/dev/null is a special file that destroys what it receives.
37/50
Pipes Pipes send the STDOUT of the first command to the STDIN of the second command cmd1 | cmd2
Makes it easy to chain commands.
Example These two commands are equivalent cat numbers.txt | ./parrot ./parrot < numbers.txt 0 5 0 5
1 6 1 6
2 7 2 7
3 8 3 8
4 9 4 9 38/50
Creating input stream on the fly Use the echo command and a pipe.
Simple input strings echo Hello | ./parrot
Hello
Complex input strings Use printf or echo -e to interpret escape characters, and a pipe. printf "X\tY\n1.0\t2.0" | ./parrot # can replace "printf" with "echo -e" X 1.0
Y 2.0 39/50
Exercise I
(a) Create a program encrypt that prints an encrypted version of stdin to stdout by adding 1 to each character.
echo "secret" | ./encrypt > secret.txt cat secret.txt tfdsfu I
(b) Create a program decrypt that decrypts characters from stdin and and prints to stdout.
# Use intermediate file ./decrypt < secret.txt secret 40/50
Exercise
I
(c) Check that you can also both programs directly with pipes
echo "secret" | ./encrypt | ./decrypt
secret
41/50
Outline Learning outcomes Introduction The Terminal / Command Prompt Program I/O in C Program I/O in terminal Summary Appendix References 42/50
Summary The terminal I
The terminal (on Linux / OS X) and the command prompt (on Windows) present a non-graphical user interface for interacting with the operating system.
I
The key commands (on Linux / OS X) to remember are cd, pwd, ls, cat, echo, cp, mv and rm.
I
The Windows equivalent are cd, chdir, dir, type, echo, copy, move and del.
I
Alway use TAB to complete long names: no need to type everything
I
Run programs with ./program
I
Use wildcards * and ? 43/50
Summary
I/O in C I
Write to stdout and stderr with fprintf;
I
Read characters from stdin with getchar;
I
Write characters to stdout with putchar
I/O in terminal I
Redirections I I I
I
STDOUT with > (overwrite) and (append) STDIN with < STDERR with 2>
Use pipes | to combine multiple programs.
44/50
Outline Learning outcomes Introduction The Terminal / Command Prompt Program I/O in C Program I/O in terminal Summary Appendix References 45/50
Appendix: here string
An alternative to echo and printf is to use a here string. This works in the bash and zsh shells in Linux / OS X.
Here string On the fly single line input file ./parrot