in reply to Re: Bash Parser
in thread Bash Parser

Using undef my %... is redundant as you say, really serves no purpose.

eval {$file =~ s/\$$variable/$path/g;}

I will add proper error checking to files, just didn't bother.

You are correct about the substitution, just couldn't find a better approach to do it. Which you neatly just gave me :D.

if(/^[ \t]*source +([\w\-\/\.\_\$]+)/)

The regular expression was more adapted to what people use through testing than what they might use, I will add you suggestions.

if($check_if_bash == 1 && $line_number == 1 && ! /bash/) { return }

Was mainly to exclude scripts that are executable but not bash. If you have a good idea how to verify if a script is indeed bash when it lacks #!/usr/bin/bash id be greatful.

You are right %%...%% isn't a comment it's usually a text block, the main reason to detect it was to avoid parsing it later. But it should not be included in comment statistics.

match/get_sources have slightly different structure since I don't want the statistics from sourced files since the statistics is per script and sourced files are individually included in the end.

There is as you say a possibility of a race condition I will fix this. Thanks

Replies are listed 'Best First'.
Re^3: Bash Parser
by jakobi (Pilgrim) on Sep 30, 2009 at 19:22 UTC

    %% - do you handle here docs as well and ignore embedded multiline strings containing perl or awk, for all styles of ', ", \-escaped or HERE-document?

    Identifying bash scripts:

    If the script's not being invoked by sourcing or an explicit bash filename: -T file AND -x file AND strings [\s/](ba)?sh or sh<versionstring> in the shebang line AND (to be safe) some specific bourne-shell style idiom like N>&N.

    Also consider asking file(1) for a guess.

    Definitely a lie, but still quite common:

    #!/bin/sh #!perl # -w -Sx: line count is one off eval 'exec perl -Sx $0 ${1:+"$@"}' if 0;

    Trying to distinguish between bourne-descending shell might be an additional challenge. Bash vs ksh93 vs dash vs pdksh vs zsh vs csh-considered-harmful vs ... .

    Homework for the astute reader: which of the above shells would be the most easy to detect and exclude?