A Taste of Unix; Your first file

  1. Log-in to the cs server.
    You should see the unix command prompt below:
    cs500dd@cs:/u1/class/cs500dd>
    where the d's are your digits. The command prompt means that the operating system is ready for you to enter another command. The part of the command prompt after the colon
    /u1/class/cs500dd
    is the pathname of your home directory. Each account has associated with it a home directory for the owner's files and sub-directories. The Log-in sets your current directory to be your home directory. Both pathname and current directory are talked about below.
  2. Directories contain files and sub-directories. Sub-directories are just directories that contain their own files and sub-directories.
  3. Enter the command
    ls
    to see the files and sub-directories of the current directory. You should see:
    Desktop/
    The slash at the end of the name indicates that Desktop is a directory (sub-directory) and is NOT a file.
  4. Use of Current Directory. To use Desktop in a unix command, we can use its pathname, for example enter the command
    ls   /u1/class/cs500dd/Desktop
    This will show the content of the Desktop directory
    (Since you have not made any files in Desktop, it is empty.)
    But there is a quicker way to do the same thing! Right now since Desktop is in the current directory,   /u1/class/cs500dd,   we can enter the shorter command:
    ls   Desktop
    Note: Desktop is still a pathname, but it is a pathname relative to the current directory. Pathnames that start at the root directory with a   /,   are called absolute pathnames.
  5. Looking Around; Changing the current directory. From number 1 above, we have the pathname for your home directory:
    /u1/class/cs500dd
    The left-most slash stands for the root or top directory. The rest of the slashes are NOT directories; they just separate one directory name from the next. The path tells us that:
    u1 is a sub-directory of the root directory,  /
    class is a sub-directory of directory u1,
    cs500dd is a sub-directory of directory class.
    The pathname /u1/class/cs500dd is the list of directories (path) from the root to cs500dd where each item is a sub-directory of the directory before it.
    Containment of Directories

    You can use the cd to change the current directory. Each of the four commands below changes the current directory.
    cd  /
    cd  /u1
    cd  class
    cd
    
    Enter the first cd command. It sets the current directory to be the root directory. Now enter the ls command. It displays the content of the current directory (root directory). One of the sub-directories is u1, but there are lots of others: bin, boot, etc, var, and so on.

    Enter the second cd command. It sets the current directory to be /u1. Again enter the ls command. It displays the content of /u1.

    The third cd command is different from the first two. The pathname class does not start with a /, so it is a relative pathname, relative to the current directory. Enter the third cd command. Before it executes the current directory is /u1. So the third command sets the current directory to be the class sub-directory of directory /u1. Enter the ls command to see all of the class accounts.

    Enter the fourth cd command. The syntax (pattern) of all unix commands is:

    cmdName arg1 arg2 ...
    where the command name is followed by zero or more arguments separated by spaces. A command argument is just something typed. This fourth cd command has zero arguments. It sets the current directory to be your home directory. You can not get lost since this last cd takes you back home!

  6. Using a file editor (pico); Making a small file. First make sure you are in your home directory,.
    cd
    We are going to make a program file named pgm1.c We start our work by entering the command:
    pico pgm1.c
    This command starts the pico editor program which will let you write or change file pgm1.c.
    Unlike the other unix commands you have seen, pico does not immediately finish. It waits until we tell it we are done writing.

    The upper part of the pico screen is where you type in the file content. The lower two lines list several pico commands you can use.

    -----------------The two important pico commands------

    ^O   Write Out
    ^X   Exit
    
    The caret (^) in front of each command means hold down the ctrl (or control) key when typing the letter that follows.

    ^O:   To SAVE your work (Write Out) you hold down the ctrl key when you type the O key. When you do this, pico responds with the line:

    File name to Write:   pgm1.c
    The file name that appears is the name you typed in the when you started pico. Mostly you will just press enter to accept the name,
    but you have the option of backing up, changing the file name, and THEN pressing enter which writes your work to the new file.

    ^X:   To EXIT pico, you hold down the ctrl key and type the X key. If you do this after you have done the ^O command, pico will return you to the unix command line.
    But if you have not saved all that you have typed, pico gives you the option of saving your recent changes before returning to the unix command line.

    ---------------------------------------------

    In the pico window type:

    #include<stdio.h>
    
    int main() {
      printf("Hello, World\n");
    }
    

    Once all of the above has been typed, type the command ^O then press the enter key to save your work to the file pgm1.c. Finally type ^X to exit pico.

  7. Checking the file pgm1.c. First enter the command:
    ls
    
    This will list out the content of the current directory. You should see:
    Desktop/  pgm1.c
    
    Now let's check the content of our file pgm1.c One way to do this is to use the pico command:
    pico pgm1.c
    
    and then do ^X to exit pico. A quicker way is to enter the command
    cat  pgm1.c
    
    The cat command displays the content of pgm1.c and is done immediately. There is NO NEED to exit with a ^X.

  8. Compile and Run. Compile the C language program pgm1.c by entering the command:
        gcc pgm1.c
    
    If you made no grammar errors when you wrote pgm1.c, then NO complaints (error messages) will appear on the screen and the command will MAKE a new file, a.out that contains the machine language translation of pgm1.c. If there are errors, use the command
        pico pgm1.c
    
    to look file pgm1.c and fix it. Compare to the code above. Save the fixed code. Exit pico. Recompile.

    Finally when pgm1.c compiles without error, You are ready to run the program. Enter the command:
        a.out
    
    You should see
    Hello, World
    
    on the screen.