Sometimes we will be directing you to use a command window or terminal to compile and run C# programs. [1]
Reasons to use the command line:
Enter
.The most direct way to access the command line (often called a command shell):
On Windows, to have easy access to Mono tools, press the Windows key (lower left or lower right on the keyboard) and type Mono, then select the Mono Command Prompt and press Enter.
Alternately, if you do not need Mono tools for sure, the general way to get a command window is to press the Windows key and R (lower or upper case) together, then type cmd and press the Enter or Return key – this brings up the basic command processing program, cmd.
On a Mac just open a Terminal window – this is fine for the Mono SDK commands.
Mac OSX, Linux and other Unix variants work basically the same once you get to a terminal, so we will only distinguish Windows and Mac OS-X.
The command shell waits for you to type in a command (a short name that the shell recognizes) followed by 0 or more parameters separated by spaces (and Enter). Note that if a parameter contains spaces you must surround the parameter value with matching single or double quotes – you’ll see an example later.
We are going to mention some of the simplest uses of basic commands. More advanced documentation would include more options.
Some commands are common between the Windows and Mac shells:
stands for Change Directory – you can use this command to change the current working directory to a different one.
You can use this command to change to directories where your C# program source files are located, if different from the initial directory.
On Windows, suppose you created a directory C:\COMP170\hello; to
change to that, type cd C:\COMP170\hello and press Enter – the shell
prompt will change to show this new directory location and programs like
mcs and mono will be able to access files there, directly
by name. If the Comp170 directory was you current directory, it would
be shorter to use relative paths and just cd hello
. Remember if
you want a different Windows drive, you must first use a
drive change command.
On a Mac you can also use either an absolute or a relative path with cd
.
If you included a space in one or more of the directory names, for example C:\COMP 170\hello (a space between COMP and 170) you should enclose that part(s) in quotes like so: cd C:\”COMP 170”\hello
Mac Note: if you type just cd and press Enter you will change back to your home directory. There is also a shorthand name for your home directory in command paths: tilde (~), often shifted backquote on the keyboard. Sorry, no such thing with Windows.
stands for make directory – you can use mkdir to create a new empty directory in the current directory.
For example, on a Mac with current directory /Users/YourLogin, type mkdir hello and press Enter – this will create a new directory /Users/yourLogin/hello if it did not exist before; you can now create a C# source file in that directory and enter cd hello in the command shell.
An optional Windows abbreviation is md.
removes an empty directory that you give as parameter, e.g.,
rmdir hello
With Mono installed (and for Windows, with a Mono command window), the programs associated with Mono can be used:
Other useful commands, with different names for Windows and Mac, are listed next by generic function, with general Windows syntax first and Mac second, and then often examples in the same order:
Display the contents of a text file in the command window. The Unix/Mac name origin: a more complicated use of cat is to concatenate files.
type textFileNamecat textFileNametype my_program.cscat my_program.cs
Make a copy of a file. Caution: If the second file already exists, you wipe out the original contents!
copy originalFile copyNamecp originalFile copyNamecopy prog.cs prog_bak.cscp prog.cs prog_bak.cs
Erase or remove a file:
erase fileToKillrm fileToKillerase poorAttempt.csrm poorAttempt.cs
Another Windows equivalent is del
(short for delete).
Help on a command:
help commandName;commandName –help
Note the double dash above: This sometimes works for concise help on a Mac while you can generally get immensely detailed help overload on a Mac from
man commandName
This is not a subject of this course, but commands can be combined into script files.
Scripting languages are in fact whole new specialized programming languages, that include many of the types of programming statements found in C#.
Copying or pasting with a Mac is is the same with a terminal as in other editing: Use the same Apple Command key with C or P, and you can select with the mouse.
In Windows it is more complicated to use a command window: You can paste into the current command line by right clicking on the Command Window Title bar, and select edit and then paste.
By default a Windows command window is not sensitive to the mouse. You can change so that it is sensitive for select and copy: Right click in the title bar, select defaults, and make sure the check boxes under edit options are all checked. (The last two are explained in the next section.) Click OK. Then you can select with mouse and press Enter for the selection to be remembered in the copy buffer.
Both Windows and Mac (with the right options selected, like the Windows check boxes in the last section), allow you to reduce typing:
You can bring back a previous command for the history of commands that are automatically remembered: Use the up and down arrows. This makes it very easy to run the same command again, or to make slight edits.
Both Windows and OS-X can see what files are in any directory being referred to. If you just start to type a file or folder name and then press the Tab key, both Windows and OS-X will do file completion to complete the name if there is no ambiguity. If there is ambiguity, they work differently:
[1] | Thanks to Dr. Robert Yacobellis for elaborations to this section. |