Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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"
}
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.
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!
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-###
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.