6.9 Exercises

6.9.1 Permission systems

See Section 12.0.7 for solutions.

Consider the three users chimp, gorilla and alien. For each user, there is a group with the same name, i.e. chimp belongs to group chimp; gorilla belongs to group gorilla, and alien belongs to group alien. In addition, chimp and gorilla both belong to group ape.

For the following examples, list all users that have 1) read permissions, 2) write permissions and 3) execute permissions. Hint: the user and group permissions always supersede lower (i.e. other) permissions.

The first column represents permissions, the second column represents user, the third column represents group:

Permissions User Group

  1. -r--r----- chimp chimp

  2. -rwxrw-r-- chimp ape

  3. -r-xr--rwx alien ape

6.9.2 Changing Permissions

See Section 12.0.8 for solutions.

Note: If you are on Windows using Windows Subsystem for Linux (WSL), make sure you work in a Unix directory as Windows will not let you change permissions for directories under its control (e.g. your Windows home or desktop folders). Simply type cd, and it will jump to your Linux home directory. In there, you should be able to modify permissions.

  1. Create a file “myPrecious.txt” and use echo to write the text “I and I alone!” into it. Make sure you are the only one allowed to read or write the file. Check the permissions using ls.

  2. Change the permissions again to make sure that only the group permissions are set to read and write. Check that they are correct. Try to print the file content to the screen.

  3. Remove the file myPrecious.txt

  4. Create a directory “It’s all yours!”. Create a file “yours.txt” inside that directory. Check it is there.

  5. Remove execution rights for yourself and try to enter the directory. Can you still see the files inside the directory?

  6. Remove directory “It’s all yours!”

  7. Use vim to create a script “love.sh” that prints “I like BASH!” to STDOUT, and execute it 100 times.

6.9.3 Writing BASH Scripts

See Section 12.0.9 for solutions.

  1. Write a script called “dog.sh” that takes three arguments: the name, the age and the color of a dog. In your script, print “Here comes …, a … year old dog of … color.”, where you replace … with the arguments above. Call your script with two of your favourite dogs.

  2. Use vim to create a script “like.sh” that takes an argument and prints “I like …!” where … is the argument passed. Use a for loop to write “I like biology!”, “I like computer science!” and “I like bioinformatics!”.

  3. Write a script called “reasons.sh” that takes an argument and appends it to a file called “whyILikeBASH.txt”. Call your script with three reasons why you like BASH (if you can’t come up with a reason, you can also lists reasons why don’t like it, and hopefully change your mind soon). Check the file.

  4. Write a script called “append.sh” that takes two arguments: a string and a name of a file to which the string is to be appended. Use it to write the words “Bern”, “Fribourg”, “Lausanne” to a file called “words.txt”. Check the file.

  5. Write a script called “append2.sh” that takes two arguments: a string and a name of a file to which the string is to be appended. Your script should first test if the file (given by the second argument) exists. If it does not, the script should create it and write “Created by append.sh” to it. Call your script to write three words of your choice to a file called “moreWords.txt”. Check the file.

  6. Create a script “positive.sh” that takes a number as an argument. If the number is larger than zero, it should write the number to a file. The name of that file should be yourNumber.txt (for example, if your number is 5, the file is called 5.txt). Call your script with the numbers -10, 0 and 10, and check if only a file called “10.txt” has been written.

  7. Create a script “explain.sh” that goes through all files and directories in the current directory and prints the name followed by “is a directory” or “is a file”. Create some extra directories and files before running it.

  8. Create a script “helper.sh” that 1) writes a script “print.sh” and 2) executes it. The script print.sh shall then take two arguments: a string and a number, and it should print the string number times to STDOUT. Finally, the script helper.sh should take the same arguments and forward them to print.sh when executing it. Run it to print 10 times “Gotcha!”.