Some dude
Some dude

Reputation: 587

Regular expression to get an entire function from a shell script?

I'm writing a small bash script and I need to read other scripts and search for specific functions in them so I can copy those functions from start to finish. I'm not an expert using grep, so I tried something like:

grep -E ".*() \{.*\}" file.sh

But it didn't work, and it also wouldn't get the entire function if it did, just the code between the first opening bracket and the first closing one.

So what would be a good regular expression for this? It doesn't necessarily have to use grep, I just figured it would.

Upvotes: 0

Views: 325

Answers (1)

jil
jil

Reputation: 2691

No need to reinvent wheel here. Bash naturally contains a parser to get the information needed. You could use declare (or type). E.g.

source file.sh
declare -F      # will print list of all declared functions
declare -f foo  # will print function definition for function foo

See help declare and help type for more information.

Upvotes: 3

Related Questions