Arrays and Shell Customization

Refer to the slide entitled Arrays in the slides at the link entitled "interactive bash", or other documentation about bash arrays. Refer also to the slide entitled "Why don't variable changes 'work'?"

In a clone of your distributed virtual machine log in as user student and operate in /home/student.

On the command line, run a command that loads an array named DAYS with the names of the seven days, monday through sunday. Follow it with a command that prints "There are 7 days in the week." Do not supply "7" literally, your command must use the expression that returns the count of elements in DAYS.

Now we'll write a script. With an editor open a new file named in your home directory. In it, type a command much the same as the one you ran at the command line, except that it loads an array named WEEKDAYS (not DAYS) with just the names of the five (not seven) weekdays in order monday through friday. Load monday into the array element with index 1 (not the default 0), so that monday through friday will have index values 1 through 5 respectively (not 0 through 4). The script consists of just that single line.

In case you don't know a command line editor, you could start the gui just for the purpose of using the gedit graphical editor, which is easier. Press ctrl-alt-F2 (gives you a new, separate terminal), log in there as student, startx to launch the gui, run the gedit graphical editor (icon under "favorites") to do the job and save to /home/student, press ctrl-alt-F1 to return to your original command line shell environment.

Save this script file then run it. To make that happen, you'll have to 1) give it executable permissions, and 2) specify the script file's location unambiguously:

chmod +x
                             [ the leading period is shorthand for the current directory, and becomes an unambiguous full path prefix for the file ]


At the command line, now that running the script has created the WEEKDAYS array, run much the same command you ran above to print the an element count. Again, use the expression that returns array element counts, applied to WEEKDAYS. We expect it to produce "5" so that it will print something like "There are 5 weekdays in the week."

If it doesn't, run the script again, but source it this time (with the bash built-in command "source"). Then repeat the command that returns WEEKDAYS' count. For your efforts, you now have available in your environment a weekday array. You could use it for lookup any time you want to refer to a weekday by its number, and get its name. It's a new feature! But only in this shell. What if you wanted it to make this array a standard item, so that you would have it in the environment of all the shells that you run (always there, like PATH for example)?

This calls for a shell customization. In ~/.bashrc (equivalent to /home/student/.bashrc) add the line:

source ~/            [ ~ is shorthand for the current user's home directory ]

Exit the shell back to the login prompt, then start a fresh shell by logging in anew. Run the command to count WEEKDAYS' elements, to test whether or not WEEKDAYS exists.

What to turn in:

Make a screenshot showing a fresh login, with the login and password prompts visible. Then execute the command to show the count of the WEEKDAYS array, printing "There are 5 weekdays in the week." And then the command that shows all the array's element values, that is, the names of the weekdays. And finally show your MAC address:

ifconfig  enp0s3  |  grep ether

The screenshot needs to show all 4 artifacts, the login prompt, the command showing the array's element count, the command showing the array's elements' contents, and the VM interface's MAC address. Name the screenshot file "arraysshells.jpg" (or .png) and transfer it to your assignments directory on the remote server.

To take this screenshot, you'll need to use the screenshot tool of your host, not of the guest VM (why?). Arrange your VM's VirtualBox window to be open on your host's desktop, then snap it.