linux_course_doc/certificates/essentials/lpi_essentials_lesson_03.md

169 lines
6.2 KiB
Markdown
Raw Permalink Normal View History

2021-04-10 14:04:47 +02:00
# Lesson 3.1
## Guided Exercises
1. According to the extensions, which of the following tools were used to create these files?
* archive.tar
* archive.tgz
* archive.tar.xz
1. According to the extensions, which of these files are archives and which are compressed?
* file.tar
* file.tar.bz2
* file.zip
* file.xz
1. How would you add a file to a gzip compressed tar file?
1. Which tar option instructs tar to include the leading / in absolute paths?
1. Does zip support different compression levels?
## Explorational Exercises
1. When extracting files, does tar support globs in the file list?
1. How can you make sure a decompressed file is identical to the file before it was compressed?
1. What happens if you try to extract a file from a tar archive that already exists on your filesystem?
1. How would you extract the file archive.tgz without using the tar z option?
# Lesson 3.2.1
## Guided Exercises
1. List the contents of your current directory, including the ownership and permissions, and redirect the output to a file called contents.txt within your home directory.
1. Sort the contents of the contents.txt file from your current directory and append it to the end of a new file named contents-sorted.txt.
1. Display the last 10 lines of the /etc/passwd file and redirect it to a new file in your users Documents directory.
1. Count the number of words within the contents.txt file and append the output to the end of a file field2.txt in your home directory. You will need to use both input and output redirection.
1. Display the first 5 lines of the /etc/passwd file and sort the output reverse alphabetically.
1. Using the previously created contents.txt file, count the number of characters of the last 9 lines.
1. Count the number of files called test within the /usr/share directory and its subdirectories. Note: each line output from the find command represents a file.
## Explorational Exercises
1. Select the second field of the contents.txt file and redirect the standard output and error output to another file called field1.txt.
1. Using the input redirection operator and the tr command, delete the dashes (-) from the contents.txt file.
1. What is the biggest advantage of only redirecting errors to a file?
1. Replace all recurrent spaces within the alphabetically sorted contents.txt file with a single space.
1. In one command line, eliminate the recurrent spaces (as done in the previous exercise), select the ninth field and sort it reverse alphabetically and non-case sensitive. How many pipes did you have to use?
# Lesson 3.2.1
## Guided Exercises
1. Using grep and the /usr/share/hunspell/en_US.dic file, find the lines that match the following criteria:
* All lines containing the word cat anywhere on the line.
* All lines that do not contain any of the following characters: sawgtfixk.
* All lines that start with any 3 letters and the word dig.
* All lines that end with at least one e.
* All lines that contain one of the following words: org , kay or tuna.
* Number of lines that start with one or no c followed by the string ati.
## Explorational Exercises
1. Find the regular expression that matches the words in the “Include” line and doesnt match the ones in the “Exclude” line:
* Include: pot, spot, apot
* Exclude: potic, spots, potatoe
* Include: arp99, apple, zipper
* Exclude: zoo, arive, attack
* Include: arcane, capper, zoology
* Exclude: air, coper, zoloc
* Include: 0th/pt, 3th/tc, 9th/pt
* Exclude: 0/nm, 3/nm, 9/nm
* Include: Hawaii, Dario, Ramiro
* Exclude: hawaii, Ian, Alice
1. What other useful command is commonly used to search within the files? What additional functionalities does it have?
1. Thinking back at the previous lesson, use one of the examples and try to look for a specific pattern within the output of the command, with the help of grep.
# Lesson 3.3.1
1. The user types the following to their shell:
2021-04-10 14:12:38 +02:00
```
2021-04-10 14:08:58 +02:00
$ PATH=~/scripts
$ ls
Command 'ls' is available in '/bin/ls'
The command could not be located because '/bin' is not included in the PATH environment variable.
ls: command not found
```
2021-04-10 14:04:47 +02:00
* What has the user done?
* What command will combine the current value of PATH with the new directory ~/scripts?
1. Consider the following script. Notice that it is using elif to check for a second condition:
2021-04-10 14:12:38 +02:00
```
2021-04-10 14:08:58 +02:00
> /!bin/bash
> fruit1 = Apples
> fruit2 = Oranges
if [ $1 -lt $# ]
then
echo "This is like comparing $fruit1 and $fruit2!"
> elif [$1 -gt $2 ]
then
> echo '$fruit1 win!'
else
> echo "Fruit2 win!"
> done
```
2021-04-10 14:04:47 +02:00
* The lines marked with a > contain errors. Fix the errors.
1. What will the output be in the following situations?
2021-04-10 14:12:38 +02:00
```
2021-04-10 14:08:58 +02:00
$ ./guided1.sh 3 0
$ ./guided1.sh 2 4
$ ./guided1.sh 0 1
```
2021-04-10 14:04:47 +02:00
## Explorational Exercises
1. Write a simple script that will check if exactly two arguments are passed. If so, print the arguments in reverse order. Consider this example (note: your code may look different than this, but should lead to the same output):
2021-04-10 14:12:38 +02:00
```
2021-04-10 14:08:58 +02:00
if [ $1 == $number ]
then
echo "True!"
fi
```
2021-04-10 14:04:47 +02:00
1. This code is correct, but it is not a number comparison. Use an internet search to discover how this code is different from using -eq.
1. There is an environment variable that will print the current directory. Use env to discover the name of this variable.
1. Using what you have learned in questions 2 and 3, write a short script that accepts an argument. If an argument is passed, check if that argument matches the name of the current directory. If so, print yes. Otherwise, print no.
# Lesson 3.3.2
## Guided Exercises
1. Read the contents of script1.sh below:
2021-04-10 14:12:38 +02:00
```
2021-04-10 14:07:05 +02:00
#!/bin/bash
if [ $# -lt 1 ]
then
echo "This script requires at least 1 argument."
exit 1
fi
echo $1 | grep "^[A-Z]*$" > /dev/null
if [ $? -ne 0 ]
then
echo "no cake for you!"
exit 2
fi
echo "here's your cake!"
exit 0
```
2021-04-10 14:04:47 +02:00
* What is the output of these commands?
2021-04-10 14:12:38 +02:00
```
2021-04-10 14:07:05 +02:00
./script1.sh
echo $?
./script1.sh cake
echo $?
./script1.sh CAKE
echo $?
```
2021-04-10 14:04:47 +02:00
2. Read the contents of file script2.sh:
2021-04-10 14:12:38 +02:00
```
2021-04-10 14:07:05 +02:00
for filename in $1/*.txt
do
cp $filename $filename.bak
done
```
2021-04-10 14:04:47 +02:00
* Describe the purpose of this script as you understand it.
## Explorational Exercises
1. Create a script that will take any number of arguments from the user, and print only those arguments which are numbers greater than 10.