Using the command line "shell"
While you can write code in an IDE (like Visual Studio or Eclipse), for this course you should try to run your programs from the command line (or "shell"). The shell is a program that takes text input commands, and produces text output.
If you haven't used a shell before, it may seem old fashioned, obscure, and slow. That's just because you're not used to it. Once you learn it, you'll see that you can work much faster and more accurately than you could otherwise.
How to open a shell
- DOS: Start menu, Run, "cmd"
- OSX: Terminal.app (in /Applications/Utilities) or xterm if you have X11
- Linux: use xterm or something similar
What directory am I in (what's my "working directory")?
When working in a shell, you are always in some directory. You can find out which one by doing the following:
- DOS: look at the command prompt, or use the command chdir
- bash: pwd (think: print working directory)
The shell knows how to find some built-in commands, but to run other commands (like your own programs) you need to specify the directory, relative to where you currently are. So it's good to know where you are.
How do I move to another directory?
cd [directory name]For example: cd project5 or cd project5/debug/tests (on bash) or cd project5\debug\tests (on DOS).
The parent directory is always .. (two periods), and the current directory is always . (one period). The character to divide the names of two directories is backslash (\) on DOS, forward slash (/) on bash.
Note that spaces in directory names can cause you problems -- because a space is how the shell determines the end of part of a command. So going to a directory with a space will require you to surround the directory name in quotes, e.g. cd "project 5/debug/test random", or escape the spaces with backslashes, e.g. cd project\ 5/debug/test\ random. Using tab-completion (see further down this page) can help do this automatically for you.
What is in the current directory?
- DOS: dir (think: directory listing)
- bash: ls (think: list)
Where are my files?
- wherever you left them
- Visual Studio (and other IDEs) often put compiled files in "debug" or "release" (or similarly-named) subdirectories under the project directory
How do I compile from the shell?
- DOS (assuming you have Visual Studio installed):
- cl /EHsc *.cpp (assuming you want to compile all cpp files in the current directory)
- if this fails, run the following command, and try again: c:\Program Files\Microsoft Visual Studio 8\Common7\Tools\vsvars32.bat (its location will depend on where Visual Studio is installed)
- produces [something].exe, where [something] is the name of the file that contained main()
- bash (assuming you have g++ installed):
- g++ *.cpp
- produces "a.out", a program (you can rename it)
How do i run my program from the shell?
- go to the directory holding the .exe you want to run
- type in the name of the program, hit return
- ctrl-z [Enter] to signify end of file
- go to the directory holding the program you want to run
- type ./[name] where [name] is the name of the program (e.g. a.out)
- if there's an error, you may have to chmod +x [name] -- tell bash that the file is executable
- ctrl-d to signify end of file
File input and output redirection
When running a program from the shell, you need to provide it some input, and it will produce output. You might be tempted to just type in the input on the keyboard, and then look at the output. Those are wrong. Let me explain.
Typing input is
errer errir error-prone. It also
takes a long time.
Looking at output is also error-prone. You may not even be able see some problems, like an extra space at the end of a line.
- Instead of typing input to your program, redirect it from a file. This saves you time (over multiple tests), allows you to be more precise and create larger test cases, it allows you to repeat and automate your testing.
- Instead of looking at the output of your program, redirect it to a file. This also saves you time, and allows you to capture exactly the output the program produced, which allows you to compare it precisely against a known-correct output file (using diff).
How to redirect input from a file
First, you need a file containing the input you want. It should be a plain-text file (e.g. you could create it with a text editor, such as Notepad on Windows, TextEdit on OSX, your chosen IDE, or a general-purpose text editor like VIM or Emacs). Let's assume the file is called "input".
Second, you run the program from the shell. After the program's name, put the text < input. So something like this:
programName < input (on DOS) ./programName < input (on bash)
What's going on here? The shell sees that you want to run a program. It also sees that you have <, which indicates input redirection. It uses the filename you provide, opens the file, and connects it to the standard input of the running program. Very easy to do!
How to redirect output to a file
Let's assume that you want to put the output in a file called "output". Run the program from the shell. After the program's name, put the text "> output". So something like this:
programName > output (on DOS) ./programName > output (on bash)
Now, you have exactly the text that the program produced. Why is this useful? See below, the section on diff.
Redirecting input and output
You can (and should) combine input and output redirection, e.g.:
programName < input > output (on DOS) ./programName < input > output (on bash)
Diff (short for "difference") is a type of program that compares two files and shows their differences. Diff saves you time and simplifies your job of determining if two output files are the same. It will show you only the differences, allowing you to focus on just those.
In this class, you should use a diff program on your own computer to compare your output with the known-correct output (e.g. the output produced by a solution provided by your professor).
There are many programs that provide diff:
- Many IDEs provide some way of doing a diff on two files.
- On Windows, you can use WinMerge.
- On Linux and OSX, you probably have access to the program "diff", which you can call like this:
diff file1 file2
- The VIM and Emacs editors have built-in diff modes that are very useful and allow editing during comparison.
Most diffs allow stricter or looser comparisons. For example, you might specify whether differences in spacing are significant. For this course, you should select the strictest options.
Miscellaneous other things about using the shell
- Tab completion: save yourself time by typing only part of the name of a file or directory, and then typing "tab" to get the shell to complete the word for you. Sometimes typing tab twice will list the options you have (if there are multiple).
- Command history: use the up/down arrows to recall recent commands.
- Extra output mode on professor's programs: most solutions your professor provides have an "extra output mode" which you can enable by giving (any) command-line argument.
- Creating a directory: mkdir [name]