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
- 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.
- 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:
The list of parameters usually consists of the names of directories or files.
Capitalization needs to be taken into account (-a ≠ -A)!
- 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?
Part 2: Creation and Modification of Subdirectories
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”.
- 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?
- 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
- 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.
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
- 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?
- 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/