Updated April 10, 2023
Introduction to Shell Script Usage
At the beginning of understanding what usage functionality primarily is, let us look at an analogous situation to get a real feel of this topic. In any other programming language in case we use in built functions in our code, we go through the documentation to see all the different mandatory parameters we need to incorporate to successfully run the function. In case of non-availability of some number of parameters, we get a message saying the parameters are missing. For example, in a programming language suppose there is a function to add 2 numbers. Now, as a user, if I send only one operand then the function will throw an error because it will be unable to clearly understand or interpret the other operand on which the addition would be operated. This calls for a usage message and the role explained above is exactly what usage functionality delivers in case of shell script.
How to Implement Usage Script in Shell?
Here we discuss how we implement usage script in our program. For this, we would need a complete understanding of some other functionalities like switch case commands, if condition, and operators which are other articles we have already written about in great detail. The following 2 steps are critical to be followed in case one needs to set up a usage functionality and they are:
- One would need to verify the number of arguments that needs to be passed to the script. The requirement is that in the first check if the number of parameters is less than the ones that are mandatorily required, we may display the usage message at that instant itself. For example, in the case discussed above, if the number of operands is less than 2 for an additional function, we can immediately issue a usage message.
- In case of the number of arguments match the number of a mandatory number of parameters required, but the type of argument doesn’t match what we require for the function can again be immediately issued a usage message. For example, in addition, function both the operands should be an integer, in case one of them fails to be, the usage message is issued.
Now, we eventually want to understand avenues where this functionality is used. Let us look into them in detail now.
- The first usability we see is to have a script that is properly documented for public use. In case of the script being used by someone for the first time or after a long time, one might have a tendency to forget things, thus having a usage message will go in hand a long way.
- The next usability we see is checking for a user in a database. In case of the username not existent, one can issue a usage message saying that the username doesn’t exist or maybe if there is no username passed, one can respectively quote the message that the username needs to be passed.
- In case, if someone inputs arguments to get help on the documentation perspective of the script, the user might type for example, “–help”, and in these case, we would be expected to open up the help file, for the user so that they can glance through the documentation of the script.
There are even other small places in a need basis where one can use usage functionality and exploit the underlying intention of using the same. Again, like any other functionality, once this functionality is also paired with for loops, if conditions, etc. can bring wonders to the code you would be working on. Now we would look into pseudocode for usage functionality and understand the underlying aspects of the same before we move to an actual example.
#Check for number of arguments supplied in the command line
If… Then… Else loop along with an appropriate exit code and usage message
#Check for data type match for all arguments passed
If… Then… Else loop along with an appropriate exit code and usage message
#Check for access privilege of the user who is running the script.
If… Then… Else loop along with usage message signifying that either the user is an authorized one or exiting the code with appropriate exit code for unauthorized access or in some cases even block the entire code execution all together, if this part of code is a part of a bigger code.
Example on Shell Script Usage
In the below example we would try to recreate a real life programming scenario and try to use all the functionality for usage in the shell script.
(learnUsage.sh)
Code:
#Check the number of arguments passed in the command line
usage_print() {
echo "Either input 2 arguments or use --help_needed or -? to refer documentation"
echo -e "\nUsage: ./learnUsage.sh [argument1] [argument 2]\n"
}
numArg=$#
if [ $numArg -le 1 ]
then
#Check if the argument is --help_needed or -?
if [[ ( $# -eq 1 ) && ( $1 == "--help_needed") || $1 == "-?" ]]
then
echo "Link for Help Document: <Provide the link here>"
exit 0
fi
usage_print
exit 81
fi
echo "The next set of logic for rest of the problem statement enters here!"
In the above code, the flow of code is to judge if the number of arguments is less than or equal to 1, in such case we would check if the number of argument is one and it is either –help or -h, we would echo the required usage message, in case it is not we would mention how this code needs to be used. If all the arguments are present, we would echo that we are good to proceed for next steps. You would notice the exit status, which will be mentioned when the code runs in the background and the code exists. All the outputs will be clear in the output tab!
Output:
Conclusion
In the ending note, we would like to mention the extensive use of the usage functionality to bring about a livelier shell script where it will help out the fellow users in the long term! You can also play around with username and password with keeping the username in a file and check if the username is available, you can proceed to the next steps, otherwise exit the code altogether. It will be fun to do that exercise!
Recommended Articles
We hope that this EDUCBA information on “Shell Script Usage” was beneficial to you. You can view EDUCBA’s recommended articles for more information.