In Unix / Bash, is "xargs -p" a good way to prompt for confirmation before running any command?
I have asked how to make any command "ask for yes/no before executing" in the question
In Bash, how to add "Are you sure [Y/n]" to any command or alias?
It seems like for the command
hg push ssh://username@www.example.com//somepath/morepath
I can also do this
echo ssh://username@www.example.com//somepath/morepath | xargs -p hg push
The -p
is the one that does the trick. This will be set
as an alias, such as 开发者_Go百科hgpushrepo
. Is this a good way,
any pitfalls, or any better alternatives to do it? I was
hoping to use something that is standard Unix/Bash
instead of writing a script to do it.
The disadvantage to using an alias is that it won't take parameters. If you wanted to generalize that hg
command so you could use it with any username, hostname or path, you'd have to use a script or a function.
By the way, using a script is "standard Unix/Bash". A simple script or function is just as easy (easier, really, because of the increased power and versatility) as an alias. Aliases are useful for very short, extremely simple command shortcuts. Frequently they're used to enable an option as a default (eg. alias ls='ls --color=auto'
).
For unchanging commands that you use frequently that don't need arguments (except those that can be appended at the end), aliases are perfectly fine. And there's nothing wrong with using xargs
in the way that you show. It's a little bit overkill and it's an unnecessary call to an external executable, but that shouldn't be significant in this case.
xargs
has a nasty tendency to lead to uncomfortable surprises because of the separator problem https://en.wikipedia.org/wiki/Xargs#Separator_problem
GNU Parallel http://www.gnu.org/software/parallel/ does not have that problem and thus may be a safer choice.
seq 10 | parallel -p echo
Watch the intro video for GNU Parallel: http://www.youtube.com/watch?v=OpaiGYxkSuQ
I use sometimes such wrappers to customize commands.
ls () { command ls -F "$@"; }
This defines a function wrapping the original command and keeps the PATH
variable variable.
精彩评论