How to remove comments from a bash script? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to remove comments from a bash script?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-22-2012
LessNux LessNux is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 25 February 2014, 10:15 PM EST
Posts: 40
Thanks: 14
Thanked 1 Time in 1 Post
Question How to remove comments from a bash script?

I would like to remove comments from a bash script. In addition, I would like to remove lines that consist of only white spaces, and to remove blank lines.


Code:
#!/bin/bash
perl -pe 's/ *#.*$//g' $1 | grep -v ^[[:space:]]*$ | perl -pe 's/  +/ /g' > $2
#
# $1 INFILE
# $2 OUTFILE


The above code seemed to work at first. Unfortunately, however, I found later that the above code destroys the following two special variables.

${#ARRAY[@]} the number of array elements
$# the number of shell arguments

A workaround is to replace "${#" and "$#" with words that do not appear in the input file before applying the above code.



Code:
sed 's/${#/__UNUSUALWORD1__/g ; s/$#/__UNUSUALWORD2__/g' in.txt | \
perl -pe 's/ *#.*$//g' | grep -v ^[[:space:]]*$ | perl -pe 's/  +/ /g' | \
sed 's/__UNUSUALWORD1__/${#/ ; s/__UNUSUALWORD2__/$#/' > out.txt


However, the preparatory replacement is awkward. I would like to modify 's/ *#.*$//g' so that it will not match "${#" or "$#". Does anyone know a better solution?

Bash comments always start with #. However, the problem is that bash allows some exceptions where # does not lead a comment, as shown below.


Code:
${#ARRAY[@]} the number of array elements
$# the number of shell arguments
\# escaped by a backslash.
'abcd#efgh' protected by quotes
"abcd#efgh" protected by quotes

Does anyone know how to remove comments from bash scripts without destroying the exempted #'s that do not lead comments? (In addition, I would like to remove lines that consist of only white spaces, and to remove blank lines.)

Many thanks in advance.

Last edited by methyl; 06-22-2012 at 10:33 AM.. Reason: more code tags
Sponsored Links
    #2  
Old 06-22-2012
arrals_vl arrals_vl is offline
Registered User
 
Join Date: May 2012
Last Activity: 27 October 2014, 5:40 AM EDT
Posts: 48
Thanks: 2
Thanked 1 Time in 1 Post
Can you please post your script, or some lines of you script?
Sponsored Links
    #3  
Old 06-22-2012
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 18 April 2014, 5:13 AM EDT
Posts: 6,396
Thanks: 287
Thanked 673 Times in 642 Posts
@arrals_vl
It's the Perl program at the beginning of Post #1 .
    #4  
Old 06-22-2012
haczyk's Avatar
haczyk haczyk is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 9 September 2012, 3:13 PM EDT
Location: Poland/Drzewica
Posts: 37
Thanks: 6
Thanked 1 Time in 1 Post
Quote:
Originally Posted by LessNux View Post
Does anyone know how to remove comments from bash scripts without destroying the exempted #'s that do not lead comments?.

Code:
egrep -i -v "^#|^$" file

?
Sponsored Links
    #5  
Old 06-22-2012
arrals_vl arrals_vl is offline
Registered User
 
Join Date: May 2012
Last Activity: 27 October 2014, 5:40 AM EDT
Posts: 48
Thanks: 2
Thanked 1 Time in 1 Post
I mean the script where you want to remove the comments...not the script that removes the comments.
Sponsored Links
    #6  
Old 06-22-2012
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 31 October 2014, 5:56 PM EDT
Location: Saskatchewan
Posts: 19,743
Thanks: 832
Thanked 3,375 Times in 3,162 Posts
The problem is, to understand which # are comments and which # aren't, you have to understand the script. "#" '#' should not be stripped out for instance -- or any other time # is not given to the bare line but wrapped inside something. It's possible to wrap # inside quite complex structures, I'm not sure you can check for every possible thing with regexes alone.

What's the best thing out there for understanding scripts? A shell, of course.

There is an option for the shell to print lines as it executes them(which strips out comments). Also, an option to check lines for syntax without actually running them. Unfortunately they seem to be mutually exclusive. Seeing if there's anything else relevant...
Sponsored Links
    #7  
Old 06-22-2012
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 973 Times in 789 Posts
Quote:
Originally Posted by LessNux View Post
I would like to remove comments from a bash script. In addition, I would like to remove lines that consist of only white spaces, and to remove blank lines.
I'm curious. Why?

Regards,
Alister

---------- Post updated at 12:21 PM ---------- Previous update was at 12:11 PM ----------

Also, while your post makes it clear that you are aware that naively removing #.* can cause problems, the same is true for a blank line. If it is part of a heredoc or some other quoted string, removing a blank line alters the meaning of the script.

Regards,
Alister
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed remove css comments gravesit Shell Programming and Scripting 9 08-25-2010 01:31 PM
Sed script, changing all C-comments to C++-comments black_hawk Shell Programming and Scripting 2 11-21-2009 07:17 PM
how can i remove comments in random positions in a file?(bash) bashuser2 Shell Programming and Scripting 2 05-02-2009 05:47 AM
please explain this sed shell script to remove C++ comments. Priyaranjan Shell Programming and Scripting 1 03-05-2009 10:52 AM
Remove comments... LivinFree UNIX for Dummies Questions & Answers 1 10-11-2001 09:43 AM



All times are GMT -4. The time now is 05:58 PM.