0

Sorry for If I look stupid asking this question, but I am very curious to know about it.

I wanted to simply calculate the length of a string (that is hash value). So, I opened terminal and did this:

$ apropos length

that returned me with a bunch of commands/functions having (3) or (3ssl) appended at the end of them. Now man man gives us information about what these section numbers mean.

3   Library calls (functions within program libraries)

Out of curiosity, I just tried with all these commands (in hope at least one would work)

strcspn (3)          - get length of a prefix substring
strlen (3)           - calculate the length of a string
strnlen (3)          - determine the length of a fixed-size string
strspn (3)           - get length of a prefix substring
wcslen (3)           - determine the length of a wide-character string
wcsnlen (3)          - determine the length of a fixed-size wide-character string

and got nothing but same error for every command

$ strnlen HelloWorld 
$ strnlen: command not found

Well, I know how to find length of string in shell using wc -m , expr length and other workarounds.But,

Is it possible to write a bash script that can internally call these library commands and get the task done ?

11
  • 2
    You must have a program that calls the library functions. It can have statements like if(strcmp(argv[1],"strlen")==0) printf("%d\n",strlen(argv[2])); etc. Commented Apr 8, 2018 at 15:36
  • Use Google: strcspn example
    – Cyrus
    Commented Apr 8, 2018 at 15:45
  • I don't understand what you are asking. Is tcc not suitable for your needs? What are those needs and what solution would satisfy you? For the majority of cases, shell script is altogether unsuitable for anything which requires you to initialize something and then use it repeatedly, because the foundational pirinciple of shell scripts is to run subprocesses, and two independent processes can't really share an object in memory. A wrapper in Python or Ruby might make an excellent interface for using a small set of library functions from a shell script, but that's basically a different question.
    – tripleee
    Commented Apr 8, 2018 at 15:46
  • 1
    ... And how to write a C program is a different question still, and obviouly too broad (maybe post a separate question with your C code so far if you have trouble figuring out how to use these functions).
    – tripleee
    Commented Apr 8, 2018 at 15:49
  • Probably you've got some XY problem. You should motivate your question and give more context into it, so edit your question to improve it a lot (by adding several paragraphs about the primordial issue you've got) Commented Apr 8, 2018 at 16:36

2 Answers 2

3

You cannot execute any library function from your Bash shell (in general) - excluding of course the many library functions that your shell calls (for example its cd builtin will call the chdir(2) system call).

You should use some program (usually some executable produced by the C compiler, or some other compiled language like Go, Ocaml, C++, Rust, ...) to call these functions. Once you've got a program your shell would use fork(2) then execve(2) to run it.

Some shells (e.g. zsh) also accept plugins. You could write (in C, then compile it) an extension, make a plugin of it and load that plugin.

In practice, to run the wcslen(3) function from your shell, you should write some C source code calling that (e.g. yourprog.c), compile that source code into an ELF executable (e.g. with gcc -Wall -Wextra -g yourprog.c -o yourbin with GCC) then run that ./yourbin executable with appropriate arguments. You should expect to make bugs in your program and to spend time debugging it (so use the gdb debugger to understand what is going wrong, then improve your code in yourprog.c and repeat).

BTW, a shell deals mostly with strings (and perhaps arrays of them), but most C functions consume and/or return data which are not strings, but something more complex (for examples: fopen(3) returns a FILE* handle, readdir(3) and fgetpwent(3) don't manipulate strings, etc etc...). So there is an "impedance mismatch", and you need to have or write code to solve that.

You could be interested in using some scripting language like Guile or Python.

BTW, be aware that in practice UTF-8 is used everywhere today on Linux (so it is unlikely you want to call wcslen).

3
  • Can you comment on any BASH analog to Powershell's ability to invoke .NET sdk methods directly, e.g. [System.DateTime]::Now? Other than guiling some lib for use in the shell via guile invocation (smallest C code footprint in terms of bugs created I can think of), is this something that strictly must be managed via plugin/custom exe, or is there some other way to interact with the library functions directly from BASH/Shell?
    – Chris
    Commented Aug 3, 2023 at 14:50
  • The reason I ask is because I'll generally need an SOW that is paid for to write any kind of C code. I suppose I can open-source key Guile wrappers I use often in my own time since that seems feasible to manage and fix myself, but I'd like to know if this is really the only way to duplicate what Powershell is doing in BASH before screwing around with it.
    – Chris
    Commented Aug 3, 2023 at 14:52
  • 1
    Linking this answer as a supplement: stackoverflow.com/questions/3453076/…
    – Chris
    Commented Aug 3, 2023 at 14:57
0

I've written a program called cliFFI for this.

cliFFI is a tool for calling shared library functions ad-hoc, right from your shell, without having to write and compile a wrapper script. It is especially meant for reverse engineering, but you might find it helpful for scripting or development purposes too.

For the OP's use case, it would be as simple as cliffi libc.so i strlen HelloWorld

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.