Using :<<cut / cut to comment out block of bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using :<<cut / cut to comment out block of bash script
# 1  
Old 08-20-2018
Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code.
Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines.

The "warning " is at last line of script.



Code:
 done < results
 169  echo "END read all positioning parameters" 
 170 pause
 171 }
 172
 173 :<<cut         ERROR HERE  
 174 #do
 175 #if [ "$(( $choice % 2))" -ne 0 ]; 
 176 # echo $choice
 177 #done 
 178  pause 
 179 while read choice



Code:
   	 	 	 	   pi@pi:/usr/bin $ sudo Z-5.sh
 + debug=true
 /usr/bin/Z-5.sh: line 3540: warning: here-document at line 173 delimited by end-of-file (wanted `cut')
 + :
 /usr/bin/Z-5.sh: line 173: % 2: syntax error: operand expected (error token is "% 2")
 pi@pi:/usr/bin $

# 2  
Old 08-21-2018
There is a utility called cut and I would not use this as the delimiter of a here document.
Does the closing line of the here document hold only the word cut, without any whitespaces or other characters?
# 3  
Old 08-21-2018
Yes, here is my working code

Code:
 calc_wt_size
 140 : <<cut
 141 x=1
 142 while [ $x -le 5 ]
 143 do
 144   echo "Welcome {${x}} times"
 145   x=$(( $x + 1 ))
 146 done
 147 pause  
 148 cut          on separate line 
 149 # TOK echo "AFTER CUT "


When I use cut in same file again it gives me error .
One was as originally posted

Here is the new error I am getting



Code:

+ echo 'ANOTHER  cut '
ANOTHER  cut 
+ pause
+ read -p 'Press [Enter] key to continue...' fackEnterKey
Press [Enter] key to continue...
+ :
/usr/bin/raspi-config-DEBUG.sh: line 209: % 2: syntax error: operand expected (error token is "% 2")
/usr/bin/raspi-config-DEBUG.sh: line 3522: syntax error near unexpected token `}'
/usr/bin/raspi-config-DEBUG.sh: line 3522: `}'

And here is the code causing the new error

Both lines 209 and 210 cause same error when uses separately.


Code:
echo "ANOTHER  cut "
 208 pause 
 209  : <<cut
 210 : <<cut
 211 echo "AFTER LAST cut @line $LINENO"
 212 pause 
 213 do
 214 #if [ "$(( $choice % 2))" -ne 0 ];
 215 #echo $choice
 216 #pause 
 217         case $choice in

# 4  
Old 08-21-2018
This is truly bizarre.
Did you take notice of cero's reply!
I will re-iterate his reply:
"""There is a utility called cut and I would not use this as the delimiter of a here document."""

Reading your OP implies you want to cut lines out of an executable script on the fly.
Am I correct?
If YES, then I would seriously reconsider what you are doing.
There may be other languages but IIRC the only one I know of that can delete lines on the fly is the Sinclair QL's SuperBASIC.
I am sure bash would get shell shock trying to do this. <pun intended>
# 5  
Old 08-21-2018
PHP users routinely remove and replace executable PHP code “on the fly”.

This is pretty standard these days to dynamically change executable code in runtime.
# 6  
Old 08-21-2018
I have been struggling with bash for less than a month now.

The idea was to use existing bash script and ADD some functionality to it.

When I do coding I like to keep my attempts in code so I do not repeat it.

So I am spoiled by C ability to comment out block of code and found that using "cut" can be used in bash. Unfortunately I have learn the "#" used at the bash scrip in this fashion #!... is really not "commented out line " - so this "problem using "cut" may be same issue.

BTW I did build a simple test function and using two "cut' in succession works just fine - that is not the issue.

I have been writing "code" for few years and firmly believe the "computer" will skip commented out code as instructed and it really does not bother me I my code contains code which is NOT executed one way or another.



Right now my code just "exit"s before getting to offending code.
I will try to analyze / delete the offending code to get rid of this error.
# 7  
Old 08-21-2018
bash 4.x wants the ending cut on a single line, otherwise it issues a warning at the very last line (while bash 3.x and ksh do not issue a warning).
The other error is from substitution in the here-document. Any $( ) or $(( )) or ${} or $var is evaluated and substituted by the result.
You can suppress this substitution by putting the delimiter word in quotes.
Code:
: <<"cut"
here-document with $(( % 0 ))
cut

The most confusing thing with your script is that in normal code mode the shell sees the # and treats the line as a comment. However in a here-document the shell does not see a #comment.

Last edited by MadeInGermany; 08-21-2018 at 02:47 PM..
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

2. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies

3. Shell Programming and Scripting

Cut out string in bash script

Hi all, I'm trying to extract string from variable in BASH. That's probably trivial for grep but I couldn't figure it out. I want to get name, there's sometimes digit after it, but it should be left out. STRING=http://name5.domain.com:8000/file.dat Could someone help me with that? Any... (10 Replies)
Discussion started by: cootue
10 Replies

4. UNIX for Dummies Questions & Answers

cut usage in bash

Hello, Could you put some light on this: > echo "ref_categorie=test" | cut -c1-15- test > echo "ref_categorie=test" | cut -c1-14- =test echo "ref_categorie=test" | cut -c15- test echo "ref_categorie=test" | cut -c15 t > It's executed on AIX if that matters. The man page is not very... (2 Replies)
Discussion started by: tsurko
2 Replies
Login or Register to Ask a Question