The Bash Shell Lab Exercises

Lab 1—First Script

  1. Write a script called greetme that will:

    1. Contain a comment section with your name, the name of this script, and the purpose of this script.

    2. Greet the user.

    3. Print the date and the time.

    4. Print a calendar for this month.

    5. Print the name of your machine.

    6. Print the name and release of this operating system, (cat /etc/motd).

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

    8. Print all the processes root is running.

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

    10. Print your disk usage (du).

    11. Use the id command to print your group ID.

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

    13. 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? Why do you need this line?

Lab 2—Command Line Arguments

  1. Write a script called rename that will take two arguments: the first argument is the name of the original file and the second argument is the new name for the file.

    If the user does not provide two arguments, a usage message will appear on the screen and the script will exit. Here is an example of how the script works:

    $ rename
    								Usage: rename oldfilename newfilename
    $
    $ rename file1 file2
    								file1 has been renamed file2
    								Here is a listing of the directory:
    								a file2
    								b file.bak
    							
  2. The following find command (SunOS) will list all files in the root partition that are larger than 100K and that have been modified in the last week. (Check your man pages for the correct find syntax on this system.)

    								find / –xdev –mtime –7 –size +200 –print
    							
  3. Write a script called bigfiles that will take two arguments: one will be the mtime and one the size value. An appropriate error message will be sent to stderr if the user does not provide two arguments.

  4. If you have time, write a script called vib that creates backup files for vi. The backup files will have the extension bak appended to the original name.

Lab 3—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 his or her age (use expr).

    4. Ask the user's login name and print his or her user 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 -x and -v options for debugging your script. How did you use these commands? How do they differ?

Lab 4—Conditional Statements

  1. Write a script called checking that will:

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

    2. Will test to see if a command line argument was provided.

    3. Will 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."
      									
  2. In the lookup script, ask the user if he or she would like to add an entry to the datafile. If the answer is 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 him or her the line preceded by the line number.

Lab 5—Conditionals and File Testing

  1. Rewrite checking. After checking whether the named user is in the /etc/passwd file, the program will check to see if the user 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."
    							
  2. Use the let command to evaluate a set of grades. The script will ask the user for his or her numeric grade on an examination. ( Use declare -i ). The script will test that the grade is within a legal range between 0 and 100. If not, the program will exit. If the grade is within the legal range, the user's letter grade will be displayed, e.g., You received an A. Excellent! The range is:

    A (90-100) B (80-89) C (70-79) D (60-69) F (Below 60)

  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. Add a menu to the lookup script to resemble the following:

    [1] Add entry.
    [2] Delete entry.
    [3] View entry.
    [4] Exit.
    

    You already have the Add entry part of the script written. The Add entryy 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.

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

    The Delete part of the script should first check to see if the entry exists before trying to remove it. If it does, 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.

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

Lab 6—The Case Statement

  1. The ps command is different on BSD (Berkeley UNIX) and System 5 (ATT UNIX). Linux uses the BSD options to ps. On System 5, the command to list all processes is:

    								ps –ef
    							

    On Linux, the command is:

    								ps aux
    							

    Write a program called systype that will check for a number of different system types. The cases to test for will be:

    AIX
    LINUX
    HP–UX
    SCO
    OSF1
    ULTRIX
    SunOS (Solaris / SunOs)
    OS
    

    Solaris, HP–UX, SCO, and IRIX are ATT-type systems. The rest are BSDish.

    The version of UNIX you are using will be printed to stdout. The system name can be found with the uname -s command or from the /etc/motd file.

  2. Write a script called timegreet that will:

  3. Provide a comment section at the top of the script, with your name, the date, and the purpose of the program.

  4. Convert the following program to use the case command rather than if/elif.

    #!/bin/bash
    # Comment section
    you=$LOGNAME
       hour=$( date +%H )
    echo "The time is: $( date +%T )"
     if (( hour > 0 && hour < 12 ))
    then
       echo "Good morning, $you!"
    elif (( hour == 12 ))
    then
       echo "Lunch time!"
    elif (( hour > 12 && hour < 16 ))
    then 
       echo "Good afternoon, $you!"
    else
       echo "Good night, $you. Sweet dreams."
    fi
    

Lab 7—Loops

Select one of the following:

  1. Write a program called mchecker to check for new mail and write a message to the screen if new mail has arrived.

    1. The program will get the size of the mail spool file for the user. (The spool files are found in /usr/mail/$LOGNAME on ATT systems and /usr/spool/mail/$USER on Linux and UCB systems. Use the find command if you cannot locate the file.) The script will execute in a continuous loop, once every 30 seconds. Each time the loop executes, it will compare the size of the mail spool file with its size from the previous loop. If the new size is greater than the old size, a message will be printed on your screen, saying Username, You have new mail.

    The size of a file can be found by looking at the output from ls -l, wc -c or from the find command.

  2. Write a script that will:

    1. Provide a comment section at the top of the script, with your name, the date, and the purpose of the program.

    2. Use the select loop to produce a menu of foods.

    3. Produce output to resemble the following:

    1. steak and potatoes

    2. fish and chips

    3. soup and salad

      Please make a selection. 1

      Stick to your ribs.

      Watch your cholesterol.

      Enjoy your meal.

    1. steak and potatoes

    2. fish and chips

    3. soup and salad

      Please make a selection. 2

      British are coming!

      Enjoy your meal.

    1. steak and potatoes

    2. fish and chips

    3. soup and salad

      Please make a selection. 3

      Health foods…

      Dieting is so boring.

      Enjoy your meal.

  3. Write a program called dusage that will mail a list of users, one at a time, a listing of the number of blocks they are currently using. The list of users will be in a file called potential_hogs. One of the users listed in the potential_hogs file will be admin.

    1. Use file testing to check that potential_hogs file exists and is readable.

    2. A loop will be used to iterate through the list of users. Only those users who are using over 500 blocks will be sent mail. The user admin will be skipped over (i.e., he or she does not get a mail message). The mail message will be stored in a here document in your dusage script.

    3. Keep a list of the names of each person who received mail. Do this by creating a log file. After everyone on the list has been sent mail, print the number of people who received mail and a list of their names.

Lab 8—Functions

  1. Rewrite the last program, systype, as a function that returns the name of the system. Use this function to determine what options you will use with the ps command in the checking program.

  2. The ps command to list all processes on ATT UNIX is:

    								ps –ef
    
    							
  3. On Linux/BSD UNIX, the command is:

    								ps –aux  or ps aux[9]
    							

    [9] Using the leading dash with Linux will produce a warning. See the man page.

  4. Write a function called cleanup that will remove all temporary files and exit the script. If the interrupt or hangup signal is sent while the program is running, the trap command will call the cleanup function.

  5. Use a here document to 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
    

    Write a function to handle each of the items in the menu. After the user has selected a valid entry, and the function has completed, ask if the user would like to see the menu again. If an invalid entry is entered, the program should print:

    								"Invalid entry, try again."
    
    							

    and the menu will be redisplayed.

  6. 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:

    1. Phone

    2. Address

    3. Birthday

    4. Salary

  7. Use the trap command in a script to perform a cleanup operation if the interrupt signal is sent while the program is running.

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

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