0

As I play more and more with Ubuntu, I'm starting to see how I can automate the majority of the repetitive commands that I perform whenever I reinstall the operating system. Thus far, it's just one gigantic apt-get script/terminal command list that's connected by dozens of && symbols.

I used to be decent at python, but I've forgotten quite a lot. Here's what my apt-get command looks like right now (which I simply paste in and press enter):

sudo add-apt-repository -y ppa:numix/ppa && sudo add-apt-repository -y ppa:gwendal-lebihan-dev/hexchat-stable && sudo add-apt-repository -y ppa:webupd8team/nemo && sudo apt-get update && sudo apt-get install -y numix-icon-theme numix-gtk-theme docky hexchat nemo nemo-fileroller dconf-tools vlc unity-tweak-tool gnome-tweak-tool python-pip && sudo pip install speedtest-cli && gsettings set org.gnome.desktop.background show-desktop-icons false && xdg-mime default nemo.desktop inode/directory application/x-gnome-saved-search && gsettings set org.gnome.desktop.interface gtk-theme "Numix" && gsettings set org.gnome.desktop.interface icon-theme 'Numix-Circle' && gsettings set com.canonical.desktop.interface scrollbar-mode normal

I know, it's not exactly clean. I don't want to keep doing it this way and I'd like to turn it into a script of sorts that would be far easier for me to update, maintain, and read. Here's some pseudo code I typed up of what I'd like to do.

#Install/Config Script

#Add Repos
print ("Adding Numix, Hexchat, and Webupd8 Repositories...")
enter.line ('sudo add-apt-repository -y ppa:numix/ppa && sudo add-apt-repository -y ppa:gwendal-lebihan-dev/hexchat-stable && sudo add-apt-repository -y ppa:webupd8team/nemo')

#Update Repos
print ("Updating repostiory lists...")
enter.line ('sudo apt-get update')

#Install apps
print ("Installing Numix Icon Theme...")
enter.line ('sudo apt-get install -y numix-icon-theme')
print ("Installing etc etc...")

#Configure Nemo File Explorer
print ("Configuring Nemo as default file manager")
enter.line ('gsettings set org.gnome.desktop.background show-desktop-icons false')
enter.line ('xdg-mime default nemo.desktop inode/directory application/x-gnome-saved-search')

#Set Numix Theme and fix scrollbars
print ("Setting Numix GTK and Icon Set...")
enter.line ('gsettings set org.gnome.desktop.interface gtk-theme "Numix"')
enter.line ('gsettings set org.gnome.desktop.interface icon-theme 'Numix-Circle'')
enter.line ('gsettings set com.canonical.desktop.interface scrollbar-mode normal
')

Essentially, I'd like to be able to save it as some sort of executable script, and simply run it in the terminal and let it do its thing. I'm familiar with Java, C++, and Python, but it has been a long time since I have coded.

Any suggestions for someone trying to get back into the game would be much appreciated! Please note I am not asking for anyone to do all the work for me. I'm looking forward to trying and failing as I figure it out on my own, I'd just love for some input on how to start, what language to use, organizational layouts, and any other helpful tips for getting started.

2 Answers 2

1

Use a shell script (bash):

#!/bin/bash
#above line ensures it runs with bash because of the '#!'
sudo apt-get update
sudo apt-get install -y numix-icon-theme package2 package3 package4
sudo apt-get install -y package5 package6 package7 etc

gsettings set org.gnome.desktop.interface gtk-theme "Numix"

Alternatively, if you know your preferences will change, you could write a script that reads in an input file:

#newserver.in
install,numix-icon-theme,package2,package3
install,package4
custom,gsettings,"set org.gnome.desktop.interface gtk-theme \"Numix\""

Reading an input file

#!/bin/bash
infile=foobar.in
thisserver=""

cat $infile | while read line
do
   case $line in
      "#"*) [ ];; #matches commented lines
      "") [ ]  ;; #matches empty lines
      *)          #matches the rest
         #echo $line 
         while IFS=, read val1 val2 val3 val4 val5 val6
         do
            #echo "val1=$val1   val2=$val2   val3=$val3   val4=$val4"
            #echo "$thisserver    $line"
            if [[ "$thisserver" == author* ]] && [ "$val1" != "SERVER" ];
            then
               echo "$val1 $val2 wrote during the $val3"
            elif [[ "$thisserver" == movies* ]] && [ "$val1" != "SERVER" ];
            then
               echo "$val1 ($val2) rates $val3 on imdb.com"
            fi
            if [ "$val1" = "SERVER" ];
            then
               echo "$val2"
               thisserver=$val2
            fi

         done <<< $line
         ;;
   esac

done

foobar.in

#foobar.in

# example comment
SERVER,authors.example.com,
alexandre,dumas,1700s
robert,heinlein,1900s,science fiction, nothing to see here, extra properties

timothy,zahn,1900s,
mark,twain,1800s,
SERVER,movies.example.com,
The Avengers,2012,8,
Star Wars,1997,9,
Ratatouille,2007,8,
4
  • Many scripts that I write read in files. If you want me to summarize in this answer, I can.
    – Wally
    Commented Mar 14, 2014 at 14:15
  • If you could, that would be wonderful! It would make it a lot easier to simply add a new package whenever I need to update the script.
    – Harsha K
    Commented Mar 14, 2014 at 19:52
  • Ping. I added "Reading an input file."
    – Wally
    Commented Mar 17, 2014 at 13:34
  • I apologize for the late reply. This is great~ I'm playing around with it now and I'm looking forward to getting it working. Thank you!
    – Harsha K
    Commented Mar 23, 2014 at 5:56
1

Just save that line in a file called, say, do_stuff.sh. And run it like this . path/to/do_stuff.sh.

If you really want to do it properly just add #!/bin/bash as a first line and chmod +x path/to/do_stuff.sh. Then you can run it as an executable.

No need to (re)learn python - bash is a scripting language.

3
  • Interesting! I didn't realize bash scripting was that easy.
    – Harsha K
    Commented Mar 14, 2014 at 19:42
  • How do bash scripts know to execute the next line when the first line has finished? I know that ; means to execute regardless of the preceding command whereas && will only execute if the preceding command was successful. So which does bash pick by default?
    – Harsha K
    Commented Mar 14, 2014 at 20:09
  • newline is equivalent to a semi-colon Commented Mar 17, 2014 at 8:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .