The implementation of the Shell programming help function

Source: Internet
Author: User
A program that often needs help explaining.
The ways to activate help instructions are described in two ways
1. The simplest way is to discriminate by the number of parameters. For example, displays the help description without any parameters.
2. The normal practice is that the command line is followed by the-h parameter.
Shell help can be written using a bunch of ECHO commands to output to the console, and better
Here Doc syntax, which shows that the document is neatly formatted and consistent with the output. Not a bunch of echoes and double quotes.
Here's an example:

1. Simple method
#!/bin/bash-
Set-o Nounset # Treat unset variables as an error
Help ()
{
Cat <<-EOF
DESC: This program is used to ....
Usage:./1.sh <filename>
Author:hjjdebug
License: ...
Eof
Exit 0
}
If [$#-lt 1] # cannot be used < Here is a numeric comparison, not a string comparison
Then
Help
Fi

2. Formal Methods
#!/bin/bash-
#set-o nounset # This option is off, otherwise it is not defined by the syntax error. Affect vision.
Help ()
{
Cat <<-EOF
DESC: This program is used to ....
Usage:./1.sh <filename>
Author:hjjdebug
License: ...
Eof
Exit 0
}

#这里通过判断 the existence of a discriminant, or through $ #判别, shift will change the value of both,
While [-N "$"]; Do
Case $ in
-h) help;; # function Help is called
--) Shift;break; # End of options
-*) echo "error:no such option $."; Exit 1;;
*) break;;
Esac
Done

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.