CIT052 Index > Shell Variables and Arithmetic

Shell Variables and Arithmetic

Write a script to do the following:

  1. Create an array called german_days with the words Sonntag, Montag, Dienstag, Mittwoch, Donnerstag, Freitag, and Samstag. (These correspond to Sunday through Saturday.)

  2. Set a variable named day_number to the day of the week in the range 0..6 by executing the date '+%w' command.

  3. Put the date in European numeric form into a variable euro_date by executing the date '+%d-%m-%Y' command.

  4. Put the date in English into a variable us_date by executing the code date '+%A, %m/%d/%Y' command.

  5. Set the variable total_files to the number of files in your current directory by executing the ls command and piping that result to wc -l.

  6. Set the variable n_directories to the number of subdirectories in your current directory by executing ls -l, piping that to grep '^d' and piping that result to wc -l. (See if you can figure out why this works.)

  7. Set the variable reg_files to be the number of regular (non-directory) files. You get to figure out how to do this yourself. Hint: arithmetic is good.

  8. Echo these data as shown below. The items in italics will be filled in by the variables you set in the preceding steps. You must use a “here” document to output the results. Your line breaks must occur exactly where you see them here. Don’t worry about grammar; if you end up with “1 directories” that’s all right.

    Heute ist Montag, 31-03-2008.
    Today is Monday, 03/31/2008.
    
    You have 2 directories and 3 regular files
    for a total of 5 files.

When You Finish

Rename your script file in the form lastname_firstname_shell_vars.sh and upload it to Moodle.