5.5 Exercises

5.5.1 Conditionals

See Section 12.0.5 for solutions.

  1. Declare a variable x and set it to 5. Write an if statement that prints “indeed” if xis smaller than 10 and “nope” otherwise. Then set x to 20 and run your statement again.

  2. Write an if statement that prints “is a file” if a variable that is indeed a file, “is a directory” if it is a directory and nothing if it is neither (i.e. it does not exists). Create a file FileA.txt and a directory “myDir”. Declare the variable that and set it to “FileA.txt”, “myDir” and “FileB.txt” to test if your if statement does the right thing. Hint: you need a nested if statement.

5.5.2 Loops

See Section 12.0.6 for solutions.

  1. Go to directory chimp (previously created in Exercises Chapter 2), write a loop to add the words ”bananas”, ”mangos” and ”ants” to the file food.txt

  2. Write a loop to add the word ”banana” 100 times to food.txt

  3. Declare a variable n and set it to “1 2 3”. Then write a for loop that loops over n and prints the first i lines of food.txt for each entry i in n (i.e. for i equal to 1 2 and 3). Now set n to “10 20 30” and rerun your loop.

  4. Write a loop that writes 100 times ”ACGT” to DNA.txt, but all 100 times on the same line. Hint: check the man pages for echo to find a way!

  5. Write a loop that reads each line of food.txt and prints it, starting with ”Line n = ” followed by the actual line. Here, n is the number of the line (Line 1 = , Line 2 = , …)

  6. Write a for loop that creates directories “myDir_1”, “myDir_2”, …, “myDir_10”.

  7. Write a for loop that writes “I’m in directory DIR” to a file “DIR.txt” into each directory starting with “myDir_”, where DIR corresponds to the name of the directory.

  8. Create a directory “all”. Then write a for loop that loops over all directories starting with “myDir_”, enters that directory, moves all files in there to the directory all and exists the directory again. Can you also come up with a solution without using a loop?

  9. Write a for loop that creates 100 files names “File_1.txt”, “File_2.txt” and so forth. Each file “File_x.txt” should contain exactely x lines reading “Line 1”, “Line 2”, …, “Line x”. For instance, file “File_4.txt” should have the four lines “Line 1”, “Line”, “Line 3* and”Line 4”. Hint: use nested loops.

  10. Write a for loop that loops over all files “File_1.txt”, “File_2.txt”, … and appends the line “This file has x lines” to them, where x should match the actual number of lines. Hint: use wc -l FILE | awk '{print $1}' to get the number of lines of a file FILE. We will learn these commands later.

  11. Use a while loop to read each line of file “File_5.txt” and print “File_5.txt, line x: LINE” to screen, where x is the line number and LINE is the actual line of that file.

  12. Use a for loop to run the above solution (printing the file name and line number in front of the line) for all files matching “File_*.txt”. Write the output for any file “File_x.txt” to “File_x.txt.annotated”.

  13. Use ls to create a file allFiles.txt that contains the names of all files “File_2.txt”, “File_3.txt”, File_12.txt”, File_13.txt”, “File_22.txt”, “File_23.txt”, “File_32.txt”,…, “File_93.txt”. Then use a while loop to create a file someLines.txt that contains the first 7 lines of all files listed in allFiles.txt.

  14. [Challenge] Use a for loop to print out the first 50 values of the Fibonacci series 1, 1, 2, 3, 5, 8, 13, …. Hint: use a variable to store the previous two values.

;)
;)