1

Consider a script pass_command

ls | "$@"

It accepts named commands as arguments

pass_command sort
#prints files sorted

Which is good enough for all practical purposes. I'm curious though, Is there a mechanism that lets me pass compound commands? Something like

pass_command (sort | uniq) #Forgive the silly example.

Edit: A more complicated example that motivated this question.

until who | grep mary
do
  sleep 60
until

Signals mary logging on. I wanted to generalize this to signal some event. The intuitive idea was this:

#watchfor
until "$@"
do
  sleep 60
done

And then you would call it as

watchfor (who | grep mary)

That was the idea. You can give the argument a name, and the above works fine. It just made me think of this.

I suppose I was trying to understand the most idiomatic way of hiding the signaling command.

2

You would have to use eval in the script as soon as $@ is more than a simple command (the argument to the script has to be passed as a quoted string):

ls | eval "$@"

But then again, this would also be the same as

pass_command sort | uniq

i.e., you pass the sort command and the use uniq on the output.

I see no real use for this sort of script or function as Unix's pipelining capability seems to cover most practical applications.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.