run bash script without input - bash

I have written a bash script which installs a number of packages, however for each consecutive package installation i am promoted with the following message:
After this operation, 1,006 kB of additional disk space will be used.
Do you want to continue [Y/n]?
Is there a way of setting the default to Y so no user input is required? My script is expected to run at night without any intervention
thanks in advance

Two methods come to mind. The first (and better option) is to use the options in your package manager. For example:
apt-get install -y [YOUR_PACKAGE]
if you use apt (type apt-get install --help for more help there).
The second is more of a quick-'n-dirty one...use a pipe after yes:
yes | apt-get install [YOUR_PACKAGE]
which always brings a smile to my face :p
The latter option also answers yes to ALL other questions, which can be handy (errors etc.) but can be risky (which is the reason those questions are there in the first place!)

I think that message looks like you are using apt-get.
In that case you can use the flag --assume-yes or shorter: -y, which should automatically answer yes to that question without prompting the user

Related

How to write Yes command as a answer to this question? Thanks

I am absolute beginner in coding. I took code from ChatGPT to install Anaconda. However, I stuck at last step. I tried almost all possible variants of yes. Could sb help me, pls? How I can write yes to last question? Thanks.
anaconda3 v2022.10 [Approved]
anaconda3 package files install completed. Performing other installation steps.
The package anaconda3 wants to run 'chocolateyinstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[A]ll - yes to all/[N]o/[P]rint):
To answer an interactive prompt that is being displayed by Chocolatey, type the letter enclosed in [...] that corresponds to the desired option, which in your case is y (for [Y]es), or - to also auto-confirm subsequent prompts, if any, during the installation at hand - a (for [A]ll - yes to all).
Case doesn't matter.
Press Enter to submit your choice.
To suppress (conceptually: auto-confirm) confirmation prompts, follow the guidance in the note you cite:
For a given installation command, add the -y option on its command line.
To persistently suppress confirmation prompts, for all future Chocolatey installation commands, run the following one-time command, from an elevated session (run as administrator):
choco feature enable -n allowGlobalConfirmation

which command doesn't work on my computer

