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 ?

C0deDaedalus
  • 361
  • 1
  • 4
  • 19
  • 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. – Paul Ogilvie Apr 08 '18 at 15:36
  • Use Google: `strcspn example` – Cyrus Apr 08 '18 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 Apr 08 '18 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 Apr 08 '18 at 15:49
  • Probably you've got some [XY problem](http://xyproblem.info). 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) – Basile Starynkevitch Apr 08 '18 at 16:36
  • @BasileStarynkevitch, You are very true about XY problem. Maybe I should write a code first and then ask question. Will update in the question for the same. – C0deDaedalus Apr 08 '18 at 16:50
  • You should improve your question by adding paragraphs about motivation and context – Basile Starynkevitch Apr 08 '18 at 16:52
  • Even the recent edit is not motivating your question and not giving the real issue you've got at first. So please add the original motivation – Basile Starynkevitch Apr 08 '18 at 17:02
  • @BasileStarynkevitch, This is straight issue and I cannot be more motivating about it. So I would prefer to try writing a code myself and then update the question. Thanks everyone for your time and efforts. – C0deDaedalus Apr 08 '18 at 17:07
  • https://unix.stackexchange.com/q/436305/50557 is nearly a duplicate (cross-posting) – Basile Starynkevitch Apr 08 '18 at 17:08
  • What is the actual original issue you've got? It certainly is not something that `wc -m` solves.... – Basile Starynkevitch Apr 08 '18 at 17:09

1 Answers1

2

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).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 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 Aug 03 '23 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 Aug 03 '23 at 14:52
  • 1
    Linking this answer as a supplement: https://stackoverflow.com/questions/3453076/call-c-function-in-bash-script – Chris Aug 03 '23 at 14:57