LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-02-2013, 11:55 AM   #16
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191

So clearly no need to have this in a single line entry as who could ever want to type it out all the time.
So in a script it looks a little cleaner:
Code:
#!/usr/bin/awk -f

{
	line=$0
	gsub(/"[^"]*"/, "", line)
	gsub(/'[^']*'/, "", line)
	gsub(/#.*$/, "", line)
}

/test()|function test/ {

	if( sub("test()|function test", "", line) ) {
		start=1
		brackets=0
	}
}

/[{}]/ && start{

	brackets+=split(line,_,"{") - 1
	brackets-=split(line,_,"}") - 1
}

start{
	print $0" - "brackets
	
	if (brackets <= 0){
		print "EXITING"
		exit
	}
}
You can shift the "EXITING" back if you prefer, but I think the rest is a little cleaner.
 
Old 08-02-2013, 03:26 PM   #17
apottere
LQ Newbie
 
Registered: Jul 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
Yeah, I think I might give up on this one. The intent was to have "modules" of bash files, and be able to extract certain functions from them to insert into others. I can get the same kind of functionality with "@start_test <function test....> @end_test" with much less hassle.
 
Old 08-02-2013, 03:39 PM   #18
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by apottere View Post
The intent was to have "modules" of bash files, and be able to extract certain functions from them to insert into others.
Oh you might as well consider this one for your solution and an example project that makes use of it, by the same author.

Last edited by konsolebox; 08-02-2013 at 03:40 PM.
 
1 members found this post helpful.
Old 08-02-2013, 03:45 PM   #19
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I doubt you'll ever be able to write a completely reliable function extractor using regex-based tools like awk. The nested brace patterns are just too unpredictable. What if there are brace characters sitting in comments, for example?

Actually, I have a suggestion for a much easier way to get what you want (maybe):

Code:
( . test.sh && declare -f test )
This will print out the test function, as bash sees it. It will however be restructured into what declare considers reusable script syntax, so the original formatting and things like comments will disappear.
 
1 members found this post helpful.
Old 08-02-2013, 04:01 PM   #20
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I just saw the updated posts that appeared after I started writing, so here's another idea.

If you want to have a kind of library of functions, and the input files are completely under your control, then just separate each function with a fixed delimiter string, and use that to have awk extract the ones you want.

function_file.txt:
Code:
###---###
test_a(){
    echo "This is test function A"
}
###---###
test_b(){
    echo "This is test function B"
}
###---###
Code:
$ awk 'BEGIN{ RS="\n###---###\n" } /^test_b/' function_file.txt
test_b(){
    echo "This is test function B"
}
 
1 members found this post helpful.
Old 08-02-2013, 04:29 PM   #21
apottere
LQ Newbie
 
Registered: Jul 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
I doubt you'll ever be able to write a completely reliable function extractor using regex-based tools like awk. The nested brace patterns are just too unpredictable. What if there are brace characters sitting in comments, for example?

Actually, I have a suggestion for a much easier way to get what you want (maybe):

Code:
( . test.sh && declare -f test )
This will print out the test function, as bash sees it. It will however be restructured into what declare considers reusable script syntax, so the original formatting and things like comments will disappear.
This is exactly what I was looking for, I don't much care about formatting when the executable is created. In fact, is there any way to compress it further with declare? The man pages didn't seem to have anything like that.
 
Old 08-02-2013, 04:31 PM   #22
apottere
LQ Newbie
 
Registered: Jul 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
I just saw the updated posts that appeared after I started writing, so here's another idea.

If you want to have a kind of library of functions, and the input files are completely under your control, then just separate each function with a fixed delimiter string, and use that to have awk extract the ones you want.

function_file.txt:
Code:
###---###
test_a(){
    echo "This is test function A"
}
###---###
test_b(){
    echo "This is test function B"
}
###---###
Code:
$ awk 'BEGIN{ RS="\n###---###\n" } /^test_b/' function_file.txt
test_b(){
    echo "This is test function B"
}
This is a much cleaner version of what I had (and I may steal it for future use), but declare -f is what I was looking for. Thanks for the help!
 
Old 08-02-2013, 04:35 PM   #23
apottere
LQ Newbie
 
Registered: Jul 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by konsolebox View Post
Oh you might as well consider this one for your solution and an example project that makes use of it, by the same author.
This is a cool concept too, but I'm not sure I want to link at run-time.
 
Old 08-02-2013, 04:45 PM   #24
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Using markers is actually another good idea. I would also suggest using function-start and function-end markers to make it more specific to functions only even if there are some other lines around.
Code:
###-begin-###
test_a(){
    echo "This is test function A"
}
###-end-###
 
Old 08-02-2013, 04:51 PM   #25
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by apottere View Post
This is a cool concept too, but I'm not sure I want to link at run-time.
Just curious. Why did you have to extract the functions for?

And what kind of modules? I mean how would you use them?
 
Old 08-02-2013, 04:57 PM   #26
apottere
LQ Newbie
 
Registered: Jul 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
I'm trying to modularize my code, so I can write a script with modules and then link it all together. This means I only need the modules present once, and after that I have a working executable that I can use anywhere.
 
Old 08-02-2013, 05:16 PM   #27
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I'm not exactly sure what you mean by "compress it further", but no, there's no way that I know of to control the format of the declare output. It's all handled internally.
 
Old 08-02-2013, 05:37 PM   #28
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by apottere View Post
I'm trying to modularize my code, so I can write a script with modules and then link it all together. This means I only need the modules present once, and after that I have a working executable that I can use anywhere.
Then Shell Script Loader could be one solution. Scripts that work with Shell Script Loader could also be compiled to be made as one file. See the section about external compilers. And PlayShell makes use of all those features. You could examine it to see how it's done. See start.sh, compile.sh and loader/*.

It's also helpful since you can test your code with your modules separated without needing to compile all together first and makes debugging easier to detect which file actually had the error.
 
Old 08-02-2013, 11:15 PM   #29
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Personally I would just place all your functions / modules in a group of files and then simply source the appropriate file so the function / module is now available.
 
Old 08-02-2013, 11:31 PM   #30
apottere
LQ Newbie
 
Registered: Jul 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Personally I would just place all your functions / modules in a group of files and then simply source the appropriate file so the function / module is now available.
The object is to end up with a completely contained script I can move anywhere and have work, not one that relies on other files being in the correct place. This is normally a fine solution, just not in this case.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I extract columns from a file without using awk or perl? KG425 Programming 13 06-06-2012 11:40 AM
BASH or AWK: extract columns in multiple files and combine to a single file cristalp Programming 2 03-15-2012 11:55 AM
How to extract lines from file using AWK keenboy Linux - General 7 08-05-2010 08:29 AM
using awk substring function on a file in a bash script matt007 Programming 3 06-17-2008 08:17 PM
Getting awk to extract scripts from a file jspaceman Programming 5 11-24-2002 06:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration