You are not logged in.

#1 2018-04-12 06:49:09

Adderall
Member
From: New York City
Registered: 2017-06-18
Posts: 30

How to automatically force a "yes" answer to prompts?

Hi, newbie here, trying to learn a bit more about the Arch command line. I am trying to make a script (which I named Scc.sh) where, after I type,

sudo pacman -Scc

the following two prompts would automatically be answered with "yes," but I cannot figure out how to do so; thus far, I have only been successful in getting the first prompt being automatically answered. After trying different methods and reading a few MAN pages, the best I came up with is this:

echo "y" | sudo pacman -Scc \n
echo "Y"

which produces the following output:

[europa@Nemesis ~]$ ./Scc.sh 

Cache directory: /var/cache/pacman/pkg/
:: Do you want to remove ALL files from cache? [y/N] y
removing all files from cache...

Database directory: /var/lib/pacman/
:: Do you want to remove unused repositories? [Y/n] Y
[europa@Nemesis ~]$

There is no confirmation in the second prompt of unused sync repositories being removed. However,

sudo pacman -Scc

returns the following output:

[europa@Nemesis ~]$ sudo pacman -Scc

Cache directory: /var/cache/pacman/pkg/
:: Do you want to remove ALL files from cache? [y/N] y
removing all files from cache...

Database directory: /var/lib/pacman/
:: Do you want to remove unused repositories? [Y/n] Y
removing unused sync repositories...
[europa@Nemesis ~]$

Now, I know this may seem like a weird question, so before anyone gets mad and starts insulting me, I'd like to mention that all I am attempting to do is learn/understand the command line more, as I have fell completely in love with Arch since switching over from Ubuntu a couple years back.

Thank you in advance for your help, and have a great day.


"Sometimes you win, sometimes you learn."

Offline

#2 2018-04-12 07:16:10

amish
Member
Registered: 2014-05-10
Posts: 475

Re: How to automatically force a "yes" answer to prompts?

man yes

Also see man pacman (--noconfirm option)

Last edited by amish (2018-04-12 07:21:24)

Offline

#3 2018-04-12 07:17:44

yttrium
Member
Registered: 2017-02-19
Posts: 11

Re: How to automatically force a "yes" answer to prompts?

man pacman: https://www.archlinux.org/pacman/pacman.8.html
The option  --noconfirm suppresses confirmation prompts. The warning says "Bypass any and all “Are you sure?” messages. It’s not a good idea to do this unless you want to run pacman from a script.", which apparently is what you want to do.

Offline

#4 2018-04-12 08:38:54

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Re: How to automatically force a "yes" answer to prompts?

Adderall wrote:

before anyone gets mad and starts insulting me

mad lol


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#5 2018-04-14 08:19:04

a821
Member
Registered: 2012-10-31
Posts: 381

Re: How to automatically force a "yes" answer to prompts?

Maybe check paccache, if your aim is to keep you pacman cache clean.

Offline

#6 2018-04-14 11:16:59

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: How to automatically force a "yes" answer to prompts?

Adderall wrote:

the best I came up with is this:

echo "y" | sudo pacman -Scc \n
echo "Y"

... all I am attempting to do is learn/understand the command line more.

Then this example can be useful.  The difference between the two `echo "y"` commands is that the output of only one of them is sent to the stdin of the pacman command.  The second `echo "y"` simply runs on it's own *after* pacman is done and has exited.  As a first incremental improvement for instructional purposes, you could do this:

( echo "y"; echo "y" ) | sudo pacman -Scc

This creates a subshell (the parentheses) within which two echo "y" commands are run.  The output of that entire subshell is sent to pacman's stdin, so pacman now gets two "y"s in a row which will accomplish what you were trying to do (though I'm making no statement here on whether that is worth doing).

Now creating a subshell just to run two sequential echo commands is a bit silly, so we can get rid of that and just use one echo command that outputs the two "y"s each followed by a newline.  This will have the same effect as the example above (also acheiving what you intended to do):

echo -e "y\ny" | sudo pacman -Scc

Now, given that others have realized there may be times where you would just want to generate a string of "y"s followed by newlines to feed as input to another process, a command was created to do just that: it creates a string of "y"s followed by newlines as long as it's output is connected.  So this too will do the same as the above:

yes | sudo pacman -Scc

Last edited by Trilby (2018-04-14 11:17:43)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#7 2021-05-31 04:26:52

abiral
Member
Registered: 2021-05-27
Posts: 2

Re: How to automatically force a "yes" answer to prompts?

use pacman -help to view list of commands
pacman -S --help to view options
there you can see --noconfirm

Offline

#8 2021-05-31 08:01:54

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 23,447

Re: How to automatically force a "yes" answer to prompts?

Please don't necrobump 3 year old topics. Also --noconfirm is explicitly not a solution here since that will answer "No" to prompts where N would be preselected (and has been mentioned two times prior).

Closing.

Offline

Board footer

Powered by FluxBB