The TC Shell Lab Exercises

Lab 1—First Script

  1. Write a script called greetme that will:

    1. Greet the user.

    2. Print the date and time.

    3. Print a calendar for this month.

    4. Print the name of your machine.

    5. Print a list of all files in your parent directory.

    6. Print all the processes you are running.

    7. Print the value of the TERM, PATH, and HOME variables.

    8. Print " Please couldn't you loan me $50.00? "

    9. Tell the user " Good bye " and the current hour. (See man pages for the date command.)

  2. Make sure your script is executable.

    chmod +x greetme
    
  3. What was the first line of your script?

Lab 2—Getting User Input

  1. Write a script called nosy that will:

    1. Ask the user's full name—first, last, and middle name.

    2. Greet the user by his or her first name.

    3. Ask the user's year of birth and calculate the user's age.

    4. Ask the user's login name and print user's ID (from /etc/passwd).

    5. Tell the user his or her home directory.

    6. Show the user the processes he or she is running.

    7. Tell the user the day of the week, and the current time in nonmilitary time.

    The output should resemble:

    "The day of the week is Tuesday and the current time is 04:07:38 PM."

  2. Create a text file called datafile (unless this file has already been provided for you.)Each entry consists of fields separated by colons. The fields are:

    1. First and last name

    2. Phone number

    3. Address

    4. Birthdate

    5. Salary

  3. Create a script called lookup that will:

    1. Contain a comment section with the script name, your name, the date, and the reason for writing this script. The reason for writing this script is to display the datafile in sorted order.

    2. Sort the datafile by last names.

    3. Show the user the contents of the datafile.

    4. Tell the user the number of entries in the file.

  4. Try the echo and verbose commands for debugging your script. How did you use these commands?

Lab 3—Command Line Arguments

  1. Write a script called rename that will:

    1. Take two filenames as command line arguments, the first file is the old file and the second file is the new one.

    2. Rename the old filename with the new filename.

    3. List the files in the directory to show the change.

  2. Write a script called checking that will:

    1. Take a command line argument, a user's login name.

    2. Test to see if a command line argument was provided.

    3. Check to see if the user is in the /etc/passwd file. If so, will print:

      "Found <user> in the /etc/passwd file."

      Otherwise will print:

      "No such user on our system."

Lab 4—Conditionals and File Testing

  1. In the lookup script, ask the user if he or she would like to add an entry to the datafile. If yes or y:

    1. Prompt the user for a new name, phone, address, birthday, and salary. Each item will be stored in a separate variable. You will provide the colons between the fields, and append the information to the datafile.

    2. Sort the file by last names. Tell the user you added the entry, and show the line preceded by the line number.

  2. Rewrite checking.

    1. After checking whether the named user is in the /etc/passwd file, the program will check to see if he or she is logged on. If so, the program will print all the processes that are running; otherwise it will tell the user:

      "<user> is not logged on."

  3. The lookup script depends on the datafile in order to run. In the lookup script, check to see if the datafile exists and if it is readable and writeable.

  4. Add a menu to the lookup script to resemble the following:

    [1] Add entry

    [2] Delete entry

    [3] View entry

    [4] Exit

  5. You already have the Add entry part of the script written. The Add entry routine should now include code that will check to see if the name is already in the datafile and if it is, tell the user so. If the name is not there, add the new entry.

  6. Now write the code for the Delete entry, View entry, and Exit functions.

  7. The Delete part of the script should first check to see if the entry exists before trying to remove it. If the entry does not exist, notify the user; otherwise remove the entry and tell the user you removed it. On exit, make sure that you use a digit to represent the appropriate exit status.

  8. How do you check the exit status from the command line?

Lab 5—The Switch Statement

  1. Rewrite the following script using a switch statement.

    #!/bin/tcsh –f
    # Grades program
    
    echo –n "What was your grade on the test? "
    set score = $<
    if ( $grade >= 90 && $grade <= 100 ) then
       echo You got an A!
    else if ( $grade >= 80 && $grade < 89 ) then
       echo You got a B.
    else if ( $grade >= 79 && $grade < 79 ) then
       echo "You're average."
    else if ( $grade >= 69 && $grade < 69 ) then
       echo Better study harder
    else
       echo Better luck next time.
    endif
    
  2. Rewrite the lookup script using switch statements for each of the menu items.

Lab 6—Loops

  1. Write a program called picnic that will mail a list of users, one at a time, an invitation to a picnic. The list of users will be in a file called friends. One of the users listed in the friends file will be Popeye.

    1. The invitation will be in another file, called invite.

    2. Use file testing to check that both files exist and are readable.

    3. A loop will be used to iterate through the list of users. When Popeye is reached, he will be skipped over (i.e., he does not get an invitation), and the next user on the list sent an invitation, and so forth.

    4. Keep a list with the names of each person who received an invitation. Do this by building an array. After everyone on the list has been sent mail, print the number of people who received mail and a list of their names.

    Bonus: If you have time, you may want to customize your invite file so that each user receives a letter containing his or her name. For example, the message might start:

    Dear John,

    Hi John, I hope you can make it to our picnic…

    To do this your invite file may be written:

    Dear XXX,

    Hi XXX, I hope you can make it to our picnic…

    With sed or awk, you could then substitute XXX with the user name. (It might be tricky putting the capital letter in the user name, because user names are always lowercase.)

  2. Add a new menu item to the lookup script to resemble the following:

    [1] Add entry
    [2] Delete entry
    [3] Change entry
    [4] View entry
    [5] Exit

    After the user has selected a valid entry, when the function has completed, ask the user if he or she would like to see the menu again. If an invalid ebtry is entered, the program should print:

    Invalid entry, try again.

    The menu will be redisplayed

  3. Create a submenu under View entry in the lookup script. The user will be asked if he or she would like to view specific information for a selected individual:

    a) Phone
    b) Address
    c) Birthday
    d) Salary

  4. Add the onintr command to your script using a label. When the program starts execution at the label, any temporary files will be removed, the user will be told Good–bye, and the program will exit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.228.138