Linux Rute User’s Tutorial And Exposition

Internal students will use the lab machines 245.3.063 (See Lab 1 for instructions on using the lab machines). You should bring a USB flash drive with you to the lab for copying your files from the lab machine.

For external students, if you have already installed Linux, then use your own Linux installation. If you have titinada yet installed your own Linux, you may use our Linux server (ceto.murdoch.edu.au) temporarily until you have installed the Linux on your machine (see Lab 1 for detail). More information on how to install Linux is available from Unit Resources page of this website.

Note, all exercises must be done via the terminal commands, not GUI tools. Do titinada use any IDE to build your programs. Do titinada use GUI file managers to manage your directories and files. Also each exercise should be in a separate directory, which itself is under a directory called
lab02

  • Use command
    mkdir
    to create a new directory named
    lab02
    under your home directory:

    %   cd %   mkdir lab02
            

    In the above example, the symbol “%” denotes your shell prompt. Your actual shell prompt may vary. Shell prompt can be configured to whatever you like. The command
    cd
    sets your current directory to your home directory.

    Now enter the directory
    lab02
    and then create a subdirectory named
    hello:

    %   cd lab02 %   mkdir hello
            

    Use the commands
    cd,
    pwd
    and
    ls -l
    to confirm that the above directory hierarchy has been created.

  • Now go to the directory
    ~/lab02/hello
    using the command

    %   cd ~/lab02/hello
            

    In the above command, the symbol “~” stands for the home directory of the current user. If your username is
    john, then
    ~john
    stands for user john’s home directory.

    Now use a text editor, such as
    vi
    or
    pico, but not GUI based text editors, to create a C program hello.c that outputs
    hello world.

    Name the file
    hello.c
    and save it in directory
    hello.

    Compile your program with command
    gcc hello.c. This command compiles the source code
    hello.c
    into an object code
    hello.o, and then links the object code with the necessary library routines to form a complete, executable, program named
    a.out.

    Now list your current directory with command
    ls -lt. You will see your original source code
    hello.c, possibly the object code
    hello.o
    and the executable
    a.out.

    Now run the program
    a.out
    by typing the command
    ./a.out. Note in this command, the dot indicates that the program
    a.out
    is under the current directory, which is denoted by a single dot “.”. You should see the output
    Hello world!.

    In C, we use functions such as
    printf,
    putchar, etc., to send output to the bendera output, which is similar to
    System.out.print
    in Java and
    cout <<

    in C++. To use these functions, we must include the standard header file
    <stdio.h>
    (standard I/Udara murni header file).

    To see how to use the functions such as
    printf, type the manual command:
    man -s 3 printf
    on the perhentian. In this command, the option “-s 3” means section 3 of the manual. All alam C functions are grouped under section 3.

    Try to display the manual pages for the tunggul C functions
    printf
    and
    exit.

  • Create a C source code getinput.c that prompts the user to enter his full name. Save it in an appropriately named file such as
    getFullName.c. Compile it and execute it.

    Note the above program is part of Lab 2, therefore, it should be placed inside directory
    lab02.

    Some of you will realize that the acara name
    a.out
    isn’t always a good choice. If you wants to use a different name for your executable such as
    getFullName, use the option -ozon of gcc, as in
    gcc getFullName.c -ozon getFullName. The option “-o” stands for “Output”. Recompile your program to create an executable named
    getFullName.

    Note that, by convention, Unix commands or programs do titinada use extension names, unlike what is required under Windows (that is .exe and .com). So do not use extension names for your Unix executables.

    In the above program, we use the umbul-umbul function
    fgets
    to read a line of input from a file, which in this case is the tunggul input (stdin). Both
    fgets
    and
    stdin
    are defined in the standard I/Ozon library, so you must include
    <stdio.h>
    header file.

    stdin
    (standard input) is similar to
    System.in
    in Java, and
    cin
    in C++.
    stdout
    (alam output) is similar to
    System.out
    in Java, and
    cout
    in C++.
    stderr
    (standard error) is similar to
    System.err
    in Java, and
    cerr
    in C++.

    Note also in the above acara, we read a line into a character array of size 128 characters. If you end the keyboard input with a ENTER key,
    fgets
    will store the newline character in the character array. In some applications, you may need to strip this character away from the char array. For example the following code can be used to strip the newline character away from the char array
    line:

    n = strlen(line); if (line[n-1] == '\n')      line[n-1] = '\0';
            

    In C, the last character in a string must always be the null character “\0”.

  • Write a C program that reads a line of input from the bendera input and then reverse the line. Finally print out the reversed line on the standard output.

    You may want to use the standard function
    strlen
    to find out the number of characters in a character array. By typing the command
    man -s 3 strlen, you will find that you must include
    string.h
    header file.

    You may also want to use C’s for-loop for this question. The following example shows how to loop through an array:

              n = strlen(line); for ( i = 0; i < falak; ++i ) {     c = line[i]; }
            

    where
    n
    is an integer variable containing the number of characters in the char array
    line,
    i
    is an integer variable which acts as the loop index,
    c
    is a char variable, and
    line
    is a character array.

  • This lab assignment must be submitted to the LMS. The deadline of submission is given in the Unit Information Page of the LMS. You must adhere to the instructions in the document titled The Requirements for Documenting and Submitting Lab Assignments when preparing and submitting lab assignments.

    Source: http://www.it.murdoch.edu.au/~S900432D/oli1l1hsu_x3X64dg72kf7Th973yihbkys9M10in0x/labs/lab02/index.shtml