help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] which file in bash source code (tarball) contain a print


From: Greg Wooledge
Subject: Re: [Help-bash] which file in bash source code (tarball) contain a print output function
Date: Tue, 20 Mar 2012 16:32:03 -0400
User-agent: Mutt/1.4.2.3i

On Wed, Mar 21, 2012 at 03:16:38AM +0700, Opponent O. Oliviala wrote:
> I have some script in /home/scripts/dictionaryScript
> Inside the dictionaryScript: sed 's,Hello,Hi,g' | sed 's,Mar,Changed,g' |
> and so on

You can combine all those into a single sed command.

sed -e 's,Hello,Hi,g' \
    -e 's,Mar,Changed,g' \
    ...

It would run a whole lot more efficiently that way.

> In my /.bashrc file I add an override 2 command
> 
> function echo{
> /bin/echo $* | /home/scripts/dictionaryScript
> }

This should be:

function echo {
  command echo "$@" | /home/scripts/dictionaryScript
}

> function date{
> /bin/date | /home/scripts/dictionaryScript
> }
> 
> That's work well, However If i desire to use my dictionaryScript with all
> command (ls,time,printf,more etc..), I would override all the command.
> So, How about adding my script before it print every std output.

For starters, you could add /home/scripts to your PATH so that you don't
have to specify the full path to dictionaryScript every time.

If you want the entire output of your script to be filtered through
something, normally you pipe it explicitly like this, when you invoke
your script:

  ./myscript | dictionaryScript

But you're saying you can't or won't do that; you simply want to invoke
./myscript and have it filter itself.

The best way to do that would be to wrap the entire content of the script
into a function, and then call that function and filter it, like this:


#!/usr/bin/env bash
myscript() {
  .... the ENTIRE script goes in here ....
}
myscript "$@" | dictionaryScript


But I bet you're not going to like that.  You'll probably think that
adding 3 lines to the script (both top AND bottom) is too much.  You'll
probably want to do it with FEWER lines or something.  (I don't know why
so many people insist on such things, but it's common.)

If that is the case, then you can reopen the script's stdout to point
to a pipe that you create within the script:


#!/usr/bin/env bash
exec 1> >(dictionaryScript)
... the ENTIRE script goes here ....


However, doing this causes issues with timing, because now your output
is being filtered by a background process instead of a foreground process.
Some people won't care about that, and some people will.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]