Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Unix / Bash, is "xargs -p" a good way to prompt for confirmation before running any command?

Tags:

bash

shell

unix

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://[email protected]//somepath/morepath

I can also do this

echo ssh://[email protected]//somepath/morepath | xargs -p hg push

The -p is the one that does the trick. This will be set as an alias, such as 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.

like image 622
nonopolarity Avatar asked Oct 14 '22 02:10

nonopolarity


2 Answers

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.

like image 150
Dennis Williamson Avatar answered Oct 20 '22 18:10

Dennis Williamson


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

like image 42
Ole Tange Avatar answered Oct 20 '22 19:10

Ole Tange