如何从 shell 执行库调用命令?
How to execute library calls commands from shell?
对不起,如果我问这个问题看起来很愚蠢,但我很好奇想知道它。
我想简单地计算一个字符串的长度(即哈希值)。所以,我打开终端并执行了以下操作:
$ apropos length
返回给我一堆 commands/functions,最后附加了 (3)
或 (3ssl)
。现在 man man 向我们提供了有关这些 section numbers
含义的信息。
3 Library calls (functions within program libraries)
出于好奇,我尝试了所有这些命令(希望至少有一个能起作用)
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
并且每个命令都得到相同的错误
$ strnlen HelloWorld
$ strnlen: command not found
嗯,我知道 how to find length of string in shell 使用 wc -m
、expr length
和其他 workarounds.But、
是否可以编写一个 bash 脚本来在内部调用这些库命令并完成任务?
您不能从您的 Bash shell 执行任何库函数(一般来说)——当然不包括您的 shell 调用(例如它的 cd
内置函数将调用 chdir(2) 系统调用)。
你应该使用一些程序(通常是一些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)到运行它。
一些 shell(例如 zsh
) also accept plugins。您可以编写(用 C 语言,然后编译)一个扩展,制作一个插件并加载该插件。
在实践中,运行 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 了解问题所在,然后在 yourprog.c
中改进代码并重复)。
顺便说一句,shell 主要处理字符串(可能还有字符串数组),但大多数 C 函数使用 and/or return 数据,这些数据不是字符串,而是更复杂的东西(例如:fopen(3) returns a FILE*
handle, readdir(3) and fgetpwent(3) 不要操作字符串等)。所以有一个 "impedance mismatch",你需要拥有或编写代码来解决它。
您可能有兴趣使用一些 scripting language like Guile or Python。
顺便说一句,请注意实际上 UTF-8 is used everywhere 今天在 Linux(所以你不太可能想打电话给 wcslen
)。
对不起,如果我问这个问题看起来很愚蠢,但我很好奇想知道它。
我想简单地计算一个字符串的长度(即哈希值)。所以,我打开终端并执行了以下操作:
$ apropos length
返回给我一堆 commands/functions,最后附加了 (3)
或 (3ssl)
。现在 man man 向我们提供了有关这些 section numbers
含义的信息。
3 Library calls (functions within program libraries)
出于好奇,我尝试了所有这些命令(希望至少有一个能起作用)
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
并且每个命令都得到相同的错误
$ strnlen HelloWorld
$ strnlen: command not found
嗯,我知道 how to find length of string in shell 使用 wc -m
、expr length
和其他 workarounds.But、
是否可以编写一个 bash 脚本来在内部调用这些库命令并完成任务?
您不能从您的 Bash shell 执行任何库函数(一般来说)——当然不包括您的 shell 调用(例如它的 cd
内置函数将调用 chdir(2) 系统调用)。
你应该使用一些程序(通常是一些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)到运行它。
一些 shell(例如 zsh
) also accept plugins。您可以编写(用 C 语言,然后编译)一个扩展,制作一个插件并加载该插件。
在实践中,运行 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 了解问题所在,然后在 yourprog.c
中改进代码并重复)。
顺便说一句,shell 主要处理字符串(可能还有字符串数组),但大多数 C 函数使用 and/or return 数据,这些数据不是字符串,而是更复杂的东西(例如:fopen(3) returns a FILE*
handle, readdir(3) and fgetpwent(3) 不要操作字符串等)。所以有一个 "impedance mismatch",你需要拥有或编写代码来解决它。
您可能有兴趣使用一些 scripting language like Guile or Python。
顺便说一句,请注意实际上 UTF-8 is used everywhere 今天在 Linux(所以你不太可能想打电话给 wcslen
)。