removing shell comments from ksh scripts 
Author Message
 removing shell comments from ksh scripts

This is a multi-part message in MIME format.
--------------2BFF8D90CA8F77A200AA39A3
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Does anybody know of a reliable way/have a program to remove comments
from ksh scripts?

This should have been a simple C program, but the problem I'm up against
is how to handle lines like this...

k=${j##*/}      # comment

...where the program has to know that the 1st hash isn't a comment
character.


Thanks,

James
--
_____________________________________________________________________
Home Page -           http://www.*-*-*.com/ ~jhunt/
Sablime/Volume FAQ - http://www.*-*-*.com/ ~jhunt/sab+vol_faq.html
--------------2BFF8D90CA8F77A200AA39A3
Content-Type: text/x-vcard; charset=us-ascii; name="vcard.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for James Hunt
Content-Disposition: attachment; filename="vcard.vcf"

begin:          vcard
fn:             James Hunt
n:              Hunt;James
org:            Lucent Technologies Network Systems UK Ltd
adr:            Lucent Technologies;;Swindon Road;Malmesbury;Wiltshire;SN16 9NA;England

title:          Software Engineer
tel;work:       +44 1666 832981
tel;fax:        +44 1666 832844
note:           Sablime and Volume Administrator, Application Support
x-mozilla-cpt:  ;0
x-mozilla-html: TRUE
version:        2.1
end:            vcard

--------------2BFF8D90CA8F77A200AA39A3--



Sat, 26 Feb 2000 03:00:00 GMT  
 removing shell comments from ksh scripts



Quote:
>This is a multi-part message in MIME format.
>--------------2BFF8D90CA8F77A200AA39A3
>Content-Type: text/plain; charset=us-ascii
>Content-Transfer-Encoding: 7bit

>Does anybody know of a reliable way/have a program to remove comments
>from ksh scripts?

>This should have been a simple C program, but the problem I'm up against
>is how to handle lines like this...

>k=${j##*/}  # comment

>...where the program has to know that the 1st hash isn't a comment
>character.

Your program has to do some rudimentary lexical and syntactic analysis of the
script. You simply cannot do this reliably without paying attention to the
grammar of the shell language.

The ${x##y} parameter expansion schema isn't the only problem. Have you
considered quoted text?

        COMMENT="# blah"

This is a valid statement in the language, with no comment.

Also, there is this:

    if [ $# -lt 3 ] ; then
        echo this script requires at least 3 arguments
        exit 1
    fi

An easy algorithm that is likely to work well is one that will recognize the
comment character only if it does not appear within nested delimiters or quote
delimiters.  That is, you set up some state machine that recognizes quotes
and () {} delimiters. Whenever you see a # character, you only treat it
as a comment if you are not between any delimiters, and if you are not scanning
a $# . You can tell that you are not by examining the state of the state
machine.  When you see a # character that is in a context where it must be
treated as a comment, you simply scan to the end of the line discarding all
encountered characters. Your program should probably also do the safe thing
and bail out with diagnostic messages if it sees some problem in the source,
such as unbalanced delimiters.

I'd read a document that describes the grammar of the scripting language to be
absolutely aware of all the pitfalls. The Korn shell isn't exactly POSIX,
so a comment {*filter*} that works on the POSIX language might not quite work
for all possible Korn scripts.

Quote:
>begin:          vcard
>fn:             James Hunt
>n:              Hunt;James
>org:            Lucent Technologies Network Systems UK Ltd
>adr:            Lucent Technologies;;Swindon Road;Malmesbury;Wiltshire;SN16 9NA;England

>title:          Software Engineer

You are a software engineer and you need help with this programming triviality?
It seems that anyone can call him or herself a ``Software Engineer'' these
days.
--
"In My Egotistical Opinion, most people's C programs should be
indented six feet downward and covered with dirt."
        -- Blair P. Houghton


Sat, 26 Feb 2000 03:00:00 GMT  
 removing shell comments from ksh scripts

On Tue, 09 Sep 1997 15:51:28 +0100, James Hunt

Quote:

>k=${j##*/}  # comment

>...where the program has to know that the 1st hash isn't a comment
>character.

If the hash is always enclosed in braces, just keep track of the
braces to see if a comment has started.

Hope this works.

Dan



Sat, 26 Feb 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Ullman 7.4 Why script works when comments removed?

2. emacs comment edit for shell scripts ?

3. Solution to decoding QUERY_STRING in ksh (Korn Shell)

4. Scheme Shell: Shell Scripting for the 90's

5. Scheme Shell: Shell Scripting for the 90's

6. Scheme Shell: Shell Scripting for the 90's

7. Scheme Shell: Shell Scripting for the 90's

8. Scheme Shell: Shell Scripting for the 90's

9. I want to put my awk file into ksh script

10. awk within ksh script

11. ksh-awk script problem

12. scripts: ksh calls awk: no stdout?

 

 
Powered by phpBB® Forum Software