How do I write a shell script to install a list of applications?
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?
5 Answers
I would assume the script would look something like this:
#!/bin/sh
apt-get update # To get the latest package lists
apt-get install <package name> -y
#etc.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 tells apt-get not to prompt you and just get on with installing)
Well, according to your question the easiest script would be:
#!/bin/sh
LIST_OF_APPS="a b c d e"
aptitude update
aptitude install -y $LIST_OF_APPSHowever 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
cat example.list | xargs sudo apt-get -y install 2 #!/bin/bash
set -eu -o pipefail # fail on error and report it, debug all lines
sudo -n true
test $? -eq 0 || exit 1 "you should have sudo privilege to run this script"
echo installing the must-have pre-requisites
while read -r p ; do sudo apt-get install -y $p ; done < <(cat << "EOF" perl zip unzip exuberant-ctags mutt libxml-atom-perl postgresql-9.6 libdbd-pgsql curl wget libwww-curl-perl
EOF
)
echo installing the nice-to-have pre-requisites
echo you have 5 seconds to proceed ...
echo or
echo hit Ctrl+C to quit
echo -e "\n"
sleep 6
sudo apt-get install -y tigExplanation:
set -eu -o pipefailcommand:Command elements Explanation setModify how the shell environment operates -uIf a variable does not exist, report the error and stop (e.g., unbound variable) -eTerminate whenever an error occurs (e.g., command not found) -o pipefailIf a sub-command fails, the entire pipeline command fails, terminating the script (e.g., command not found) If this script encounters any errors, it will fail and exit.
Ref:
sudo -n truecommand:Command elements Explanation sudoRun as superuser -nNon-interactive. Prevents sudofrom prompting for a password. If one is required,sudodisplays an error message and exitstrueBuiltin command that returns a successful (zero) exit status Run as a superuser and do not ask for a password. Exit status as successful.
Ref: ,
test $? -eq 0 || exit 1 "you should have sudo privilege to run this script"command:Command elements Explanation testTakes an expression as an argument, evaluates it as '0' ( true) or '1' (false), and returns the result to the bash variable$?$?A variable used to find the return value as the exit status of the last executed command -eqequals 0Value result is true ||Logical "OR" is a Boolean operator. It can execute commands or shell functions based on the exit status of another command exitExits the shell with a status of N. If N is unspecified, it uses the exit code of the last executed command 1Value result is false and used here as an argument to the exit command to use as an exit code "you should have sudo privilege to run this script"If the exit code is false, print this message to the terminal 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: , , ,
echo installing the must-have pre-requisitescommand:Command elements Explanation echoBuiltin command used to print information or messages to the terminal installing the must-have pre-requisitesMessage to print to the terminal Tell the user that it is going to install some pre-requisite packages before installing the actual program.
Ref:
while read -r p ;command:Command elements Explanation whileCreate a while-loop, i.e. perform a given set of commands ad infinitum as long as the given condition evaluates to true readRead a line from the standard input and store it in a variable -rOption passed to readcommand that avoids the backslash escapes from being interpretedpArbitrary variable for readto store captured input. Here it represents each package to be installed;Control operator AND. Proceed to the next command and execute it regardless of the exit status of the previous command (execute even if the previous command fails) Read a given file line by line forever or until receiving a value of "false," then continue onto the proceeding command.
Ref: , , ,
do sudo apt-get install -y $p ;command:Command elements Explanation doReserved word used to delimit the sequence of commands which follow. i.e., start apt-getTool used by the Debian APT (Advanced Package Tool) package manager installA command used to install packages -yLong-form is --yes. assume "yes" on all query prompts$pUsed to call the arbitrary variable pfromreadand use it as standard inputInstall the list of packages as a superuser without prompting for confirmation to install.
Ref:
done < <(cat << "EOF" <list of packages> EOF)command:Command elements Explanation doneReserved word used to delimit the sequence of commands which precede. i.e., stop <Redirection to the standard input catConcatenate. used for viewing, creating, and appending files <<Redirection that reads input from the current source until encountering a delimiter and then using those lines as the standard input for a command EOFEnd of File delimiter cat << EOF-EOFThis will read, then print everything enclosed within the EOFblock<(list)Obtain the output of the list; parentheses indicate that the list will execute in a subshell environment Read the list of packages and gather them as standard input. Redirect it to the
readcommand, which captures it as thepvariable, and then sends it to the$pvariable, which allows it to get executed by theinstallcommand, and when it reaches theEOFdelimiter, redirect the output todoneeffectively ending thewhile readloop.Ref: ,
The following four echo messages are self-explanatory:
echo installing the nice-to-have pre-requisitesecho you have 5 seconds to proceed ...echo orecho hit Ctrl+C to quit
however, the next one is not.
echo -e "\n"command:Command elements Explanation -eEnable the function of backslash characters \nBackslash escaped sequence for new line This command creates a newline.
sleep 6command:Command elements Explanation sleepDelay the execution of a bash script, typically for N seconds, unless using an option to indicate longer lengths of time Delay the execution of the following command for 6 seconds.
Ref:
sudo apt-get install -y tigcommand:Install the
tigpackage with the Debianapt-gettool while running the installation as a superuser, and do not prompt for confirmation.
General references:
1I would opt for the following script: vim install
#!/bin/bash
apt-get update # To get the latest package lists
apt-get install $1 -yThen I should make the above script executable chmod +x install. Then to use it, I could type: ./install <package_name>. Example: ./install clang