unix_corr
unix_corr
8. Make a copy of your file and stock it in a new subdirectory called bacup.
mkdir bacup; cp myAnimals.txt bacup/
9. Now you realize you misspelled the name of the backup directory (you forgot a k), rename it
to backup.
mv bacup backup
1
3 Redirecting I/O and basic piping
Navigate back from backup to Unix-TD1.
Questions
10. Using command echo (first read the manual page typing man echo) and the appropriate I/O
redirection, add “rat” to the animal list in your myAnimals.txt file.
echo rat >> myAnimals.txt
11. Create a new file called myAnimals2.txt containing the following list of animals (one per line)
— fox, alligator, gorilla, horse, dog, manatee, dove.
nano myAnimals2.txt
12. If one combines myAnimals.txt and myAnimals2.txt files, how many words and how many
lines are there? Use a pipeline of commands cat and wc to answer the question.
cat myAnimals.txt myAnimals2.txt | wc
13. Write a file called myCombinedAnimals.txt, containing the names of all the animals in your
myAnimals.txt and myAnimals2.txt files. The file should be sorted alphabetically and should
not contain repeated values.
cat myAnimals.txt myAnimals2.txt | sort | uniq > myCombinedAnimals.txt
14. Now using a single pipeline, write a new file called MYcOMBINEDaNIMALS.txt where the names
of the animals are written in capital case.
cat myCombinedAnimals.txt | tr a-z A-Z > MYcOMBINEDaNIMALS.txt
15. How many words in your MYcOMBINEDaNIMALS.txt file contain an A character?
grep "A" MYcOMBINEDaNIMALS.txt | wc -l
16. How many words do not contain an A character? Write the list in a file called noA.txt.
grep -v "A" MYcOMBINEDaNIMALS.txt > noA.txt
2
5 Creating users and changing permissions
Navigate to your Unix-TD1 directory.
Questions
18. Change the permissions of file myAnimals.txt so nobody can write the file.
chmod 555 myAnimals.txt (or even better chmod a-w myAnimals.txt)
19. Execute echo lombriz > myAnimals.txt. What happens?
(permission denied)
20. Create a user called “other” with password “o”.
sudo useradd other; sudo passwd other
21. Transfer the ownership of file myAnimals.txt to user other.
sudo chown other myAnimals.txt
22. Now try to change the permissions of file myAnimals.txt so everybody can read, write and
execute the file. What happens? Why?
chmod 777 myAnimals.txt (it fails because you are not the owner of this file anymore)
23. Now delete user other. What happened to the myAnimals.txt file?
sudo userdel other; stat myAnimals.txt (the file’s owner is the user whose UID is 1001
⇒ this user does not exist anymore ⇒ root should take ownership of this file)
6 My first script
Create a local directory called scripts and navigate to it.
Questions
24. Write and execute a bash script that:
(a) prompts the user for two numbers using the read builtin command,
(b) checks that both of them are positive (going back to the previous step if not),
(c) computes their mean,
(d) outputs the result if it is greater than or equal to 5,
(e) outputs an error message otherwise.
# !/ bin / bash
n1 = -1
n2 = -1
until [[ $n1 - ge 0 && $n2 - ge 0 ]]; do
echo -n " number ␣ 1 ␣ = ␣ " && read n1
echo -n " number ␣ 2 ␣ = ␣ " && read n2
done
let mean =( $n1 + $n2 )/2
if [ $mean - ge 5 ]; then
echo " mean ␣ = ␣ $mean "
else
echo " Mean ␣ is ␣ less ␣ than ␣ 5 " >&2
exit 1
fi
(correction)
3
7 Reading a well formatted CSV file
Let us consider that you are system administrator in a well-known banking company. The following
(very unsecured) file registers all customer transactions.
Questions
25. Create and fill in that file. Call it data.csv.
26. Write a bash script that outputs the current balance of a customer list.
./ balance . sh data . csv Lara Geralt Clad
Lara 880
Geralt 0
Clad 19
# !/ bin / bash
function balance ()
{
local IFS = " ; "
local balance =0
while read username date transaction comment
do
if [ " $username " == " $2 " ]
then
let balance = balance + $transaction
fi
done < $1
printf " % -10 s ␣ % d \ n " $2 $balance
}
if [ $ # - eq 0 ] || ! [ -f $1 ]
then
echo " script ␣ filename ␣ [ username ]... " >&2
exit 1
fi
for customer in " $ { @ :2} "
do
balance $1 $customer
done
(correction)