HOWTO: a simple post-install script

Forum rules
Share your brain ;)
pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

HOWTO: a simple post-install script

Unread post by pidsley » Fri Jul 05, 2013 4:51 pm

I have been doing some simple net installs lately, and I always end up installing the same set of apps. I have a script that does this for me, but it also occurred to me that a similar script might be useful for people who whine about a BBQ release not having everything they want already bloating it up. So here is a simple script that installs some bloat for you. It doesn't check to see if you already have the latest versions (apt-get does that for you), it doesn't give you a spiffy dialog with options, and it doesn't wipe your ass.

If you want to add something to the install, simply define a new string at the beginning and make sure you add it to the last "docmd" line. If you don't like the apps I chose, change the strings at the beginning. If you don't want one of the categories I defined, just define it to an empty string, like this

Code: Select all

BROWSER=""
Otherwise, save this script somewhere in your $PATH, make it executable (chmod +x), and run it.

Code: Select all

#!/bin/bash

WM="openbox"
FILEMAN="ranger"
PLAYER="cmus"
TERM="urxvt"
BROSWER="netsurf"

function docmd() {
    cmd=$@
    echo $cmd
    read -n1 -p 'run command? (y/n/a) '
    echo
    ans=$REPLY
    if [ $ans == 'a' ]; then
        echo 'abort'
        exit 1
    elif [ $ans == 'y' ]; then
        echo -e '\nrunning '$cmd'\n'
        $cmd
    fi
}

docmd "sudo apt-get update"
docmd "sudo apt-get install --no-install-recommends" $WM $BROWSER $FILEMAN $PLAYER $TERM

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: HOWTO: a simple post-install script

Unread post by machinebacon » Fri Jul 05, 2013 10:47 pm

That's a cutie! I'll rewrite this one and add a whiptail :) Thank you Sir!
..gnutella..

JoeMama
Village Idiot
Posts: 54
Joined: Sat Jun 22, 2013 5:40 am
Location: Wisconsin

Re: HOWTO: a simple post-install script

Unread post by JoeMama » Sat Jul 06, 2013 3:53 am

Pidsley, You forgot that they should name the script as indolent_sniveller.sh :D [I kid, I kid]

So essentially this is an automated way of installing a given set of predetermined packages without prying open a terminal and simply doing the old "apt-get install shiznit" routine? Being unfamiliar with Bash and scripts my question is, can the string BROWSER be replaced by any string of one's choosing such as MAGICWINDOWTOINTERWEB as long as you have a legitimate package name such as "iceape" ?
As in MAGICWINDOWTOINTERWEB="iceape"
Or did you use the string BROWSER because it is a required "exact term" [for lack of a better way of putting it] ?
I'm also assumming that said strings MUST be capitalized
Can you add as many packages as you want as long as each string is added it to the last "docmd" line.
What if I wanted to install Chrome along with Iceape? Would I simply add the entries
MAGICWINDOWTOINTERWEB="chrome"
MAGICWINDOWTOINTERWEB="iceape"
FILEMAN="pcmanfm" ....

Is it even OK to have two identical strings used even though the packages are different? Or are no duplicate strings allowed. Which leads to, if you can't make up your own STRING and must use an exact predefined one, and no duplicate "category" STRING is allowed, i.e. only one BROWER="package" entry is allowed, are you basically shit outta luck and simply need to add the second Browser, chrome, thru the normal apt-get install routine?
TIME OUT..I need a cocktail...
In the end, this is a nice way for me to learn some stuff and get familiar with scripts. I appreciate this tutorial. Who knows how long it would have been for me to learn or discover this on my own.
One last thing, I assume it is best to do this outside of X if using a WM, DE. Or does that mainly apply to installing anything X related?
Time for a refill. Cheers.
I know that you believe you understand what you think I said,
but I'm not sure you realize that what you heard is not what I meant.

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: HOWTO: a simple post-install script

Unread post by rhowaldt » Sat Jul 06, 2013 11:06 am

@Joe: read up on some Bash, but:
- variable-names ('BROWSER') do not need to be in all-caps. this is just a convention that is nice because everybody can instantly spot the variables.
- you can name a variable anything you want. the only thing to keep in mind are the global variables already in place in your environment, the so-called 'environment variables'. hey, look, there's a Wiki entry for that: http://linuxbbq.org/wiki/index.php?titl ... t_variable
- if you want to do Chromium and Iceweasel together, you could do 'BROWSER="chromium iceweasel" or you could do 'BROWSER1="chromium" BROWSER2="iceweasel"' then call them both on the 'docmd' line. the method you describe does not work because the variables have the same name, and will thus overwrite.
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: HOWTO: a simple post-install script

Unread post by machinebacon » Sat Jul 06, 2013 11:30 am

Right, it would even be possible to give a new variable

APTINSTALL="sudo apt-get install"

and then call in docmd like:

docmd $APTINSTALL $BROWSER1

This would be helpful if you want the script to run distro-independent. A routine at the beginning of the program could determine which distro you use and then change the package manager accordingly.
..gnutella..

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: HOWTO: a simple post-install script

Unread post by rhowaldt » Sat Jul 06, 2013 12:04 pm

@Joe: you should really just see variables as containers for information-storage. say you do
INSULT="you are a penisface"
then go and type your rant:
echo "dear so-called friend, $INSULT - not to mention the fact that you are an idiot. btw, did i mention that $INSULT ? oh right, i must've forgotten. well, just to be sure, let me once again make clear to you that $INSULT"

