Purchase Solution

How to use command line options in Bash Shell Script

Not what you're looking for?

Ask Custom Question

Create a script that will allow the user to enter one of several choices from the command line. The specific requirements are as follows:

1. Copy one file to another. The user enters the source and destination file to copy to and from. The script shall handle the following cases:
a. Source file name specified does not exist. Script shall display an error message and terminate. Optionally, the script can display an error message and prompt the user to specify the source file name to copy from.
b. Source and destination files both exist. Script shall prompt for permission to overwrite the destination file. If permission is denied, program will display message and terminate.
c. Source file specified with no destination file specified. Script shall display error message and terminate or optionally, it can prompt the user to enter the file name to copy to.

2. Remove a file if it exists. The user provides the name of the file to remove or delete. Display a message indicating success or failure back to the user and terminates. The script shall handle the following conditions:
a. If the user enters the remove file option but no file name specified the script shall display an error message and terminate or, optionally, display an error message and prompt the user for the file name to remove and remove the file if it exists.
b. If the specified file does not exist the script shall display an error message and terminate.

3. Display the current day of the week. The script shall allow the user an option to display the day of the week.

4. Display the month's calendar. The script shall allow the user an option to display the month's calendar.

5. The script should be named commands.sh and accept options in the command line as either -r or -R for removing a file, -c or -C for copying a file, -d or –D to display the day of the week and –m or –M to display the month's calendar.

6. If the user enters command.sh without any options the script shall display the usage examples as shown below.

7. The script shall include the option and the necessary positional parameters information to perform the options as follows:

Copy a file:
prompt$ ./commands.sh –c sourceFile destinationFile
prompt$ ./commands.sh –C sourceFile destinationFile

Remove a file:
prompt$ ./commands.sh –r fileName
prompt$ ./commands.sh –r filename

Display day of week:
prompt$ ./commands.sh –d
prompt$ ./commands.sh –D

Display month's calendar:
prompt$ ./commands.sh –m
prompt$ ./commands.sh –M

Purchase this Solution

Solution Summary

The Shell script gives a practical example of how to
A. Use command line parameter (e.g., -d, -m, etc.) for different purposes within the same script
B. Use functions inside a shell script to easily create blocks of code which serves a particular purpose
C. How to write documentation for shell script
D. How to write functions for common tasks, such as remove files and copying files

Solution Preview

Documentation for the attached script (commands.sh) is given below.

NAME

Commands.sh - Multiple shell commands script

SYNOPSIS

Copy a file:
./commands.sh -c sourceFile destinationFile
./commands.sh -C sourceFile destinationFile

Remove a file:
./commands.sh fileName
./commands.sh filename

Display day of week:
./commands.sh -d
./commands.sh -D

Display month’s calendar:
./commands.sh -m
./commands.sh ...

Purchase this Solution


Free BrainMass Quizzes
C# variables and classes

This quiz contains questions about C# classes and variables.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.