Scripting - Chapter 1: Getting to know Bash

From PlayOnLinux
Revision as of 14:48, 22 March 2015 by Petch (talk | contribs) (Add an advanced tip about using playonlinux-bash)
Jump to: navigation, search

Introduction

You would like to create your own scripts for PlayOnLinux but you don't know squat about programming? This tutorial will explain the basics and you'll soon be a pro in playonlinuxian Bash.

Bash?

Bash is a command interpretor (or "shell") in GNU/Linux and Mac OS X. It's the one appearing when you launch a terminal. It also allows you to make scripts. A script is a list of commands that your shell will execute one after the other. PlayOnLinux has advanced functions in Bash to make the scripting process easier. Let's get acquainted with them.

Requirements of every script

Every PlayOnLinux script must contain this boilerplate code:

#!/usr/bin/env playonlinux-bash
[ "$PLAYONLINUX" = "" ] && exit 0
source "$PLAYONLINUX/lib/sources"

POL_SetupWindow_Init

POL_SetupWindow_Close
exit
Do not copy the line numbers to the script. It is also highly recommended that you type it out by hand while learning, as opposed to copying and pasting, so as to become more familiar with the scripting language.

The above code may not make sense to you yet. We will explain what it does as we go.

During this tutorial, I will not repeat this code over and over in the examples, as you will need it in every script. Remember to always include it, otherwise your script won't work.

Executing your script

Ask your favourite text editor to save your script, then use the function Run a local script from the Tools menu of PlayOnLinux.

Script signing

PlayOnLinux uses digital signing (authentication) on scripts to certify that scripts have been validated by PlayOnLinux scripters. All the scripts available from the Install window of PlayOnLinux are validated. Each time you will try to run a script that has not been validated by PlayOnLinux scripters, you will get a warning message telling you that the script has no valid signature (see screenshot below).

PlayOnLinux Wizard Run a local script

Warning!!

The signature of the script you are trying to run is NOT valid.

PlayOnLinux has not approved this script and will not be responsible for it. Please ensure you know what you are doing!


You must click on Next button to get PlayOnLinux to show you the script source, then click on the I Agree checkbox, before you can (finally) run the script (see screenshot below).

PlayOnLinux Wizard Run a local script

Here is the source code of the script. Check it carefully:

#!/usr/bin/env playonlinux-bash
[ "$PLAYONLINUX" = "" ] && exit 0
source "$PLAYONLINUX/lib/sources"
 
POL_SetupWindow_Init
 
POL_SetupWindow_Close
exit
I agree

Experienced Developer Tip

While this tip is not required for this tutorial, it can be a real time saver for experienced script developers: If you're familiar with the command line interface, you may have guessed from the first line of the script above that PlayOnLinux scripts can also be run using the playonlinux-bash command; Written exactly as above, you can even set the executable bit on PlayOnLinux install scripts!