In the past, we 'which' command to get the info of the relevant software on our computer, Like:
which python
which git
But now it seems don't work on my MacOS Mojave. Is there anything wrong with my setting?
New edition:
The result will turn out to be like this
AA:~ AA$ which python
/usr/bin/which: illegal option -- -
usage: which [-as] program ...
New edition2:
AA:~ AA$ type --all which
which is aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
which is /usr/bin/which
AA:~ AA$ type -all python
python is /Users/AA/anaconda3/bin/python
python is /Users/AA/anaconda/bin/python
python is /usr/bin/python
AA:~ AA$
The alias is what causes the error message. Apparently the alias definition is simply erroneous for your system. It's not clear what defined this alias or why; it seems wrong on several levels, so I doubt it's part of the standard install.
You can remove the alias with unalias which; but I'm guessing it's defined in one of your startup files, and should be removed from there - after this point, it should be gone for good the next time you log in.
But anyway, you should not be using which - it's better to accustom yourself to the POSIX standard command type. It was introduced specifically to replace which but it's apparently still hard to eradicate the old command from people's minds.
For me, this was when something from a yarn global upgrade replaced my system's /usr/bin/which.
/usr/bin/which -> ../local/share/.config/yarn/global/node_modules/.bin/which
Since your error message is exactly the same as the text strings inside that program, it is possible that your issue is from a similar source.
console.error('which: illegal option -- ' + flag)
console.error('usage: which [-as] program ...')
"Fixed" with reinstalling which through the system's package manager (e.g. sudo dnf reinstall which). That may now interfere with whatever JavaScript package depended on that, but I plan on removing those globals, so I will not find out.
Better fix: I feel like my system is dirty now and needs all of its bin files checked... Comparing everything in /usr/local/share/.config/yarn/global/node_modules/.bin to see if it has an equivalent in /usr/bin seems like an easy enough way to look for other points of interference.
for x in $(ls /usr/local/share/.config/yarn/global/node_modules/.bin/*); do
ls -l /usr/bin/"$(basename "${x}")"
done
Then those can be checked against the system's package manager with commands like the following. Then reinstalled if needed.
sudo dnf info ...
sudo dnf provides ...
More:
Seems weird to me that it would allow clobbering existing system programs. At the very least, I would expect it to have used /usr/local/bin instead. That extra system and system-like package management seems to be why they have done away with yarn global in yarn 2 (berry).
I have different packages for sudo yarn global list and yarn global list. Maybe using sudo at some point was my mistake?
P.S. Thanks goes to #tripleee, I was unaware of type. I have seen various other ways of attempting to handle different which programs and different versions of which, along with alternatives like test (and others I have forgotten), but type looks worth trying as a replacement.
P.P.S. Annoying to find out that this OS is configured to run tab completion through which.
P.P.P.S. Yes, I know scripting with the output ls is a bad habit.

Auto confirm an apt prompt in Bash

I am trying to make a bash script that will auto confirm the apt prompt when installing a package.
Here is what I try to auto confirm, and yes, I already tried "-y".
sudo apt install matchbox-keyboard -y
My rasp is in french and it keeps telling me that "-y" isn't recognized. Of course, since my rasp is in french, the prompt gets confirmed when I write "O" instead of "Y", but since I want my script to work with every language, I am looking for a way to use "-y" even on a french machine.
Does anyone of you know how to force the english confirm on a foreign language machine ?
Thanks in advance !

Can Crontab be used to manage packages if it requires input only some of the time

I essentially have a suite of python packages, binaries, and other programs requiring constant updates and I wanted to see if I could create a crontab entry that would run a BASH script that basically just runs these updates monthly. I have successfully created a crontab entry that calls a shorter test script I wrote for this purpose. The script is not the issue but it is as follows if it helps answer the question, which I doubt.
#!/bin/bash
#Program update automator script
#Leveraging crontab to update all my neuroimaging, python, and conda packages monthly without having to do it myself because I forget and am a lazy POS
#Note - Am Using Conda and Pip for package management and not apt-get, which I don't think I have installed because I had read somewhere you can get discrepancies in processes if you have apt-get and one of the other ones both on the system
#Created 12.27.17
#Updated 12.27.17
#Updates Conda itself and cleans unused packages and tarballs out
conda update conda && conda update anaconda && conda clean --packages --tarballs
#Update AFNI binaries
#update.afni.binaries -defaults -do_extras
#Updating important Python libraries
conda update python ipython ipython-notebook matplotlib networkx numpy scipy sphinx traits dateutil nose pydot
#Update Nipype libraries
pip install --upgrade nibabel nipype rdflib nipy dipy pygraphviz graphviz
Because I had just run all these commands prior to making this entry, testing it caused it to run without error. The problem is, when I run ONLY SOME of these updates when updates ARE required you get the whole:
The following packages will be updated: Proceed ([y]/n)? y
Which requires user input.
Is there a way I can automate the process of inputting 'y'?
There are a couple of issues I can see.
The most pressing, obviously, is that cron will crash if it gets to a point where it needs input, correct? I have not seen this myself yet but this is what I have read.
I am not sure how many times I will ever need to provide input, as it is always variable which commands will have programs that need updating and which will not, so it's not as if I will always need to input 3 'y's' and it's not as if the same commands will need the same input from run to run.
Is this just a bad idea?
My current crontab entry is as follows:
0 10 1 * * source /Users/mycomputer/.bash_profile; /bin/sh /Users/mycomputer/Desktop/auto_package_updater.sh
crontab(5) entries describe commands to be run periodically (or, with #reboot at boot time). These commands are interpreted by /bin/sh (the POSIX shell, see sh(1p)...) with a prior substitution of the % character.
Notice that these commands are running in a different environment (see environ(7)...) than your interactive shell has. Hence, you may need to set your PATH variable (and perhaps other environment variables, such as LD_LIBRARY_PATH if you need it) explicitly in your auto_package_updater.sh shell script (instead of source /Users/mycomputer/.bash_profile which smells really bad ...) .
is that cron will crash if it gets to a point where it needs input,
No, cron(8) is a daemon -started by init or systemd at boot time- and does not crash (unless you got hit by a bug in cron itself, which is very unlikely). It always runs (and if you crash it -which is highly unlikely, you cannot run any crontab job afterwards). What could happen is that your particular cron job is blocked ...
Is there a way I can automate the process of inputting 'y'?
You could consider using yes(1) (which emits a potentially infinite stream of y lines) and pipe its output (see pipe(7)). For example, if pip install --upgrade nibabel nipype could want many y you might put in your shell script:
yes | pip install --upgrade nibabel nipype
(but be cautious! are you so sure you always want that?)
BTW, I am not sure it is wise to entirely automate the update of your software. How would you handle the failure of these updates, or updates to a buggy version? I would recommend to download automatically, but update manually.... And you probably should avoid updating a Python program while that program is running....
Maybe you just want to write your own updating shell script (to avoid typing repetitive commands) but run that update_my_python_packages script yourself, when you know it is reasonable to do so.

Auto accept EULA in bash script

I'm trying to write a bash script that (among other things) installs ubuntu-restricted-extras, but I can't seem to find a proper solution to automatically accept the EULA. This has been suggested in another post:
yes | apt-get install -y ubuntu-restricted-extras
But this is not working for me. Any ideas?

Resources