Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prompt a user from a script run with xargs

Tags:

bash

xargs

I'm using xargs to populate the arguments to a script in which I want to stop the script, waiting for user input. Something like:

echo a b c | xargs bash -c 'for a in "$@"; do echo $a; read; done'

but the read gets ignored. It seems that the second script is trying to get it's input from the pipe too? I've tried xargs -p but it's no better.

like image 226
drevicko Avatar asked Apr 19 '12 07:04

drevicko


People also ask

How do you pass arguments to xargs?

The -n ( --max-args ) option specifies the number of arguments to be passed to the given command. xargs runs the specified command as many times as necessary until all arguments are exhausted. In the following example, the number of arguments that are read from the standard input is limited to 1.

What is xargs in shell script?

The xargs command is used in a UNIX shell to convert input from standard input into arguments to a command. In other words, through the use of xargs the output of a command is used as the input of another command.

What is xargs option?

xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is echo) one or more times with any initial-arguments followed by items read from standard input.


1 Answers

If the option -a is given to xargs, the arguments will be read from a file instead of stdin. You can use bash's process substitution with the syntax <( ... ) to create the file on the fly.

xargs -a <( echo A B C ) bash -c 'for x in "$@"; do echo $x; read; done'

Note that here$@misses the first argument ('A' in this case). This is because bash -c puts 'A' into $0 (which normally takes the name of the script file), and $@ provides $1, $2 etc... (in this case 'B' and 'C').

like image 185
nosid Avatar answered Sep 24 '22 17:09

nosid