In: Computer Science
In Linux (Ubuntu), write a script to check command arguments (3 arguments maximum). Display the argument one by one. If there is no argument provided, remind users about the mistake. If there is an easy way to use a loop to get all arguments, use it?
a. Display the source code in an editor (#4-1)
b. Execute your script in the terminal, and display the command and the result (#4-2)
Hi,
Thank you for the opportunity to help you with your assignment.
The following is the code for your requirements as in the instructions.I have made sure to include all that is necessary for you to complete this assignment.
Please ensure you run "chmod +x argsprinter.pl" before you run the script.
I have added comments in the code for your reference. The output and screenshot is at the end of this answer.
If you have any queries, need changes in the code or need help in following the code, leave me a comment.
I will reply within reasonable time.
If this answer helps you with your assignment, do upvote
it.
Your vote will motivate me to work better.
The following is the code
//File: argprinter.pl
#!/usr/bin/perl
#counter used to display the argument number
my $arg = 1;
#Check if any argument is passed to this script, else exit
if ($#ARGV <= 1 ) {
print "You need to pass arguments to this script.\n";
exit;
}
#Iterate and print each argument
foreach my $a(@ARGV) {
print "Arg # $arg : $a\n";
$arg++;
}
Here is the source in the editor
And
finally, this is the output when the code is executed
//Output: