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 ?
if(strcmp(argv[1],"strlen")==0) printf("%d\n",strlen(argv[2]));
etc.strcspn example
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.