5.5 Exercises
5.5.1 Conditionals
See Section 12.0.5 for solutions.
Declare a variable
xand set it to 5. Write anifstatement that prints “indeed” ifxis smaller than 10 and “nope” otherwise. Then setxto 20 and run your statement again.Write an
ifstatement that prints “is a file” if a variablethatis 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 variablethatand set it to “FileA.txt”, “myDir” and “FileB.txt” to test if yourifstatement does the right thing. Hint: you need a nested if statement.
5.5.2 Loops
See Section 12.0.6 for solutions.
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
Write a loop to add the word ”banana” 100 times to food.txt
Declare a variable
nand set it to “1 2 3”. Then write aforloop that loops overnand prints the first i lines of food.txt for each entry i inn(i.e. for i equal to 1 2 and 3). Now setnto “10 20 30” and rerun your loop.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!
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 = , …)
Write a
forloop that creates directories “myDir_1”, “myDir_2”, …, “myDir_10”.Write a
forloop 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.Create a directory “all”. Then write a
forloop 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?Write a
forloop 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.Write a
forloop 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: usewc -l FILE | awk '{print $1}'to get the number of lines of a file FILE. We will learn these commands later.Use a
whileloop 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.Use a
forloop 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”.Use
lsto 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 awhileloop to create a file someLines.txt that contains the first 7 lines of all files listed in allFiles.txt.[Challenge] Use a
forloop 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.