Instruction

by Paweł Paduch published 2020/11/12 09:20:34 GMT+2, last modified 2020-11-12T09:21:58+02:00
We get acquainted with the basic commands in linux, we get to know the environment and how to compile the first programs. This lab aims to review the basics of operating in the Linux operating system environment.

Base of Linux

  1. Log in
    After starting the computers, select the LINUX operating system, the latest version.
    Log in with the login and password provided by the teacher.
  2. Home directory - we start here
    • pwd - print working directory, where we are
    • ls -la - listing the contents of the current directory
    • grep {string} {mask of files} - string search among files given by mask.
    • Environment Variables
      • echo $OUR_VAR - will display the value of OUR_VAR
      • alias ll="ls -la" - creating aliasess, here is an example of creating an ll shortcut executing the command ls -la.
      • env and set - operations on environment variables
      • export $VARIABLE - export environment variable.
      • hostname - show name of the computer
      • id - show id of user.
      • logname - show login of user.
  3. .bashrc and .profile - config files
    Many configuration files can be found in the user's home directory. Two of them.bashrc and .profile (in simplification) they are processed when opening a new terminal or logging in.
    Simple exercise:
    • Enter the following line in the .bashrc configuration file:
      export FOO="bar bar bar"
    • Open new terminal.
    • Check the contents of the variable FOO with:
      echo $FOO
  4. Moving around
    • cd <path> - change directory to the given path
    • mv <name1><name2> - change name/location of file
    • cp <name1><name2> - copy the name1 file to name2 file
    • rm <file> - removing file. Option -rf allows remove whole directory.
    • mkdir <directory> - makeing directory
    • rmdir <direcotyr> - removing directory
    Create directory A and B in the home directory. Directory AA and BB in directory A. AAA and BBB catalog in AA catalog. In directory B, directory CC. Now executecd (without parameters, we will end up in the home directory) and go to the BBB directory with one command. Check where we arepwd. Then go to the CC directory with one command. Delete the CC directory. Delete directory A. Delete directory B.
  5. Files

    • cat <file> - show content of the file
    • head <file> - display the first few lines of the file
    • tail <file> - display the last few lines of a file. Useful option -f when tracking log files.
    • ln -s <file> <link> - creating a symbolic link to a file.
    • more <file> - display file contents with freeze after each screen, typically 25 lines.
    • wc <file> - counting characters, words and lines in a file.
  6. Processes
    • ps - show running processes in the system, often used in such using ps -efa |grep student - show all system processes in long form but only pass through the filter those with the name "student"
    • kill, killall - mainly used for process removal. Although kill it is generally used to send signals.
    • top - showing the statistics of the most "resource-consuming" processes
    • the meaning of the signs& > >> | -background startup and redirectionli>
    • fg, bg - background process operations.
    Run the command ls > file.txt what will be the contents of the file file.txt? You can use the command more or cat. What is the size of the file?
    Now do the same but with a redirect >>. What is the size of the file? Repeat the operation with a single redirection and check the file size again.
  7. Others
    • tty - display the current device of the terminal
    • type <program> - full path to the program
    • uname -a - computer information including kernel number.
  8. Vim editory
    • we start writing:a,i,A,I,o,O
    • we finish writing:esc
    • moving:arrows, h,j,k,l,pgup,pgdwn,n,N,``,ma ~a
    • deleting:x,d,D
    • copying: y
    • pasting: p,P
    • searching: /,n,N,*,#
    • changing: cw,r
    • quiting: q,q!,wq,q!
    • settings: :set np :set nu :syntax on
    • config file: .vimrc
    • More information can be found prepared by me mini VIM tutorial. Unfortunatelly it's still in polish.
  9. Mounting drivers
    To mount the usb drive, execute the command
    mount /mnt/usb
    From then on in the directory /mnt/usb the contents of the usb drive will be available
    Before removing the usb drive, it must be unmounted:
    umount /mnt/usb
    Pendrive should mount automatically
    In case of problems, you have to ask the teacher.
    On some systems, mounting is in the /media directory instead /mnt
  10. Getting to know the gcc compiler
    Write a simple "hello world" program and compile it. Compilation is done with the gcc compiler.

    gcc file.c -o program

    If compilation requires specifying non-default search paths or including some library then options can be used:
    • -I<include_path>
    • -L<library_path>
    • -l<library>

    If we want the compiler to notify us about all errors and warnings, enable the option:
    • -Wall
  11. Passing parameters to the program - list of arguments (0,5 pt)
    The argument list consists of an array of pointers to strings.

    main (argc, argv)
    int argc; /* count of strings */
    char *argv[]; /* pointer to the subtitle table write **argv it is equivalent */
    {}
    Write a program that displays the input parameters. Each parameter should be displayed on a separate line and in the reverse order of entry. For example:
    ./program1 ala ma kota a kot ma ale
    ale
    ma
    kot
    a
    kota
    ma
    ala
    program1
    Note! The zero parameter is the name of the program.
  12. List of environment variables (0,5 points)
    • whenever a program is executed, a list of environment variables is also passed
    • it is placed in the process data space.
    • it is an array of pointers to C strings
    • it is terminated by a NULL pointer.
    • the parameter usually has the form: variable=string
    • it can be passed to a C program as the third parameter in main()
    • The C initialization function also creates an external variable
      extern char **environ ;
    • points to the same as envp
    • in fact the easiest way to do this is through
      char *getenv(char *variable_name);
    • The following program takes the variable list via the third argument of the main function and displays it:
      main(argc,argv,envp)
      int argc;
      char *argv[];
      char *envp[];
      {
      int ii;
      for (ii=0; envp[ii] != (char *) 0; ii++)
      printf("%s\n",envp[ii]);
      exit(0);
      }
    • rewrite and run
  13. Identification numbers, the so-called Pid'y (0,5 points)
    It is a unique number assigned by the system to each process. We can get it by means of a function:

    getpid();

    Each process also has its parent process. The parent process's pid can be obtained through a function

    getppid();

    Write a program to print the process ID and the parent process ID.
    Check what the functions getpgrp() and setpgrp() are for.
  14. User and group IDs (0,5 points)
    You can get them with getuid() and getgid().
    Write a program (or add to the previous one) that displays user and group identifiers.
    Check what the functions geteuid() and getegid() are for
  15. Files (1,5 points)
    • each file has many attributes
    • stat and fstat are used to read the file's attributes
      #include <sys/types.h>
      #include <sys/stat.h>
      int stat(char *pathname, struct stat * buf); /* by name */
      int fstat(int *fildes, struct stat *buf); /* by descriptor */
      
      struct stat {
          dev_t     st_dev;     /* ID of device containing file */
          ino_t     st_ino;     /* inode number */
          mode_t    st_mode;    /* protection */
          nlink_t   st_nlink;   /* number of hard links */
          uid_t     st_uid;     /* user ID of owner */
          gid_t     st_gid;     /* group ID of owner */
          dev_t     st_rdev;    /* device ID (if special file) */
          off_t     st_size;    /* total size, in bytes */
          blksize_t st_blksize; /* blocksize for file system I/O */
          blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
          time_t    st_atime;   /* time of last access */
          time_t    st_mtime;   /* time of last modification */
          time_t    st_ctime;   /* time of last status change */
      };
      Write a program that will display all possible data about the file whose name will be passed as a parameter. Check if the structure given above corresponds to that returned in Linux. Modify the program to display information about all specified files as parameters. The read data should be properly formatted, e.g. permissions, file type or time fields.
  16. Report.
    Sprepare a report in the form of a handy "cheat sheet" of the above commands. Check what options can be added to various commands. Search for other commands and describe.

    Some comments!
    Please upload reports to moodle under "spr" cathegory to the end of the next day at the latest. After this time, the reports will not be assessed.
    The report can be in the form of a txt or Open/Libre Office document
    The report should include the date, number and subject of the laboratory concerned, the group and the names of the team.
    The reports should be accompanied by program codes with a possible description of the compilation.
    The content of the reports may vary depending on the type of exercise. However, I will give some universal tips on what should normally be in the report and what can be rated:
    • The above-mentioned data.
    • The course of the exercise, along with screenshots (in the form of txt) of the screen, answers to the questions asked, the course of the tests we conducted.
    • Conclusions. What we learned, what we did and what we failed to do, what were the biggest problems.
    • Program codes.