Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xargs: exec command with prompt

Tags:

xargs

I'm trying to do the following with xargs

pacman -Q | grep xf86-video | awk '{print $1}' | xargs pacman -R to remove all xf86-video-* driver on my machine. To make the question more clear, here is the output of pacman -Q | grep xf86-video | awk '{print $1}':

xf86-video-ark
xf86-video-ati
xf86-video-dummy
xf86-video-fbdev
xf86-video-glint
xf86-video-i128
xf86-video-intel
xf86-video-mach64
xf86-video-neomagic
xf86-video-nouveau
....

when I redirect the result to xargs, the output looks like this:

xargs result

The point is, the command which xargs is about to execute need user to do some additional input(as you can see it needs a Yes/No), but xargs automatically add a unknown symbol #, and exit, which causes my purpose UNACHIEVED.

WHY xargs would do this or, what can I do to use xargs for command with prompt?

like image 553
helsinki Avatar asked May 05 '15 05:05

helsinki


2 Answers

You can use

xargs -a <(pacman -Q | awk '/xf86-video/{print $1}') pacman -R

Explanation:

Without further arguments xargs does not work with interactive (command line) applications.

The reason for that is, that by defaultxargs gets its input from stdin but interactive applications also expect input from stdin. To prevent the applications from grabbing input that is intended for xargs, xargs redirects stdin from /dev/null for the applications it runs. This leads to the application just receiving an EOF. (Running just pacman -R SOMEPACKAGE and pressing Ctrl+D has the same effect).

To get xargs to work with interactive commands you have to use the --arg-file=FILE argument (short -a FILE). This tells xargs to get the arguments from FILE. This also leaves stdin unchanged.

So you could either put your package list into a temporary file

pacman -Q | awk '/xf86-video/{print $1}' > /tmp/packagelist
xargs -a /tmp/packagelist pacman -R
rm /tmp/packagelist

or you can use zsh's process substitution mechanism <(list). When executing a line with <(list), <(list) is replaced by a filename from where the output of list can be read.

xargs -a <(pacman -Q | awk '/xf86-video/{print $1}') pacman -R

The single # you get is not from xargs but from zsh itself. If the shell options PROMPT_CR and PROMPT_SP are set (which both are by default) zsh tries to preserve partial lines, that is lines that did not end with a newline. To signify that such a line has been preserved zsh prints an inverse+bold character at the end of that line, by default % for normal users and # for root.

like image 163
Adaephon Avatar answered Dec 06 '22 16:12

Adaephon


You need to run pacman the second time with the --noconfirm option:

pacman -Q | grep xf86-video | awk '{print $1}' | xargs pacman -R --noconfirm

This will disable 'are you sure' messages, and do things without requiring input.

like image 23
Steve K Avatar answered Dec 06 '22 15:12

Steve K