Question: Type in and run the following scripts from the text, chapter 13: test6 (p. 339, for-each loop--of course, use your own home directory in the

Type in and run the following scripts from the text, chapter 13:

test6 (p. 339, for-each loop--of course, use your own home directory in the variable, not the authors)

test9 (p. 342, C-style for-loop with multiple variables)

test10 (p. 343, while loops)

cat then run all three scripts, and capture that part of the terminal interactions below.

>>>>>Paste your terminal capture below this line >>>>>

<<<<< Paste your terminal capture above this line <<<<<

Create a script (in your $HOME/script directory) called Lab7Script. The script should:

Check to see if the directory $HOME/Lab7 exists. If it does, delete it and any contents.

Then, create the directory (empty).

Then, using a C-style for-loop, create ten files named file-0, file-1, ..., file-9 (using touch) within the $HOME/Lab7 directory.

Then, provide a listing of $HOME/Lab7

A test run of my script is shown below:

hurley@hurley-VirtualBox ~/scripts $ Lab7Script

Deleting existing directory /home/hurley/Lab7 and all contents...done.

Creating empty /home/hurley/Lab7...done.

Listing of /home/hurley/Lab7:

Creating ten files...done.

Listing of /home/hurley/Lab7:

file-0 file-1 file-2 file-3 file-4 file-5 file-6 file-7 file-8 file-9

hurley@hurley-VirtualBox ~/scripts $

cat Lab7Script, then run Lab7Script, and capture that part of the terminal interactions below.

>>>>>Paste your terminal capture below this line >>>>>

<<<<< Paste your terminal capture above this line <<<<<

Create a script (in your $HOME/script directory) called Lab7Script2 that creates a multiplication table like mine:

hurley@hurley-VirtualBox ~/scripts $ Lab7Script2

| 1 2 3 4 5 6 7 8 9 10 11 12

-----+------------------------------------------------------------

1 | 1 2 3 4 5 6 7 8 9 10 11 12

2 | 2 4 6 8 10 12 14 16 18 20 22 24

3 | 3 6 9 12 15 18 21 24 27 30 33 36

4 | 4 8 12 16 20 24 28 32 36 40 44 48

5 | 5 10 15 20 25 30 35 40 45 50 55 60

6 | 6 12 18 24 30 36 42 48 54 60 66 72

7 | 7 14 21 28 35 42 49 56 63 70 77 84

8 | 8 16 24 32 40 48 56 64 72 80 88 96

9 | 9 18 27 36 45 54 63 72 81 90 99 108

10 | 10 20 30 40 50 60 70 80 90 100 110 120

11 | 11 22 33 44 55 66 77 88 99 110 121 132

12 | 12 24 36 48 60 72 84 96 108 120 132 144

hurley@hurley-VirtualBox ~/scripts $

You must use nested, C-style for-loops to construct the table!

A suggestion on how to structure your code:

output header lines

for each row do

output the leftmost column and divider

for each column do

output row times column

done

output end of line

done

It might be handy to be able to format your output so that all the numbers line up. Use the printf command! man printf, then see this example:

printf %3d $var

...which will output the number stored in $var in a space exactly 3 characters wide. Want to add some more space around it, to separate it from the other numbers in the table?

printf %3d $var

or

printf %6d $var

The first parameter to the printf command is the format string. % indicates the start of a placeholder. The number indicates how many spaces to use for the content; and the d means decimal number. Now all we have to do is supply a decimal number to replace the placeholder--thats the second parameter, $var. So $vars numeric content replaces the %3d, and if its shorter, like 16, itll automagically get padded with spaces so it takes up 3 characters. Try it!

If you want to output a newline character (like pressing enter on the keyboard, to end a line of output and go to the next line), you can either

echo #echo the empty string, plus a newline character

or

printf #write a newline character

When you like the way it looks,

cat Lab7Script2

then run Lab7Script2

Paste the relevant terminal interaction below.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!