Does anyone know how to write a shell script to install a list of applications? It's a pain to have to install each application by hand every time I set up a new system.
Edit:
It still asks me Do you want to continue [Y/n]?
. Is there a way to have the script input y
or for it not to prompt for input?
I would assume the script would look something like this:
Just save that as something like install_my_apps.sh, change the file's properties to make it executable, and run it from the command line as root.
(Edit: The
-y
tellsapt-get
not to prompt you and just get on with installing)Well, according to your question the easiest script would be:
However you could also enter
aptitude update && aptitude install -y a b c d e
. So maybe your question is missing the crucial point here. If there are some further requirements it would be nice to explain them.Just create a list of apps in a file, example.list, and run
Explanation:
set -eu -o pipefail
command:set
-u
-e
-o pipefail
If this script encounters any errors, it will fail and exit.
Ref: https://www.tutorialdocs.com/article/set-command-in-bash.html
sudo -n true
command:sudo
-n
sudo
from prompting for a password. If one is required,sudo
displays an error message and exitstrue
Run as a superuser and do not ask for a password. Exit status as successful.
Ref: https://linux.die.net/man/8/sudo, https://linux.die.net/abs-guide/internal.html
test $? -eq 0 || exit 1 "you should have sudo privilege to run this script"
command:test
true
) or '1' (false
), and returns the result to the bash variable$?
$?
-eq
0
||
exit
1
"you should have sudo privilege to run this script"
Test the last variable's exit code and see if it equals '0'. If not, exit with an error and print a given message to the terminal.
Ref: https://linuxhint.com/bash-test-command/, http://tldp.org/LDP/abs/html/exit-status.html#EXSREF, https://bash.cyberciti.biz/guide/Logical_OR, https://linuxize.com/post/bash-exit/
echo installing the must-have pre-requisites
command:echo
installing the must-have pre-requisites
Tell the user that it is going to install some pre-requisite packages before installing the actual program.
Ref: https://linuxhint.com/bash_echo/
while read -r p ;
command:while
read
-r
read
command that avoids the backslash escapes from being interpretedp
read
to store captured input. Here it represents each package to be installed;
Read a given file line by line forever or until receiving a value of "false," then continue onto the proceeding command.
Ref: https://linuxize.com/post/bash-while-loop/, http://linuxcommand.org/lc3_man_pages/readh.html, https://linuxhint.com/while_read_line_bash/, https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_04_09
do sudo apt-get install -y $p ;
command:do
apt-get
install
-y
--yes
. assume "yes" on all query prompts$p
p
fromread
and use it as standard inputInstall the list of packages as a superuser without prompting for confirmation to install.
Ref: https://itsfoss.com/apt-get-linux-guide/
done < <(cat << "EOF" <list of packages> EOF)
command:done
<
cat
<<
EOF
cat << EOF-EOF
EOF
block<(list)
Read the list of packages and gather them as standard input. Redirect it to the
read
command, which captures it as thep
variable, and then sends it to the$p
variable, which allows it to get executed by theinstall
command, and when it reaches theEOF
delimiter, redirect the output todone
effectively ending thewhile read
loop.Ref: https://linuxhint.com/cat-command-bash/, https://linuxhint.com/what-is-cat-eof-bash-script/
The following four
echo
messages are self-explanatory:echo installing the nice-to-have pre-requisites
echo you have 5 seconds to proceed ...
echo or
echo hit Ctrl+C to quit
however, the next one is not.
echo -e "\n"
command:-e
\n
This command creates a newline.
sleep 6
command:sleep
Delay the execution of the following command for 6 seconds.
Ref: https://linuxhandbook.com/bash-sleep/
sudo apt-get install -y tig
command:Install the
tig
package with the Debianapt-get
tool while running the installation as a superuser, and do not prompt for confirmation.General references:
I would opt for the following script:
vim install
Then I should make the above script executable
chmod +x install
. Then to use it, I could type:./install <package_name>
. Example:./install clang