Wondering how to take a set of C functions and turn them into shell/bash commands.
So say I have a simple set of C functions
int
fn1() {
// some C code for function 1.
}
int
fn2() {
// some C code for function 2.
}
int
fn3() {
// some C code for function 3.
}
I would like to then somehow create CLI commands so I can use them from the terminal.
$ fn1 <param> <param> ...
$ fn2 ...
$ fn3 ...
Not sure what the process is of doing this. If I need to somehow rewrite all the function interfaces in shell script, and then call of to a C function in some way, sort of like this (bash script):
fn1() {
callc mylib/fn1 $1 $2
}
fn2() {
...
}
...
Or if I can just somehow convert each C function into a shell script automatically by separating them into individual files fn1.c
, fn2.c
, etc. and somehow loading them into the shell with source ~/.bash_profile
type of thing.
Any help would be appreciated, thank you.
Compile the above, then execute the command “FullPathToDirectory/NameOfExecutableFile Argument1 Argument2”.
Once that is working, either move the executable file to one of the directories listed in your PATH environment variable or modify your PATH directory to include the directory that contains the executable file.
Some shells require you to execute the
rehash
command after putting a new executable in the PATH.Once that is working, modify the program as you desire.
You can create separate programs for each command. If you want all the source code in one program, a common method to do that is to create multiple file system links to the executable file (as with the Unix
ln
command) and use theargv[0]
contents to choose which function to perform. (argv[0]
usually contains the executable path, with the file name as the last component.)Write a single C executable with all the functions in it ... then make aliases for them or create wrapper shell scripts .
Note aliases won't take options but you can use functions for that : https://askubuntu.com/questions/626458/can-i-pass-arguments-to-an-alias-command
Aliases:
scripts: /usr/bin/function-x.sh
Or go old-school, perhaps: write C code to examine how it was invoked (0th arg from original command-line arguments) and invoke the right C function based on that name. Requires compiling such a C program to a single executable, then creating symbolic links to the base application where the symlinks are the names of the functions of interest. No shell code needed beyond installing the artifacts here - executable and symlinks - into a directory in your $PATH.
Example. If the following code is name toybox.c, and ~/bin exists and is in the user's $PATH, use something like:
Simple tests - only shows that the scaffolding is in place.
The source for toybox.c might look like: