Commenting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Commenting
# 1  
Old 11-12-2004
Commenting

How can i comment multiple lines in unix ..............shell script.
# 2  
Old 11-12-2004
Not possible ;
# 3  
Old 11-12-2004
Other than putting your # or ; at the beginning of each line I'm not sure it can be done.

One thought I had is what happens if you use the \ line continuation character. Like this . . .

#my comment starts here \
but is this still considered a comment? \
I'm not sure.

Give that a try in a harmless test script to find out, but it might work.
# 4  
Old 11-13-2004
In bourne, ksh, bash, etc, everything between a # and the newline is ignored. This includes a terminal backslash. But you can have as many comment lines as you want. Just start each one with a #.
# 5  
Old 11-13-2004
There is a kludgy way of doing it
Code:
echo "Testing"
: '
echo "This should be ignored"
echo "And so should this"
'
echo "This shouldn't"

i.e. everything between : ' and ' will be ignored. This will fall over if you have single quotes inside the "commented" area.

Cheers
ZB
# 6  
Old 11-13-2004
Quote:
Originally posted by zazzybob
There is a kludgy way of doing it
That actually has side effects that may not be obvious. In the original Bourne shell that was the only way to comment code. If you look at the original csh man page you see:
Quote:
If neither of the above conditions holds, the kernel cannot overlay the file and the execve( ) call fails (see exec(2)). The C shell then attempts to execute the file by spawning a new shell, as follows:

* If the first character of the file is a #, a C shell is invoked.
* Otherwise, a Bourne shell is invoked.
Bill Joy, the author of csh, explained that he figured a script would always start with a comment and csh used # for comments while sh used : for comments.

The trouble was that : didn't work that well. So Steve Bourne copied the # idea from csh and added it to the Bourne shell. If you follow ZB's syntax exactly, you will sidestep most of the problems that arose from the : comment syntax. These were mostly unintended fd effects like this:
: > /some/important/file
or unintended variable effects like this:
: ${variable:=garbage}

But : is a command and it will succeed. This will set $? and I don't see a way to avoid that. This will work unexpectedly with most sh descendants:

Code:
if [ 0 -eq 1 ]
: a harmless comment
then
          echo  whoa
fi

Also there is a performance consideration. The : command cannot be ignored like a # comment can. The arguments to : must be evaluated in case those side effect are desired.
# 7  
Old 04-16-2007
Use Here DO statement

Another way to comment out multiple lines of code is the "here document"
way:

: << --END-COMMENT--
your comment here
--END-COMMENT--

This way, there is no restriction with single quotes. Note that any delimiter
can be used in place of "--END-COMMENT--".
I have tested it It works!!!!

Last edited by rgupta78; 04-16-2007 at 03:59 PM.. Reason: Testing
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Commenting multiple lines

Hi, Can anyone let me know how to comment multiple lines in VI editor? Many thanks. Regards, Venkat. (3 Replies)
Discussion started by: venkatesht
3 Replies

2. Shell Programming and Scripting

commenting out lines between two delimiters

Hi All, I am struggling to get my head around the following issue. I am having to comment out lines between two delimiters by placing an asterix in position 7 but retain all lines in the file and in the same order. so for example a file containing: ... ... DELIM1 ... ... DELIM2... (2 Replies)
Discussion started by: Bruble
2 Replies

3. Shell Programming and Scripting

Commenting lines in a file using SED

Hi, I need to comment the below lines in a file using sed.These are the few lines of the jsp file that need to be commented. if(top.location != location){ top.location.href = location.href; } Using the below command two lines can be commented: if(top.location != location){ ... (9 Replies)
Discussion started by: meetu
9 Replies

4. Shell Programming and Scripting

Commenting lines

Hi can any body pls help me : I have a file Which Content is like following: p3:s1234:powerfail:/usr/sbin/shutdown -y -i5 -g0 >/dev/msglog 2<>/dev/msglog ca:3:respawn:/opt/GoldWing/currentPM/local/critagt > /dev/msglog 2<>/dev/msglog ca:3:respawn:/opt/GoldWing/currentPM/local/startcia.sh... (2 Replies)
Discussion started by: Aditya.Gurgaon
2 Replies

5. Shell Programming and Scripting

Commenting contab from a script

Dear All, I have many cron entries and want to comment a particular cron entry from a script. Ex- Suppose I have the below cron entries: # DO NOT EDIT THIS FILE - edit the master and reinstall. #Cron entries for Jboss server 1 0 23 * * * /usr/bin/echo 0 23 * * * /usr/bin/asdg_count.sh 0 23 *... (5 Replies)
Discussion started by: avishek007
5 Replies

6. Shell Programming and Scripting

Commenting xml file lines

Hi , I have a XML file like this <dependency> <groupId>fr.xxxx.portail.ear</groupId> <artifactId>_xxxEAR</artifactId> <version>1.0.0-20xxxxx.xxxxx-x</version> <type>ear</type> </dependency> I need to comment single/multiple lines from XML file. How can i... (6 Replies)
Discussion started by: scorpio
6 Replies

7. Shell Programming and Scripting

commenting

can we use "---------" for commenting......... (2 Replies)
Discussion started by: simmijaswal
2 Replies

8. Shell Programming and Scripting

Commenting a Line In a File

HI all I am working on a script, few details are as follows. I have one properties File and one script. The property file contains the JOBID which are to be executed and the Script runs these jobs ONE by ONE. After completing the JOB I need to comment that job in the property File. This is the... (3 Replies)
Discussion started by: Prashantckc
3 Replies

9. UNIX for Dummies Questions & Answers

Commenting lines

How to comment a set of lines in a script? we use # to comment a single line , is there ant other cmd to comment a block? (2 Replies)
Discussion started by: rolex.mp
2 Replies

10. Shell Programming and Scripting

commenting more then 1 line

Hi all, I m new for linux ... but now i will do this continuously. So my question is how to comment more then 1 line i dont wanna put # to each line.. my script is too big... or u can tell me that how can i add # to sellected lines means if i wanna put # from line number 10 to 20 then how... (3 Replies)
Discussion started by: ajayyadavmca
3 Replies
Login or Register to Ask a Question