I am developing a console application in C on linux.
Now an optional part of it (its not a requirement) is dependant on a command/binary being available.
If I check with system() I’m getting sh: command not found as unwanted output and it detects it as existent. So how would I check if the command is there?
Not a duplicate of Check if a program exists from a Bash script since I’m working with C, not BASH.
To answer your question about how to discover if the command exists with your code. You can try checking the return value.
You could also use popen, to read the output. Combining that with the whereis and type commands suggested in other answers –
Or using type:
The result of the
typecommand is a bit harder to parse since it’s output varies depending on what you are looking for (binary, alias, function, not found).