Introduction to Computer Science

Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due

Lab Exercises
(Lab 2, Chapter 6, "Introduction to Computer Science")

Content

  • Use of Shells, Man-Pages and File Systems
  • Use of Environment Variables
  • Automatization of processes by means of scripts

Preparation: Creation of a Practice Account

Switch on your computer and select “Ubuntu”. Log on to the system using the username “rnlabor” (symbolic name: ”Hochschule Ulm”) and password “student123!”.

1. Create a user called “practice” on the Linux system. To do that, click on the Unity launcher icon and enter “Users” in the search bar. Click on the “Users” icon. Unlock the application (click on the “Unlock” icon in the upper right corner of the application window). You will be asked again for a password. It is “student123!”, too. With the "Add Users" switch (bottom left) you can create the new "standard" user "practice". Afterwards you can change the "Login options" in the right part of the window. For the "Password" entry, use the password "bsysrn123!", Confirm the password again and press the "Change" button.

2. After that, close the “User Accounts” application and log off. Log in again with the user “practice” and the password “bsysrn123!”.

3. All further steps are carried out under the "exercise" account:

- Click on the Unity launcher icon and enter “Terminal”. Click on the “Terminal” icon and start a console terminal!
- The “Files” menu is an application which is similar to the “Windows Explorer” application of Windows 10. Start this application and select the “Home” folder.

There you can see the content of the folder „/home/practice“.

Part 1: First Steps with a Linux File System

The above started console terminal provides you with a „bash”-shell. Here you can work with the file system using command lines. You can find a list of typical commands in the file “Chapter6_OperatingSystemsLab2_Info” (see Moodle course -> Lab Report 2 -> Additional Information: File Systems). 

1. In the console terminal: Find out how your working directory is called (the directory in which you currently are). [pwd(1)]

Tip about „man-pages“: The entry above, [pwd(1)] means:

“Use the man-pages for the command “pwd” of section 1. There you get some

information about this command.”
Enter a command in the form “man <section> <command>”, e.g. “man 1 cd”. Now you can see the online manual to the command “cd”.
- By entering “q <return>” you can leave the man-pages.
- By pressing the “<spacebar>” button you can scroll down long texts.
- By entering “b” you can go upwards again, page by page.
- To search for a word, enter “/”, followed by a search term and <return>. The first line in which the search term appears is shown. When you enter “n”, the next line which contains the search term is displayed.
2. In the console terminal: Enter the following commands one after another:
- ls /home/practice
- ls –l /home/practice
- ls –l –d /home/practice
- ls –l –a /home/practice

Find out what the –l, -d, and -a options signify with means of the man-pages [ls(1)].

How can you interpret the information, which is given additionally to the name of a file or directory, especially the information of the “-l”-option?

Hint: A bash command usually consists of the command, options, a list of parameters and sometimes control characters:

<command> <options> <list of parameters> [<control characters>]

The list of parameters usually consists of the names of directories or files.

A typical command might be: “ls -l -a /etc”.

Capitalization needs to be taken into account (-a -A)!

Options change the behavior of a command (e.g. commands display the information in different levels of detail, according to the used option). You can also specify more than one option, which all start with a “-“ symbol, followed by a character.
Options can be combined (because the option always consists of exactly one character) in the following way: “ls -a -l” can be aggregated to “ls -al”.
In the graphical user interface (“Files” menu): try to get the same information about
your home directory, which is shown by the “ls -l”, and “ls -l -a” commands.
Hint: See the following link:
https://websiteforstudents.com/how-to-hide-and-unhide-files-with-ubuntu-desktop/
3. In the console terminal: now use so called “wildcards“:
- Change into the directory “/etc” [cd(1)].
- Use a command to see all files and directories that start with one of the letters a, b, or c! The contents of the subfolders should not be displayed. - Use a command to see all files and directories which start with “ant”, “bl”, or “ho”. The contents of the subfolders should not be displayed.

- Use a command to see all files and directories which do not start with one of the letters a, b, or c. The contents of the subfolders should not be displayed.

Hint: You can find some information at http://docstore.mik.ua/orelly/unix/upt/ch15_02.htm

Is there a similar function in the graphical user interface?

4. In the console terminal: Use the “.“ and “..” directories as well as the “~” expansion:
- Enter “cd ~” and identify your current working directory. What does “~” mean?
- Now enter “cd ..” and identify your current working directory again. What does “..” mean?
- Enter “cd ~” another time and afterwards “cd .”. Identify your current working directory. What does “.” mean? What might be the practical use of the “.” directory? For example think about the meaning of the PATH environment variable.

Part 2: Creation and Modification of Subdirectories

