Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Honoring a command



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 { }.



Messages sorted by: Reverse Date, Date, Thread, Author