Related to question 3451993, is it possible to call a function which is internal to subst.c (in the Bash source code) in a Bash script?
6 Answers
Bash supports loadable builtins. You might be able to make use of this to do what you want. See the files in your /usr/share/doc/bash/examples/loadables (or similar) directory.
2 Comments
grep subst /usr/share/doc/bash/examples/loadables/* returned nothing.grep subst /usr/include/bash/* gives /usr/include/bash/shell.h:#include "subst.h", grep shell.h /usr/share/doc/bash/examples/loadables/* gives a bunch. Unfortunately, grep prompt /usr/share/doc/bash/examples/loadables/* gives nothing, but grep prompt /usr/include/bash/subst.h does look interesting.It even possible to use C data structures ;)
This is ctypes.sh, a foreign function interface for bash. ctypes.sh is a bash plugin that provides a foreign function interface directly in your shell. In other words, it allows you to call routines in shared libraries from within bash.
Check out https://github.com/taviso/ctypes.sh ;)
1 Comment
The simplest way to do this is to write a simple program that collects the input, feeds it to the function, then prints the result. Why don't you tell us what you are attempting to accomplish and perhaps we can suggest an easier way to "skin this cat".
5 Comments
The simplest way to do this is to write a simple program that collects the input, feeds it to the function, then prints the resultHow do you do this on chrome os? It is the case where all partitions are mounted with noexec except for the rootfs which is write protected.No.
You can't access a function internal to the shell binary from the shell if it is not exported as a shell function.
1 Comment
[System.DateTime]::Now; 2) I am looking for a Linux/BASH analog that does not involve me writing more than the merest whisper of C code (I'll need an SOW or I'll be doing it in my free time). To complement this, I have reviewed guile and I am familiar with python's cffi library in addition to the python compiler nuitka. I can also use go...so any BASH/C version of this will need to be pretty easy or well-motivatedThis code looks pretty elegant: (from here) Its the same solution @Jay pointed out.
bash$ cat testing.c
#include <stdio.h>
char* say_hello()
{
return "hello world";
}
float calc_xyzzy()
{
return 6.234;
}
int main(int argc, char** argv)
{
if (argc>1) {
if (argv[1][0] =='1') {
fprintf(stdout,"%s\n",say_hello());
} else if ( argv[1][0] == '2') {
fprintf(stdout,"%g\n",calc_xyzzy());
}
}
return 0;
}
bash$ gcc -o testing testing.c
bash$ ./testing 1
hello world
bash$ ./testing 2
6.234
bash$ var_1="$(./testing 1)"
bash$ var_2="$(./testing 2)"
bash$ echo $var_1
hello world
bash$ echo $var_2
6.234
bash$