Have you had someone ask or tell you to document your code? If you haven't, then you are rare, indeed. It's a constant struggle to get sysadmins and developers to document their code and scripts properly. Everyone, it seems, wants you to do it, but no one shows you how. This article demonstrates how to document your code and scripts.
Documentation can be tedious, annoying, and less than gratifying when you realize that a very limited audience will ever read or refer to your embedded commentary. It's possible that only you and one other person might ever see your literary gems, but that isn't really the point. The point is that even you, as the programmer, will probably need to refer to this documentation in the future. You will need to recall what a particular code snippet's purpose was, or you might need to know how to reproduce a special loop for another project. You won't remember six months from now what the logic or the purpose was behind each bit of code.
Documentation fixes that.
There are no specific rules for documenting code, but there are some helpful guidelines that will make future you or a colleague very happy. In a script or program code, documentation is referred to as "comments." I refer to documentation and comments interchangeably. For this article, I use the # symbol to identify a line of commented text because it is the standard for Linux shell scripts. Other languages have their own symbols to identify comments.
Remember that the first line of a shell script looks like a comment, but it isn't. It's the shebang line that defines the shell in which the script will run. #!/bin/bash, for example.
Under the shebang line, you can begin your documentation. Begin with the creation date, author's name, and the script's overall purpose or problem it solves.
#!/bin/bash
# Date: 07/22/2020
# Author: Ken Hess
# This script parses the access log, strips out IP addresses that don't match the 192.168.1.0/24 pattern, and enters them
# into the /etc/hosts.deny file.
There you have the function of the script, who created it, and when it was created. The next step is to supply usage instructions and whether the script requires any arguments, switches, or other input.
# Usage: $ ./documentation.sh
The next part is the documentation for the script's code snippets. You have to document each step separately. Comments always precede the code they document.
# Parse the access log and extract IP addresses that don't match 192.168.1.0/24.
<code to read the access log and extract IP addresses>
# Write the IP addresses to a file (/tmp/addresses.txt)
<code to write output to a file>
# Read each line from /tmp/addresses.txt and enter them into /etc/hosts.deny
<code to read /tmp/addresses.txt and enter them into /etc/hosts.deny>
# Remove /tmp/addresses.txt
<code to remove /tmp/addresses.txt)
To revise this script later, add the author if different from the original, the modification date, and the modified code with the corresponding date.
# Modified: 07/29/2020
# Modified by: Tyler Carrigan
# 07/29/2020 - Modified to add a check for IP addresses in /etc/hosts.allow prior to adding them to the /etc/hosts.deny file.
<code to compare /etc/hosts.allow with /tmp/addresses.txt)
# 07/29/2020 - Modified to remove addresses from /tmp/addresses.txt that match in /etc/hosts.allow so that they don't get denied access.
<code to remove addresses that match /etc/hosts.allow and rewrite /tmp/addresses.txt>
Now that you've seen all of the parts, here's what the script looks like all put together and in the correct order.
#!/bin/bash
# Date: 07/22/2020
# Author: Ken Hess
# This script parses the access log, strips out IP addresses that don't match the 192.168.1.0/24 pattern, and enters them
# into the /etc/hosts.deny file.
# Modified: 07/29/2020
# Modified by: Tyler Carrigan
# Modified to prevent IP addresses in /etc/hosts.allow from being duplicated in /etc/hosts.deny.
# Usage: $ ./documentation.sh
# Parse the access log and extract IP addresses that don't match 192.168.1.0/24.
<code to read the access log and extract IP addresses>
# Write the IP addresses to a file (/tmp/addresses.txt)
<code to write output to a file>
# 07/29/2020 - Modified to add a check for IP addresses in /etc/hosts.allow prior to adding them to the /etc/hosts.deny file.
<code to compare /etc/hosts.allow with /tmp/addresses.txt)
# 07/29/2020 - Modified to remove addresses from /tmp/addresses.txt that match in /etc/hosts.allow so that they don't get denied access.
<code to remove addresses that match /etc/hosts.allow and rewrite /tmp/addresses.txt>
# Read each line from /tmp/addresses.txt and enter them into /etc/hosts.deny
<code to read /tmp/addresses.txt and enter them into /etc/hosts.deny>
# Remove /tmp/addresses.txt
<code to remove /tmp/addresses.txt)
Remember that scripts, code, and documentation are editable entities, and they have to be maintained and updated as needs require. As you can see from this example, the second author made a significant and much-needed change. The script worked before, but the changes made it better. The changes would have been perceived as invasive had the second author not made a note of the modifications.
Documentation is necessary and worth the time and effort to do. And no matter how great your memory is, in a few months or a year from now, you won't remember the intent or the reasoning behind some of the scripts you create. Document. Document. Document.
[ Download now: A sysadmin's guide to Bash scripting. ]
저자 소개
Ken has used Red Hat Linux since 1996 and has written ebooks, whitepapers, actual books, thousands of exam review questions, and hundreds of articles on open source and other topics. Ken also has 20+ years of experience as an enterprise sysadmin with Unix, Linux, Windows, and Virtualization.
Follow him on Twitter: @kenhess for a continuous feed of Sysadmin topics, film, and random rants.
In the evening after Ken replaces his red hat with his foil hat, he writes and makes films with varying degrees of success and acceptance. He is an award-winning filmmaker who constantly tries to convince everyone of his Renaissance Man status, also with varying degrees of success and acceptance.
유사한 검색 결과
GPU 작업 시간의 가치 최대화: Red Hat OpenShift AI의 진행 상황 추적 기능
글로벌 선도 기업들이 AI에 대한 비전을 비즈니스 가치로 실현하는 방법
How Red Hat cleared IT debt for scalable AI
Virtualization Is (Still) King | Compiler
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
가상화
온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래