Comment syntax

2.6 Summarize scripting basics for server administration.

📘CompTIA Server+ (SK0-005) 


In scripting, a comment is a line or part of a line in a script that is ignored by the computer when the script runs. Its purpose is to explain the code to anyone reading it, including yourself or other IT staff. Comments make scripts easier to maintain and understand. They do not affect the execution of the script.


Why Comments Are Important in IT Scripting

  1. Documentation: Helps you and others understand what a script does.
    • Example: Explaining why a backup script deletes files older than 30 days.
  2. Troubleshooting: Temporary comments can be used to disable parts of the code to test scripts safely.
    • Example: Disabling a line that restarts a server while testing.
  3. Collaboration: Multiple admins or developers can understand the logic without reading every single line.

Comment Syntax by Script Type

Different scripting languages use different symbols for comments.


1. Bash (Linux/Unix Shell Scripts)

  • Single-line comment: Use #
#!/bin/bash
# This script checks disk usage
df -h
  • Notes:
    • Anything after # on the same line is ignored.
    • Used heavily for explaining commands in server maintenance scripts, like cron jobs.

2. Batch Scripts (Windows Command Line)

  • Single-line comment: Use REM or ::
REM This batch file cleans temporary files
:: Another way to write comments
del /q C:\Temp\*.*
  • Notes:
    • REM is most common.
    • :: is shorter but cannot be used inside a command block (like IF statements).

3. PowerShell

  • Single-line comment: Use #
# This script lists running services
Get-Service
  • Multi-line comment: Use <# ... #>
<#
This script:
- Lists running services
- Exports the list to a CSV
#>
Get-Service | Export-Csv C:\Services.csv
  • Notes:
    • Very useful for documenting large scripts or complex server configurations.

4. Visual Basic Script (VBS)

  • Single-line comment: Use ' (apostrophe)
' This script shuts down the server after 10 minutes
MsgBox "Server will shut down in 10 minutes"
  • Notes:
    • No built-in multi-line comment, so each line needs '.

Best Practices for Commenting in Server Scripts

  1. Be clear and concise: Don’t write essays. A few words explaining purpose are enough. # Delete temp logs older than 30 days
    find /var/log -type f -mtime +30 -delete
  2. Keep comments up to date: Outdated comments confuse admins.
  3. Use comments to explain “why,” not “what”: The code shows what is happening; comments explain why it’s done.
  4. Use multi-line comments for complex operations: If a script has several steps (like a backup or patching process), multi-line comments help structure it.

Exam Tips

  • Know the comment symbols for each script type:
    • Bash / PowerShell → #
    • PowerShell multi-line → <# ... #>
    • Batch → REM or ::
    • VBS → '
  • Understand why comments are used: readability, documentation, and troubleshooting.
  • You don’t need to memorize complex examples, but be able to identify comments in a snippet of code.
  • Sometimes, the exam may give a line like # rm -rf / and ask what it does: the # means it’s ignored and will not delete files.

Key Takeaway: Comments are for humans, not computers. Proper commenting ensures server scripts are readable, maintainable, and safe to run.

Buy Me a Coffee