zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh Users <zsh-users@sunsite.dk>
Subject: Re: Honoring a command
Date: Sat, 3 Apr 2004 21:43:30 +0000	[thread overview]
Message-ID: <1040403214330.ZM15892@candle.brasslantern.com> (raw)
In-Reply-To: <20040401170312.GA25965@DervishD>

On Apr 1,  7:03pm, DervishD wrote:
} 
} [...] the question is that I want to modify code like this:
} 
}     print -n "Doing whatever command..." >&2
}     whatever.command 2> /dev/null || { print " error! Message" >&2; return 1; }
}     print " done." >&2
} 
} 
}     to this:
} 
}     verbosely_do "Doing whatever command" whatever.command \
}         || { print "Message" >&2; return 1;}

If you have access to the "initscripts" package, you might take a look
at /etc/rc.d/init.d/functions -- particularly the "action" function
that is defined in that file.  It does pretty much exactly what your
"verbosely_do" is meant to do (with the addition of wrapping the call
in something called "initlog" which makes syslog entries for the
command, but you can take that out).

Something to note about that "action" function is that it folds the
failure message into the wrapper function rather than following the
wrapper call with an or-command.

}     The problem I see is: what will happen if the command has
} redirections, metacharacters, quotes, variable references, etc.?

Unless you quote them, they'll all be processed BEFORE verbosely_do
is called, which may or may not do what you want.

If you do quote them, then you'll probably need to use
	eval "$*"
inside the verbosely_do function, which of course has it's own set of
problems.  As far as I can tell, the "action" function I mentioned
assumes that there are no redirections and that it's OK for metachars
and so on to have been expanded before the call.

I'd also note that both "action" and "verbosely_do" are assuming that
whatever.command does not produce any standard output of its own, or
else that it's been redirected away somewhere (which is part of what
"initlog" does, I think).

Given that the command itself produces no output, you might consider
something like this:

    verbosely_watch() {
    	emulate -L zsh
	local output
	print -u2 -n ${(r:WIDTH:)1}
	if read output
	then
	    print -u2 $output
	else
	    print -u2 '[ok]'
	fi
    }

    TRAPZERR() { print '[fail]' }
    { whatever.command } 2>/dev/null | verbosely_watch "Doing whatever"

That allows any syntax you like within the { }.


  reply	other threads:[~2004-04-03 21:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-04-01 17:03 DervishD
2004-04-03 21:43 ` Bart Schaefer [this message]
2004-04-04 11:16   ` DervishD
2004-04-05  1:09     ` Bart Schaefer
2004-04-05  9:59       ` DervishD

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1040403214330.ZM15892@candle.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).