Linux Topic
   >  Shell Scripts
   >  Using Comments
   >  Passing Arguments to Scripts
   >  Using Conditional Statements
   >  Using Loop Statements
   >  Reading and Writing Files
   >  Script Permissions
   >  Cron and Scheduling
   >  A Scripting Example
   >  Other Scripting Languages

 

Commenting your Code

Using Comments

Comments are lines which are ignored by the shell script interpreter and are not executed. So, they do nothing then?Not at all! Comments can be your best friends! Programmers use comments (-well, some of them do anyway) to write a note to themselves (-or others, reading the code) about some important feature. There are a million different uses for comments, but these are a few of the ones I personally use:

  • To state, at the top of the script, what it's purpose is. This can save much time later, when you're looking for "that script I wrote to do x"

  • To state, at the top of the script, what any parameters are. This is really useful when you come to run the thing in six month's time and can't figure out why it's not working the way you thought

  • If more than one person is working on the machine, you might want to state who the author was

  • You might write a really complex piece of code that - and when it was created or updated

  • To mark the start of a particular block of code (-e.g. subroutines, input, output, etc)

  • To explain what a complex section of code is doing

  • If you had to put a kludge / workaround in the code, why you needed to do this. For example, it might be because you couldn't get command X to work at the time, but if you revisit the code later you might have figured it out

  • Any limitations of the code: for example - "this script work work if X is in the wrong format", etc

In short, comment liberally: they can all be removed later if necessary! In shell scripting, any line starting with a hash (#) sign is treated as a comment. Additionally, if you add a "#" character partway through any line, then the rest of that line will be interpreted as a comment. These are both valid comments:

rm /tmp/myFile/*.txt # remove any old output hanging around
# script was created by Fred Bloggs on 21st September 2000

HomeSite IndexDesktop GuideServer GuideHints and TipsHardware CornerVideo SectionContact Us

 sitelock verified Firefox Download Button