Question

In: Computer Science

Perl is a programming language that can be used on Linux. Write a Perl shell script...

Perl is a programming language that can be used on Linux.

Write a Perl shell script named phone.pl that prompts the user to enter first or last or any portion of person’s name, so that can be found the appropriate entry in the phone directory file called “phones”.

If the user tries to enter name as the argument on the command line, he/she will get a warning message “You need to provide name when prompted by this script!”

If the person’s entry does NOT exist in the file “phones” then it will be displayed the following message “Name NOT found in the phone directory file!” (where Name is the user’s input).

A text file list of names and information similar to the final output of sample run #2 has been provided by the teacher.

Sample Run #1:

INPUT: $ perl phone.pl Chad

OUTPUT: You need to provide name when prompted by this script!

Sample Run #2:

INPUT 1: $ perl phone.pl

OUTPUT 1: Enter a name to search for:

INPUT 2: Chad

OUTPUT 2: Brockman, Chad 920-213-0043 [email protected]

Sample Run #3:

INPUT 1: $ perl phone.pl

OUTPUT 1: Enter a name to search for:

INPUT 2: User

OUTPUT 2: User not found in the phone directory file

Solutions

Expert Solution

Here I have implemented Perl code as per your requirement, also I have attached the output of the code. Let me know in the comment section if have any query regarding the following code.

phone.pl

#!/usr/bin/perl

if($#ARGV!=-1)
{
    # Argument on the command line
    print("You need to provide name when prompted by this script!\n");
}
else
{
    
    print("Enter a name to search for: ");
    
    # take input
    $name=<>;
    
    chop($name);
    
    # open phones file
    open(DATA, "<phones") or die "Couldn't open file file.txt, $!";
    
    # keep track of 
    # name found or not
    $found=0;
    
    # loop over whole file
    while(<DATA>) {
        
        # the line which is separated by
        # space and store into an array
        my @spl = split(' ', $_);
        
        # Here we need to delete the last 
        # character because as per the given 
        # format the last character is ','
        # So we have to remove the last character
        chop(@spl[0]);
        
        # first name(first argument)
        $f_name=@spl[0];
        
        # second name(second argument)
        $l_name=@spl[1];
        
        # Compare the name
        if($f_name eq $name or $l_name eq $name)
        {
            $found=1;
            print($_);
            
            # the user found, So break the loop
            last;
        }
    }
    
    # if user not found
    if($found==0)
    {
        print("User not found in the phone directory file\n");    
    }
    
    close(DATA);
}

Output:


Related Solutions

Perl Programming Please write a simple to understand Perl script that computes compound interest, based on...
Perl Programming Please write a simple to understand Perl script that computes compound interest, based on input from user for P, n, r and t.
Perl Programming Write a Perl script that computes compound interest balance, A, based on input from...
Perl Programming Write a Perl script that computes compound interest balance, A, based on input from user for P, n, r and t.
LINUX In Linux command line write a shell script ex1.sh that uses IF THEN to Prompt...
LINUX In Linux command line write a shell script ex1.sh that uses IF THEN to Prompt the user to "Enter a number between 1 and 10". (Hint: Use the 'echo' and 'read' commands in the script. See the slide about the 'read' command) If the number is less than 5, print "The number is less than 5" (Hint: You will read input into a variable; e.g. read NUM. In the IF statement, enclose $NUM in quotes; e.g. "$NUM". Also, remember...
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string...
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string expressed as -rwxrwxrwx and prints out whether or not theobject is a directory, file or link. The string is read in from standard input. Convert String (Part 2) Modify the script so its able to print out the octal permission which the string represents along with the file type. This is in addition to part 1. Convert String (Part 3) For the script in...
Assisted Instruction: Has to be in PERL SCRIPTING LANGUAGE with NO SUBROUTINES Programming Exercise: Write a...
Assisted Instruction: Has to be in PERL SCRIPTING LANGUAGE with NO SUBROUTINES Programming Exercise: Write a program in PERL with NO SUBROUTINES to allow the user to pick a type of arithmetic problem to study. An option of 1 means addition problems only, 2 means subtraction problems only, 3 means multiplication problems only, 4 means division problems only and 5 means random mixture of all these types. (Computer- Assisted Instruction) The use of computers in education is referred to as...
Write shell script code that displays the process tree on a Linux machine. The process tree...
Write shell script code that displays the process tree on a Linux machine. The process tree should show the process identifier of each process. Those processes that have the same ancestor should be sorted by process identifier instead of by name. The process tree should show the full names of all threads, if any and available. Whenever the user identifier associated with a process differs from the user identifier associated with the parent of that process, the process tree should...
Linux Sign into your lab account Write a bash shell script that does the following: Print...
Linux Sign into your lab account Write a bash shell script that does the following: Print out a friendly welcome message Ask the user for the name of their favorite animal Grep that animal from a text file noises.txt Tell the user what that animal says (i.e. what noise does it make) If the animal does not exist ask the user what noise the animal makes Store that new information in noises.txt
Part 2 – Scripting Goals:  Write a bash script  Use linux shell commands within...
Part 2 – Scripting Goals:  Write a bash script  Use linux shell commands within your script  Provide user feedback  Use loops  Use conditionals Remember to use chmod +x to make your file executable! Each script is 5 points in the Specifications portion of the rubric. Don’t forget to maintain good standards and comments. Script 1 – Echo-back some information Write a script name hello.sh that will take the user’s first name as a command line...
Design two shell programs working on Linux (Ubuntu) Design a shell script program, 1) reading given...
Design two shell programs working on Linux (Ubuntu) Design a shell script program, 1) reading given only two integer numbers from command line arguments and computing their multiplication. If two integer numbers are not given, print “Wrong Input” on your screen. Note that, the number of arguments is known when the script runs. Take a screenshot showing your shell program and its execution step. Design a shell program to remove all the shell programming files ending with sh on your...
WRITE IN PERL Creating a Perl Product Directory - A product "id" will be used to...
WRITE IN PERL Creating a Perl Product Directory - A product "id" will be used to identify a product (the key) and the description will be stored as the value The program should print a menu asking you to "add" an entry, "remove" an entry, "lookup" an entry, "list" all entries, and "quit" to end The user will enter a number to select the menu item from above, 1, 2, 3, 4, or 5 respectively. Asks the user to enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT