Hello I have a problem, I need to create a BASH function that I can pass a stream to NOT A FILE. Here is the function: function ff() { cat /etc/group | fgrep -f "$1" }; When I run it like so
I get an error saying "fgrep: /dev/fd/63: No such file or directory", instead of "some_uer:x:0:" Much Appreciated! asked 13 May '13, 17:49 Rocco |
It appears that bash is closing the stream before grep gets a chance to open it; it could be caused by grep not being the first command in the pipeline. Try
EDIT: Rather than the stream being closed prematurely, it appears that the way bash handles this is by connecting an unnamed pipe between
answered 13 May '13, 20:19 KJ4TIP |
First of all (a pet peeve) let's get rid of the unnecessary invocation of cat in your function:
Next, be aware that your use of that '<()' notation invokes what's called "process substitution" which executes a pipeline in a separate process and associates its stdout with a file created for the purpose; any such expression evaluates to the name of that file, so the error message you're seeing is correct. In other words, you've presented the name of the pipeline's stdout file when what you wanted was the contents, almost as if you'd done this:
...so you may by now see that in your case you could get what you want (if a bit awkwardly) thus:
...which, of course, makes it tempting to then do away with the process substitution trickery and simply do this:
Bash's process substitution facility is way cool but maybe overkill here... answered 15 May '13, 08:27 mod But note that the
(15 May '13, 08:41)
KJ4TIP
|
OK, this is the solution, based on process substitution and fgrep's -f flag:
answered 15 May '13, 08:55 mod |
I don't have enough points to edit or comment here, so: Your primary problem is that, in a function, $1 refers to the first argument passed to the function, not to the the first argument passed to the calling script. You don't pass any arguments to your function, so $1 should be undefined or a null string - not what you wanted. If you run your script using For how to actually fix it, see @KJ4TIP 's answer. answered 07 Jun '13, 17:53 josephj11 |