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.
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...
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...
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...
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...
Linux Shell Prpgramming 1.Write a quiz-script which will provide some statistics about a quiz results (summarize...
Linux Shell Prpgramming 1.Write a quiz-script which will provide some statistics about a quiz results (summarize how many good answers and how many wrong answers were provided). The script should use up to 3 execution parameters. When executed with two parameters (let’s call it interactive mode), it should assume, that the first file contains a list of questions and the second file contains correct answers to these questions. In such execution case your script should display questions one by one...
(10pts) Write a shell script to display your name to the screen. (10pts) Write a shell...
(10pts) Write a shell script to display your name to the screen. (10pts) Write a shell script to take a directory as an argument and display the contents of that directory (10pts) Write a shell script that takes any command as a parameter and displays help on that command (e.g. the result of the execution of man <command>). (10pts) Write a shell script that requires two file names as parameters and copies content of one file into another without asking...
Write a complete shell script that first asks the user to enter a URL. The script...
Write a complete shell script that first asks the user to enter a URL. The script should read the URL into a variable named url. The shell script should then retrieve the file associated with the URL by using the curl command. The output of the curl command should be redirected to a file named html_file. The shell script should then use the grep command to search the file named html_file for the word manhattan. Finally, the shell script should...
Write a shell script (to run on the Bourne shell) called cp2.sh to copy all the...
Write a shell script (to run on the Bourne shell) called cp2.sh to copy all the files from two named directories into a new third directory. Timestamps must be preserved while copying files from one directory into another. Task Requirements Three directory names must be supplied as arguments to your script when it is executed by the user. In the beginning of your script you need to check that the first two directory names provided (e.g. dir1 and dir2) exist...
this is bash scripting. a. Write a shell script that adds an extension “.deb” to all...
this is bash scripting. a. Write a shell script that adds an extension “.deb” to all the files in a directory. b. Write a command sequence or a script to insert a line “GHIJKLM” at every 10th line of a file? c. Write a bash command or script to find all the files modified in less than 5 days and print the record count of each.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT