How to write script to accept licence agreement in bash script - bash

#!/bin/bash
# To install java and jre
sudo apt-get install sun-java6-jdk sun-java6-jre
# END
While installing java and jre it will ask "Licence Agreement". How to write script to accept licence agreement.

(partly from here for background info)
export DEBIAN_FRONTEND=noninteractive
echo "sun-java6-bin shared/accepted-sun-dlj-v1-1 boolean true" |
debconf-set-selections
sudo -E apt-get install -y sun-java6-jdk sun-java6-jre
I highly recommend
http://groups.google.com/group/ec2ubuntu
for this kind of question. PaaS computing is the place to look for automatic deployment scripts :)

You can use expect:
#! /usr/bin/expect -f
apt-get install sun-java6-jdk
expect "[y/n]"
sleep 1
send "y\r"

Related

How to make apt assume yes and force yes for all installations in a bash script

I'm currently getting into linux and want to write a bash script which sets up a new machine just the way I want it to be.
In order to do that I want to install differnt things on it etc.
What I'm trying to achieve here is to have a setting at the top of the bash script which will make apt accept all [y/n] questions asked during the execution of the script
Question example I want to automatically accept:
After this operation, 1092 kB of additional disk space will be used. Do you want to continue? [Y/n]
I just started creating the file so here is what i have so far:
#!/bin/bash
# Constants
# Set apt to accept all [y/n] questions
>> some setting here <<
# Update and upgrade apt
apt update;
apt full-upgrade;
# Install terminator
apt install terminator
apt is meant to be used interactively. If you want to automate things, look at apt-get, and in particular its -y option:
-y, --yes, --assume-yes
Automatic yes to prompts; assume "yes" as answer to all prompts and run non-interactively. If an undesirable
situation, such as changing a held package, trying to install an
unauthenticated package or removing an essential package occurs then
apt-get will abort. Configuration Item: APT::Get::Assume-Yes.
See also man apt-get for many more options.
With apt:
apt -o Apt::Get::Assume-Yes=true install <package>
See: man apt and man apt.conf
If you indeed want to set it up once at the top of the file as you say and then forget about it, you can use the APT_CONFIG environment variable. See apt.conf.
echo "APT::Get::Assume-Yes=yes" > /tmp/_tmp_apt.conf
export APT_CONFIG=/tmp/_tmp_apt.conf
apt-get update
apt-get install terminator
...
You can set up API assume yes permanently as follow:
echo "APT::Get::Assume-Yes \"true\";\nAPT::Get::allow \"true\";" | sudo tee -a /etc/apt/apt.conf.d/90_no_prompt
Another easy way to set it at the top of the your script is to use the command alias apt-get="apt-get --assume-yes", which causes all subsequent invocations of apt-get to include the --assume-yes argument. For example apt-get upgrade would automatically get converted to apt-get --assume-yes upgrade" by bash.
Please note, that this may cause errors, because some apt-get subcommands do not accept the --assume-yes argument. For example apt-get help would be converted to apt-get --assume-yes help which returns an error, because the help subcommand can't be used together with --assume-yes.

How to Test a Bash File in Terminal

I've been trying to make a bash file for newbie Linux users and I wanted to know if there is a way to test the bash file before running it.
Can I just see the result of my bash file in the terminal and not actually run it?
For example, I don't want to actually update and upgrade my system when I run this script, I just want to see the result of my bash file, whether it gives me back some error or not.
Wanted to know if there is a way to just see the result, like see the result of my 'echo' commands and etc.
echo ---------------
echo hello and welcome to the automized bash file for your new linux distro!
echo ---------------
sudo apt-get update -y ; sudo apt-get upgrade -y ; sudo apt-get autoremove -y ; sudo apt-get autoclean -y ; sudo apt-get clean -y
echo ---------------
echo as you were drinking your coffee,
echo your linux distro got updated, and autocleaned as well!
Thanks in advance!
To see the results of running a bash file, a bash interpreter would have to interpret it. So the simple answer would be no.
However, if you are willing to use an online tool, you could run a bash script online. In this manner, you can see the results of running a bash script, without ever having to run it on your own machine.
A google search popped up these ones, but I cannot vouch for their legitimacy:
https://www.jdoodle.com/test-bash-shell-script-online/ (for evaluating the results of a script)
https://www.shellcheck.net/ (for assessing shell code quality)
There's no general way to run a shell script without running it. You can sometimes sort-of modify the script to make it go through the motions without actually doing anything significant, but this requires understanding the script and the commands in it.
For example, in the update script in the question, you could just add echo before each sudo apt-get command, something like this (note that I've reformatted it a bit, and added quotes around some fixed strings):
echo '---------------'
echo 'hello and welcome to the automized bash file for your new linux distro!'
echo '---------------'
echo sudo apt-get update -y
echo sudo apt-get upgrade -y
echo sudo apt-get autoremove -y
...etc...
This will simply print the commands, rather than executing them. (Note: if any commands had redirections, e.g. somecommand >outputfile or somecommand | anothercommand, the adding echo doesn't remove the redirection, so you'll need to make other changes as well).
If you want to actually see what the various apt-get commands would do if you ran them... you're in luck, because apt-get happens to have a --dry-run option (see the man page and this AskUbuntu question).
Note that this is a feature specific to apt-get. Very few shell commands have an option like this, so it's not like some sort of universal just-try-it-out switch. In fact, not even all apt-get subcommands support --dry-run.
Most relevantly, apt-get update doesn't support --dry-run! And it wouldn't be useful if it did. If you don't start by updating the package indexes -- actually updating them, not just pretending to -- then the other apt-get commands won't be able to tell what's new, and won't actually tell you what needs to be changed.
If you don't actually-for-real update the indexes, then you can't tell what the rest of the script would do if it ran for real. So you could do something like this:
...
sudo apt-get update -y
sudo apt-get upgrade --dry-run --assume-no
sudo apt-get autoremove --dry-run --assume-no
...etc...
...but be aware the script is actually executing, and while some of its effects have been disabled, others haven't.

Refactoring bash script that uses several if statements [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a bash script that checks if an installation exists and installs it if it doesn't exist based on the path.
Is there a better way to refactor this script so that there isn't as many if statements? So far, I've looked at if [ -f /usr/bin/go -a -f /usr/bin/python3 but the issue is that the install portion of the script doesn't flow nicely.
# chmod 744 to provide execute permissions on this file
echo Installing VIM, Python3, Docker, and Git
# Install VIM
sudo apt install vim
# install the required plugins
# Install Python3 and IDLE
sudo apt update
sudo apt install python3 idle3
# Install Docker but check to see if Docker already exists
if test -f /usr/bin/docker; then
echo "docker already exists here so I am skipping this intall for you."
else
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
fi
# sudo usermod -aG docker <user-name> to run docker commands without using sudo
# Install Git but check to see if Git already exists
if test -f /usr/bin/git; then
echo "git already exists here so I am skipping this install for you."
else
sudo apt install git
fi
# Install Go but check to see if Go already exists
# After installation, review the version and read this blog post https://www.jeremymorgan.com/tutorials/raspberry-pi/install-go-raspberry-pi/
if test -f /usr/bin/go; then
echo "golange already exists here so I am skipping this install for you."
else
sudo apt install golang
fi
echo Everything should be installed but check the logs for errors, thank you for your patience.
I think of something like this, using a for loop:
exes=( docker git go )
for i in "${!exes[#]}"; do
if type -p "${exes[i]}" &>/dev/null; then
echo "${exes[i]} installed"
unset exes[i]
else
echo >&2 "${exes[i]} not installed"
fi
done
sudo apt install "${exes[#]}"

Bash: Automatic reaction on press y or n

I'm starting to learn shell scripting.
I using Ubuntu and the APT and for example I will install apache with a shell script.
#!/bin/bash
sudo apt-get update
sudo apt-get install apache2
Eveythink work's but their prompt a message with
"Press y/n to install ..."
And I want that my script press automatic yes for me.
Thank's for help.
Use
$ sudo apt-get install apache2 -y
You can look here to learn more.
For any command that don't have a -y option as apt-get, you can use yes command :
yes | yourcmd
To give other input :
yes nothanks | yourcmd
Or with a delay between inputs :
while true; do echo "y"; sleep 1;done | yourcmd

How to detect whether apt-get requires a reboot using Bash?

I am writing a bash script (for apt-get based OS's) that automates the installations process of various programs. In this process I run "apt-get -fy update" and "apt-get -fy upgrade" sometimes. In the process of upgrading, occasionally, a restart is required.
My question: is there a way of testing if the system is asking for a restart after running "apt-get -fy upgrade"? I am trying to write the script for it to run from beginning to end without human any intervention.
Thank you.
Use the file /var/run/reboot-required which does exactly what you want. So we will have this:
apt-get update && apt-get -fy upgrade && [ -f /var/run/reboot-required ] && shutdown -r now
I don't recall whether apt-get actually gives you a predictably formatted message informing you whether a restart is necessary, but if it does you could just check the output, e.g. something like apt-get -fy update | grep -q 'fill in restart message pattern' && reboot.
Another probably less reliable alternative is to use checkrestart from the debian-goodies package.
If you do a
apt-get -fy update && shutdown -r now
it will respect the order and will update until finish and finally restart your server.

Resources