UNIXX Shell codes

INFO 1211 Assignment 4 Page 3 of 3

KWANTLEN POLYTECHNIC UNIVERSITY

INFO 1211 – Spring 2017

ASSIGNMENT 4

UNIX Shell Scripts

OBJECTIVE

Upon successful completion of this assignment, the student will be able to:

  • • write, test and debug basic shell scripts using the C shell and bash shell

ASSIGNMENT

In this assignment you will be writing shell scripts to perform the tasks described. When writing a script you should:

  • include a comment describing the purpose/function of the script

  • include a one line summary showing how the script is used (the “Usage” comment line)

  • include in-line comments as required to explain individual lines or sections within a script

When handing in your assignment, for each script make sure you:

  • print out the code of the script

  • obtain PuTTY screenshots (use Alt+Print Screen to put the window on the clipboard, then paste it into a document) to demonstrate that the script works for all cases it was designed to handle; set the background colour to white and the text colour to black

Part A: The C Shell

  1. Write a C shell script called double that asks the user to enter a number. The script should then multiply the number by 2 and write out the result. The program should continue to ask for a number and write out its double, using a loop, until the user enters 0 for the input number. When the user enters 0, the loop and the script should terminate, without writing out the result of 0 times 2. (Note: if the user enters 0 for the very first input number, the script also should terminate immediately.)

  1. Write a C shell script called greeting that asks the user to enter their name. The script should then ask for a number (which should be positive). If the input number was negative, the script should display an error message. Otherwise, the script should write out “Hello name!” (replacing name with the name that was entered) as many times as the number that was entered, using a loop.

  1. Write a C shell script called menunix that allows a user to select from a menu of options, and then performs the selected UNIX command. The script should allow the user to go through the menu as many times as desired, until he/she opts to quit. Here are the menu options your script should handle:

    1. Show the current working directory’s name.

    2. Show a detailed listing of the files in the current working directory.

    3. Show a list of the users who are currently logged in, including when they logged in (other details may also be displayed, depending on what command you choose to use to perform this function).

    4. Prompt the user for a file name, and display a sentence indicating the number of words in the file.

    5. Exit the menu.

  1. Write a C shell script called nowseconds that calculates the number of seconds elapsed today:

Number of seconds elapsed today: 56342 seconds.

To do this, you will have to determine the current time and convert it to the number of seconds elapsed during the current day. To access the hour number of the current time, you can use date '+%H' (and similarly for Minutes and Seconds). Remember that you can assign the result of a UNIX command to a variable if you enclose the command in backquotes.

  1. Write a C shell script called makelist which uses a loop to receive input strings from the user. Each time the user enters a string, the string should be added to a list variable. When the users enters END to terminate the list, the list variable’s contents should be displayed.

Hint: start by creating a list variable and setting it to an empty list. Each time the user enters a string, append it to the list.

  1. Write a C shell script called toggle that accepts two command line parameters. The first parameter should be an option flag, either r, w or x. The second parameter should be a file name. The script should toggle the indicated file attribute for the specified file, and display an error message if the file is not found. For example, if the user enters:

toggle r thisfile

the script should toggle thisfile’s read attribute (turn it off if it is on, or turn it on if it is off).

Hint: use a switch statement based on the second command line argument, and depending on the letter entered check the corresponding file condition flag for the file named in the third command line argument. Depending on the existing file condition, set a new file condition accordingly.

  1. Write a C shell script called canrun that displays the names of the files in the current directory that have execute permission (permission being for the file owner) and how many of these files there are. For example, output from the script might look like:

Executable files:

makelist

nowseconds

toggle

3 files found

Hint: Create a loop to cycle through all the files in the directory. Then, each time through the loop, check the -x file condition flag to see if the file is executable. If it is, write it out and also increment a counter.

Part B: Short Answer Questions

Write answers to the following questions. Provide references for your answers.

        1. Investigate the GUID Partition Table (GPT) format, which is a replacement for the legacy MBR partition table scheme.

a. Describe three important differences between GPT and MBR.

    1. List three specific operating systems that support GPT.

    2. Describe how to make changes to a Windows system in order for it to use GPT instead of MBR.

  1. Find reasons online for why some computer users prefer to use Linux instead of Windows or the Mac OS. List the reasons and describe why you think each reason is valid or not. Also, explain why you personally would or would not consider switching your personal computer platform to Linux from your current one. (If you already use Linux, explain why you made this choice.)

  2. Describe the syntax for assigning values to variables and for doing arithmetic when using the bash shell. Also, describe the syntax of the loop (for, while, etc.) and conditional (if, switch, etc.) structures for the bash shell.

  3. Rewrite Part A, #7 using the bash shell and obtain screenshots to show your results.

WHAT TO HAND IN

This is not a team assignment; each student should hand in his/her own individual work. Hand in:

• a cover page, showing your name, the course name, the assignment number and the date the assignment is submitted, all centered horizontally on the page

• hand in your answers to the exercises, as described above, showing your code and screenshots

DUE DATES: S11: Tuesday, March 28, 2017 S10: Thursday, March 30, 2017