so you see, in this way, you don't have to type all of 'you are a penisface' again and again each time you want to say it, you can just refer back to the variable that contains this string of text. the same goes with bacon's example of using an actual command inside a variable. all the script will do is say 'i will put this information here', expand the variable, and end up with the line
'docmd sudo apt-get install chromium'

fyi, 'docmd' (which in this case means 'do command') is a function, which gets called. this is simply a block of code with instructions that do something. instead of writing out the entire block each time you call it, pidsley wrote it into a function so he can just call the function instead. this saves time and makes the script clearer.

all of this goes to show you the One Ultimate Truth: programming is laziness.
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

JoeMama
Village Idiot
Posts: 54
Joined: Sat Jun 22, 2013 5:40 am
Location: Wisconsin

Re: HOWTO: a simple post-install script

Unread post by JoeMama » Sat Jul 06, 2013 3:32 pm

Rhowaldt, Pidsley, Bacon. I gotta make this quick cuz I'm leaving the house....

I apologize for a horribly written question that was kinda misleading. Although it doesn't seem like it, I never meant to ask a question about installing anything, let alone adjusting the script so one could install two browsers at the same time. It was an awful attempt at asking about some of the general fundamentals of scripts, regardless of what they were executing. I just tried to stay within the subject matter of the actual script by trying to create some retarded examples.
Unfortunately, that was Terrible. F-
The amazing part is that you still, somehow, managed to answer the questions I was attempting to ask, which were about variables. Rhowaldt, I gotta say, that was actually a well written, well explained and easy to understand write up. :D
I know that you believe you understand what you think I said,
but I'm not sure you realize that what you heard is not what I meant.

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: HOWTO: a simple post-install script

Unread post by rhowaldt » Mon Jul 08, 2013 9:30 am

don't worry Mr Mama, i understood your intent quite well, and i think so did the others (hence the useful answers). i am always happy to explain scripting-stuff to people (as long as i still know what i am talking about myself, i am by no means an expert), and examples like the one i've given you make it easier to think about these things because they are a bit less abstract than pure code. thanks for the compliment :)
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: HOWTO: a simple post-install script

Unread post by machinebacon » Mon Jul 08, 2013 11:43 am

Actually we can go through the script line by line:

Code: Select all

#!/bin/bash
This line tells which interpreter to use. Bash in this case.

Code: Select all

BROWSER="iceape"
FILEMAN="pcmanfm"
ICONS="moblin-icon-theme"
MAIL="sylpheed"
PLAYER="audacious"
TERM="lxterminal"
MANAGER="lxappearance"
These are the variables that don't need to be capitalized, but it's important that we put the equal and quote signs there.

Code: Select all

function docmd() {
# set of commands
}
That's a function, we only check the first and the last line. The function is called docmd and always remember to put the curly brackets there. Inside of this function there's a set of instructions and commands that are called. It's important to indent them when certain interpreters are used.

Code: Select all

# first Pidsley has set a variable. cmd, this time not capitalized, equals $@ - it shows the actual command that is going to be executed 
    cmd=$@
# as next the echo command displays the $cmd variable (that is $@) on the screen
    echo $cmd
# the 'read' command is waiting for user input
    read -n1 -p 'run command? (y/n/a) '
# a simple echo will display a blank line
    echo
# the variable ans equals the previously entered letter by the user
    ans=$REPLY
# if this letter is exactly equal 'a' then
    if [ $ans == 'a' ]; then
# the script will print 'abort' on the screen and
        echo 'abort'
# exit with exit code 1
        exit 1
# else, if the answer is 'y' then
    elif [ $ans == 'y' ]; then
# it first prints a message (that it's running the previously set command)
        echo -e '\nrunning '$cmd'\n'
# and finally calls the command
        $cmd
# and because it is an if-loop, we close the loop with 'fi'
    fi
I've added comments here.

Code: Select all

docmd "sudo apt-get update"
docmd "sudo apt-get install --no-install-recommends" $BROWSER $FILEMAN $ICONS $MAIL $PLAYER $TERM $MANAGER
Here the actual work is being done. Every docmd line runs the set of commands that were put into the function before. As rhosenbush pointed out, it's not only for lazy people, but also good to keep the code clean and unbloated :D
..gnutella..

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: HOWTO: a simple post-install script

Unread post by rhowaldt » Mon Jul 08, 2013 5:52 pm

ah that's so nice of you to go through the trouble of disecting that script Jules!

just one single tiny note: indentation is important in Bash for readability only, it is not a problem for the interpreter if you do not use it, it is simply a method to help humans read the code more clearly (same goes for comments). however, if you go into Python (which is fun and not difficult), indentation becomes mandatory (your script will break when not properly indented).
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: HOWTO: a simple post-install script

Unread post by machinebacon » Mon Jul 08, 2013 10:58 pm

Absolutely right, rhosey. I just mentioned indentation and forgot that my original intention (aha!) was to mention the interpreter in this context. Was already having supper in my mind :)
Thanks for your comment.
..gnutella..

ew.linux
Riesenpenis
Posts: 30
Joined: Sun Nov 11, 2012 12:56 am

Re: HOWTO: a simple post-install script

Unread post by ew.linux » Tue Nov 12, 2013 2:57 pm

Awesome for a noob like me that couldn`t have written it myself. But I will be able to modify it to my needs. Thanks for this one. As I always have a load of favourite-apps that I add to everything. This will simplify that task, and free up my time to hop trough even more distros than I currently do:)

Besides, the script is so well explained in this thread that I`m actually learning something too. Awesome:)
-ew
-------------------------------
...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it. (by Linus)

Post Reply