<<

Tutorial & Environment Setup 1 Walkthrough

1. Open your terminal application (Mac and Linux) or download and install puTTY (Windows). Linux and Mac users, go to step 2; Windows users should follow along with the CUIT instructions and then return to step

2. SSH into CUNIX by typing the “ssh @cunix.columbia.edu” (without the quotes), where is your UNI (I would type ssh [email protected]. You will be prompted for your password and a message about authenticity of the (type ‘’) 3. You are now in a UNIX terminal running a shell. Congratulations!

4. a. See what directory you’re in by entering - this should be your home directory b. a new directory using the command, mkdir cprog c. (change directory) into that directory with cd cprog d. run pwd again to make sure you made it into the new directory e. run (you won’t see much); .txt and ls again f. move back to your home directory, cd .. g. get rid of the test file with cprog/test.txt 5. Get exploring and work on the problems in section 2.

6. When you’re all done, you can logout of the session.

Contents

1 Walkthrough 1

2 Practice Problems & Challenges2

3 What is UNIX? 2

4 The TTY/Shell (bash) and the Command Prompt2 4.1 bash...... 2 4.2 The Prompt...... 3

5 man 3

6 ssh 3

7 Environment Variables, Editors, and the $PATH 3 7.1 Paths...... 3 7.2 Environment Variables and the PATH ...... 4 7.3 Editors...... 4

8 Useful Commands 4

9 Redirection and stdio 4

1 2 Practice Problems & Challenges

Write up and submit your answers to challenges 1 and 2; continue to work on 3, 4, and 5 throughout the semester.

1. a. What are . and ..? Why are they useful? b. Why doesn’t root have a .. directory? 2. a. a bash script called ‘backup’ to backup a file (ie. make a new file with the same content but a new name) that takes the file’s name and the new backup name as arguments. try it out (hint: what permissions does it need to run?) b. What does #!/bin/bash mean? what is ./ doing? c. Why doesn’t our script need an extension? what might it be if we wanted to give it one? d. Make the script handle user error

3. a. Customize the command prompt, add some color, show the pwd, go nuts and add anything you find useful. b. Change the prompt based on the success/failure of the last command; show how long the command took to run. c. Add some command aliases for frequent commands. d. How would you make these changes permanent? 4. Setup ssh keys so you don’t have to type in your password every you ssh. 5. Pick a version control system and try to start using it - here’s a simple git tutorial to tickle your fancy: git- tutorial.pdf

3 What is UNIX?

UNIX is an first written Bell Labs 1970 (the epoch). It is primarily written in C and another ( abstract) language called Assembly and it is the basis for Mac OS X, Linux, Chrome OS, Android, iOS, and even .

4 The TTY/Shell (bash) and the Command Prompt

When multi-user and multi-threaded computing was emerging (computers only ran a single program at a time for a single user before that) computer setups consisted of a single central computer and several terminals (the screen and keyboard part – no mouse) wired to it. These terminals provided a command prompt to let users run programs, just not graphical programs that you’re used to.

4.1 bash The Bourne Again SHell (bash) is one of those programs that run in a terminal – it’s called a ?shell’ program and is the default shell on OS X and Linux variants. What this means is that it runs as a command interpreter, allowing you to enter commands that run in the terminal. When you open up or ssh into (that’s in the next section) a terminal, it will already be running a shell program – you can check this out by running the command and looking for the ?bash’ process. Shell programs also allow you to write scripts (programs) using the commands (bash uses a C-like programming language) *some other shell options include the popular zsh, as well as ksh, the (sh) for bash is named, tcsh, and its predecessor, the C-shell (csh).

4.2 The Prompt The command-line prompt is usually signified by a $, #, or > and maybe some other like the current user, directory/folder, or the computer hostname – don’t worry too much about that info, it’s just there to you get your bearings as you work. The prompt waits for commands from you that it can process and run – these usually consist of C or Assembly programs which you run by entering the command name (like ps) and hitting ‘ENTER’. Many programs will take arguments, or extra data, and some optional flags – take the ls (list) command: running

2 ls with no arguments will list the files and directories that are sitting inside the current directory; if you want to see what’s in some other directory, you could run the command with that directory’s name, ls SomeOtherFolder/; ls can also take some optional flags like -a and -l (ls -a, ls -l, or even ls -al) which will show hidden files and extended information, respectively. *usually, ‘ctrl-c’ will interrupt and terminate the current process, so if you get stuck try it out. ‘ctrl-d’ will also simulate EOF, or end-of-file, which can be useful too. *if you’re on a Mac, the built-in ?terminal’ application is a terminal emulator that runs bash by default; if you’re on windows, the Command Prompt has a similar function, but different commands – consider getting a UNIX terminal emulator like puTTY if you’d like to be able to work locally.

5 man

The first UNIX program you will come to love is man, short for manual. It is your guidebook and it will reveal the wonders of the UNIX and C universes to you. It is the command manual and most programs and commands will have a ?’ – that means you can run man ls or man ps to find out what the ls or ps programs do, how you use them, and what are valid arguments and flags. Another useful tool is a program’s own help command: many programs are willing to offer you some help if you’ll just ask. You can usually do this by adding a -help, -h, --help, or --h flag to the command. Most programs will also print their usage instructions for you if you use them wrong, this can be just as helpful. *by default, man uses a program called less to display pages - you can navigate using the up and down arrow keys and typing q to . *try running man man.

6 ssh

Many computers are set up to allow remote access – often this is in the form of a terminal that you can connect to over a network and run commands on. One of these is called CUNIX (Columbia UNIX) and is set up to provide you with a standard UNIX environment to work in over the course of this semester. The way you will connect to a virtual terminal on CUNIX is by using a program called ?Secure SHell’, or ssh. Use man to figure out how to use the ssh command to access cunix.columbia.edu (the teacher will provide you with your user name). *logout will log you out of the remote session. *if you’re on PC, you will need to get a program called puTTY to ssh into CUNIX.

7 Environment Variables, Editors, and the $PATH 7.1 File Paths UNIX filesystems are structured like a - there is a ?root’ directory, or folder, in which files and other directories reside; those directories can contain their own files and subdirectories and so on – you’re probably used to this structure by now, but maybe not the fact that there is a single ?root’ directory (/) in which everything else sits. In UNIX, everything is a file (even directories) and file paths are built up using forward-slashes, / – paths can either be absolute (starting from root: /users/me) or relative (starting from where you are now: just /me if you’re already in the users directory. Note that every directory (except for root) has ‘.’ and ‘..’ subdirectories (see Practice Problem 1). *check out the pwd and cd commands.

7.2 Environment Variables and the PATH In your shell, there are some useful pieces of data hanging around called environment variables. In bash, these tend to be all upper-case and can be accessed with a $. One of these environment variables is called ?PATH’ – try running $PATH to see yours. The PATH is a list of filepaths, separated by colons, where executables (programs) can be found. This PATH is why you can run commands like ls or echo without having to specify the full path from root to where they are (/bin/ls). You can (and probably will) add your own environment variables and add directories to the path to help make your life a little easier. There are also two useful shortcuts: tilde-expansion, which expands the to your home directory (on a shared environment like CUNIX, you won’t have access to the whole filesystem, and other users won’t have access to your

3 home directory – it’s where you’ll do most of your work); and the ‘*’ wildcard, a regular expression that assumes all possible expansions (/users/myhome/* would mean everything in the myhome directory and ls /*.txt would list all the files in my home directory with the extension ‘.txt’). Pretty cool! *some other environment variables to check out are PWD, PS1, and EDITOR.

7.3 Editors Now that we’re in our shell, we’ll want to create and edit some files (after all, we want to write programs and those are just files). To do this we’ll need a text editor – there are two popular text editors in the UNIX environment: Emacs and Vim. They power users and programmers in UNIX world into two camps (that constantly make fun of each other). The choice between them largely boils down to personal taste. I recommend you pick one of those as your editor and start using it, learning tips and tricks as you go. You can start with the built-in tutorials: run vimtutor at the command prompt for Vim, and click on ?Emacs Tutorial? under Help menu for Emacs. Emacs and Vim have been ported to virtually every platform, so you can install and use them on your PC or Mac as well. Another possibility is Nano (also called Pico). Nano is very easy to use, especially for beginners, but it is rather limited in functionality. It is only recommended for those are new to UNIX and feel there are just too many things to learn before you get to do anything. Even so, I recommend that you switch to either Emacs or Vim as early as possible. *this section taken mostly from linux-environment

8 Useful Commands

Some particularly useful ones: cd, rm, , , ls, mkdir, pwd And some to get comfortable with: , ps, less, echo, scp, , which, , , , gcc, make, touch, , , , tar

9 Redirection and stdio

Often when using the CLI (command-line-interface) you will need to redirect a program’s output or write to a file. This can be done with the >, >>, and | which write, append, and pipe, respectively. Pipe is used for piping output of one command into another, ps -a | grep bash, while > and >> are used to write to a file, ls -al . > /dir info.txt. *for now, use man to check out the stdio options.

4