I'm interested in a general solution, but my specific example problem is writing a .bashrc function that wraps grep and appends a file path to the command if missing. Basically, any time grep would wait on stdin I instead want it to search a specific file. My problem is how to tell (in the wrapper) whether the final argument is a path to be searched versus e.g. the search pattern.
$ ls
example_file.txt
$ grep -someopts 'somestring' 'example_file.txt'
Pretend that -someopts are in fact arbitrary valid options to grep.
Is the final argument a pattern (to be searched for) or a file (to be searched)?
If somestring is a parameter to one of the -options, then example_file.txt is the pattern to search for, and grep will wait on stdin. Otherwise, somestring is the pattern to search for and example_file.txt will be searched.
In the former case, I want to append my own file to be searched on the end of the command, but I can't detect said case without false-positives. The only way seems to be for the wrapper to consider every argument that grep could take.
Here is my wrapper function (where check_has_path is what I need to implement):
function grep_wrapped() {
if check_has_path ; then
grep "$@"
else
grep "$@" '/default/filepath.txt'
fi
}