How to execute the rest of the code after commenting multiple lines?

Hi,

As I have seen in this forum how to comment multiple lines in the script, but it does not work properly for me.
It is blocking the code but it does not execute the rest of the codes.

This is my code

#! /usr/bin/ksh
month='date +"m%"'

: << Comments Block
if [ "$month -eq 3 ] || [ "$month -eq 6 ]
then
echo "inc = 1"
else
echo "inc = 2"
fi
-- End Comments Block

case $month in
3|6|9|12) echo "Yes";;
*) echo "No";;
esac
echo "This is the last line"

My query is after the comments block, it does not execute the case statement. Can you pls explain it?

I strongly urge you to use the traditional # method of commenting out shell
script code.

That being said, the problem with your block comment trick is your invalid
syntax for here documents.

: <<COMMENTBLOCK
if [ "$month -eq 3 ] || [ "$month -eq 6 ]
then
echo "inc = 1"
else
echo "inc = 2"
fi
COMMENTBLOCK

Hi Murphy,
Thanks for your reply.

After trying with your sample code, it does not work for me. I dont know what might be the problem.

It looks like there is some confusion here.
What fpmurphy is trying to say is that you need to comment out the lines (if needed) that don't need to be executed using # sign, like this:

#  :<<COMMENTBLOCK
# if [ "$month -eq 3 ] || [ "$month -eq 6 ]
# then
# echo "inc = 1"
# else
# echo "inc = 2"
# fi
# COMMENTBLOCK

In case you are trying to use a HERE document, as suggested you need to get rid of the colon : in front of the sign <<, and no space at the final COMMENTBLOCK :

So if you need those lines commented out you need to use the first code.

If you are trying to achieve something else with your script, then you need to look in your script and change it accordingly, or post here what are you trying to achieve.

Your quotes are open.

Hi.

A few comments. First, there is a "quoted" string feature in the shells for HERE documents. The use prevents evaluation of everything up to the closing string, including unclosed quotes, variable issues, etc.

Second, ksh does a good job of diagnosing a missing, closing, matching HERE document string -- bash (2 & 3) simply stop.

A ksh example:

#!/usr/bin/env ksh

# @(#) s1       Demonstrate quick method to render code block inoperable.

set -o nounset
echo

debug=":"
debug="echo"

## The shebang using "env" line is designed for portability and
#  demonstrations. For higher security, use:
#
#  #!/bin/ksh -

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version ksh

echo
echo " Commands before commented-out block."

: <<'EOF'
garbage
junk
worthless
utterly without merit
" crud inside of double quotes "
' debris inside single quote '
" mess inside unmatched double-quotes
' detritus inside an unmatched single-quote pair
...
and so on.
EOF

echo
echo " Commands after  commented-out block."

echo
echo " Failed block quote due to unmatched HERE stings:"

echo
echo " Commands before commented-out block."

: <<'MISSING'
more junk
OOPS!

echo " Commands after  commented-out block."

exit 0

producing:

% ./s1

(Versions displayed with local utility "version")
pdksh 5.2.14 99/07/13.2

 Commands before commented-out block.

 Commands after  commented-out block.

 Failed block quote due to unmatched HERE stings:

 Commands before commented-out block.
./s1[53]: here document `MISSING' unclosed

I don't use ksh for reasons of availability (my shebangs almost always use simple "sh"), but in this case ksh is superior ... cheers, drl

Hi Rubin,

Thanks for your explanation. I will write exactly what I need

Like in C or in C++, we have multiple lines of comments start like

/*
bla bla bla
*/

Likewise, I need the same kind of comments in Shell scripts. Is it possible here ?

  1 \#! /usr/bin/ksh
  2 month=\`date \+"%m"\`
  3 echo $month
  4
  5 &lt;&lt;COMMENTBLOCk
  6 if [ "$month" -eq 3] || [ "$month" -eq 6]
  7 then
  8 echo "inc = 1"
  9 else
 10 echo " inc = 2"
 11 fi
 12 COMMENTBLOCK
 13
 14 case $month in
 15 3|6|9|12\) echo "Yes";;
 16 *\) echo "No";;
 17 esac
 18
 19 echo "This is the last line"

Query : I need to block the line number from 5 to 12 (except # option and line no from 14 to 19 should execute as usual. Can you pls give the solution for it?

Many of Unix Experts are replied for my query. Many thanks to you.

Thanks
Yamini

Hi DRL

This is my mistake.

This should work....

cat >/dev/null <<IGNORE
asas
das
d
as
das
d
as
IGNORE

Hi DRL,

Thanks for you explanation. But for me it is bit confusing, since i am not aware of the sh. i am using ksh only. Still i will try to understand this code.

Thanks
Yamini.

Hi Porter,

What exactly does this code? I tried and I didnt get anything. Can you pls explain breifly for me?

Thanks
Yamini.

:rolleyes:

You were not supposed to get anything - that was the idea.

Anyway, read this:

SHELLdorado - Shell Tips & Tricks (Programmer)

Hi Yamini,

I still believe there is some confusion in your explanation, because you haven't explained what you are trying to do with your script, give a certain message, execute a command, etc ...

If you only need to ( block) comment lines 5 - 12 out, then that's pretty easy, either use # sign as suggested, or porter's way, or the best way : delete lines 5 - 12 .

It's simply not enough saying script doesn't work. First what are you trying to achieve with your script, besides the point of blocking lines 5 -12 ? Post your script if needed.

I got no problem running happily the following script in my linux machine,( no ksh available, should be the same though ! ).

#!/bin/bash

month=`date +"%m"`
echo "This is $month month"

echo "
        3,6,9,12 ) Yes, time to go   on vacations 
              
        other months ) No,  I'd better go on vacations again !"
              
echo "Enter month"
read month




case $month in
 3|6|9|12) echo "Yes, time to go on vacations"

    ;;

 *) echo "No, I'd better go on vacations again !"
   
  ;;

 esac

 echo "This is the last line"