1. In the console terminal: Go back into your home directory “/home/practice”. There create a new subdirectory called “lab” [mkdir(1)]!
Create some more (empty) files and directories, so that the structure which is shown below is built [mkdir(1), touch(1), cd(1), ls(1)].
Hint: With means of “touch <file name>” you can create an empty file
In the graphical user interface: Create a new subdirectory called „labG” inside your home directory “/home/practice” and within that create the structure above again.

2. In the console terminal: Try to do the following tasks with as few command calls as possible:

- Delete the subfolder “lab-3” as well as all contained files and subfolders [rm(1)]! - Now copy all files and subfolders of “lab-1” into a new subdirectory “lab-3” [cp(1)]!
- Move all Files of the subdirectory “lab-1” into the subfolder “lab-2” [mv(1)]!
- Now the file structure should look like this:
- In the graphical user interface: Do the same actions as above inside the folder “/home/practice/labG”.
3. In the console terminal: Use of direct links („hard links“):
- Change your working directory to the subfolder “lab-2”.
- Create a hard link inside of “lab-1” with the name “file-11” to the existing file “file-11”, which is in “lab-2” [ln(1)].
- Compare the original file “file-11” in “lab-2” with the new entry in the subfolder“lab-1” [ls(1)]. Can you see which file is the original and which one is the linked file by means of the “ls” command?
- Remove the original file “file-11” inside the subdirectory “lab-2” and afterwards the entire subdirectory “lab-1” with the command “rmdir lab-1”. Does this work (yes/no)? If not, why?
4. Inside the console terminal: Use of indirect links („soft links“):
- Stay inside the directory „lab-2“.
- Create a symbolic link (“soft link”) inside “lab-1” called “file-12” to the existing file “file-12” in the subfolder “lab-2” [ln(1)].
- Can you see which file is the original and which one is the linked file by means of the “ls” command?

Part 3: Environment Variables and Shell Scripts

1. In the console terminal: With the command “printenv” you can list predefined environment variables and their values [printenv(1)]. These variables can be used inside of every script or program.
Hints:

  • You can get some information about environment variables at http://tldp.org/LDP/intro linux/html/sect_07_02.html , chapter 7.2.1, 

or in the book “The Linux Command Line”, which you can download at http://linuxcommand.org/tlcl.php , “Learning the shell”

  • General information about the creation of scripts at http://tldp.org/LDP/intro-linux/html/sect_07_02.html, chapter 7.2.5, 

or in the book “The Linux Command Line”, at http://linuxcommand.org/tlcl.php, “Writing Shell Scripts”

Create a script file inside your home directory by means of a text editor with the command “gedit script.bash“, which uses suitable environment variables to do the following:

- It displays the line “You are logged in as user: <user name>”,
<user name> stands for the user who calls the script.
- It displays the line “Your home directory is: <home directory>”,
<home directory> stands for the path to the home directory of the user who is calling the script.
- It displays the line “You currently use the shell: <shell>”,
<shell> stands for the name of the shell, that is used by the user who is running the script.

Use the “echo“ command [echo(1)] for the output of the lines above.

Test your script with the command “/home/practice/script.bash”.

2. Expand the value of the PATH environment variable in that way, that you can call the script directly, which means without entering the path to the script. How can you register this modification permanently, so that is still exists when you log in next time?

Hint: You can get some information about environment variables via http://tldp.org/LDP/intro-linux/html/sect_07_02.html , chapter 7.2.1

3. Expand your script “script.bash” of the task before in that way, that it firstly displays the following two lines:
The current time stamp is:
6:23 pm and 18 seconds
- Use the “date” command [date(1)] and the “command substitution” method to display the current time exactly as shown above.
Hint: You can get more information via http://www.computerhope.com/unix/udate.htm- Please note that the output of “date” can be modified with a formatting string.
Test your script by calling “script.bash”
- Call the script in that way that the standard output of the script will be appended at the end of the file “/home/practice/output_script” in your home directory.
Hint: For information see https://www.tutorialspoint.com/unix/unix-io-redirections.htm

4. Use the at command [at(1)] to start the script “script.bash” at a specific point of time, chosen by you. Check that it works correctly by listing the content of the file “output_script” with means of the “more” command [more(1)]. What happens when you forget to redirect the standard output of the script?

Hint: You find some information at http://www.computerhope.com/unix/uat.htm
- What are the functions of the commands “atq” and “atrm”? Start several jobs at different points of time and test the two commands!

5. Now try to start the script “script.bash” repeatedly every minute with the “crontab” command [crontab(1)].

- Before calling “crontab –e”, set the environment variable “EDITOR” to “gedit” (command: export EDITOR=gedit”; do not use blank spaces before or after the “=”!).

- Watch the function of the “cron-deamon” by listing the new contents of the file “output_script” with the command “tail -f output_script” [tail(1)].

Hint: You get some information at https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

发表评论

电子邮件地址不会被公开。 必填项已用*标注