|
| 1 | +# Introduction to UNIX (continued) |
| 2 | + |
| 3 | +In the 4th and last module of your unix course, we'll how to write small programs, or scripts. |
| 4 | + |
| 5 | +Shell scripts allow us to program commands in chains and have the system execute them as a scripted chain of events. They also allow for far more useful functions, such as command substitution. You can invoke a command, like date, and use it’s output as part of a file-naming scheme. You can automate backups and each copied file can have the current date appended to the end of its name. You can automate a bioinformatics analysis pipeline. |
| 6 | + |
| 7 | +Before we begin our scripting tutorial, let’s cover some basic information. We’ll be using the bash shell, which most Linux distributions use natively. Bash is available for Mac OS users and Cygwin on Windows (which you are using with MobaXterm). Since it’s so universal, you should be able to script regardless of your platform. |
| 8 | + |
| 9 | +At their core, scripts are just plain text files. You can use nano (or any other text editor) to write them. |
| 10 | + |
| 11 | +## Permissions |
| 12 | + |
| 13 | +Scripts are executed like programs. For this to happen, you need to have the proper permissions. |
| 14 | +You can make the script executable for you by running the following command: |
| 15 | + |
| 16 | +`chmod u+x my_script.sh` |
| 17 | + |
| 18 | +by convention, bash script are saved with the .sh extension. Linux doesn't really care about file extension, but it is easier for the user to use the "proper" extensions! |
| 19 | + |
| 20 | +## executing a script |
| 21 | + |
| 22 | +You have to cd in the proper directory, then run the script like this: |
| 23 | + |
| 24 | +`./my_script.sh` |
| 25 | + |
| 26 | +To make things more convenient, you can place scripts in a “bin” folder in your home directory and add it to your path |
| 27 | + |
| 28 | +`mkdir -p ~/bin` |
| 29 | + |
| 30 | +More information on how to correctly modify your PATH [here](http://unix.stackexchange.com/a/26059) |
| 31 | + |
| 32 | +## Getting started |
| 33 | + |
| 34 | +As previously said, every script is a text file. Still, there are rules and conventions to follow in order of you file being recognized as a script |
| 35 | + |
| 36 | +If you juste write a few command and try to execute it as is, with `./my_script`, it will not work. You can invoke `sh my_script`, but it is not very convenient. |
| 37 | +`./` tries to find out which interpreter to use (e.g. which programming language and how to execute your script). It does so by looking at the first line: |
| 38 | + |
| 39 | +The first line of your bash scripts should be: |
| 40 | + |
| 41 | +`#!/bin/bash` or `#!/usr/bin/env bash` |
| 42 | + |
| 43 | +The second version being better and more portable. Ask your teacher why! |
| 44 | + |
| 45 | +This line will have the same syntax for every interpreted language. If you are programming in python: |
| 46 | + |
| 47 | +`#!/usr/bin/env python` |
| 48 | + |
| 49 | +### New line = new command |
| 50 | + |
| 51 | +After the firstline, every line of your script will be a new command. Your first scripts will essentially be a succession of terminal commands. We'll learn about flow control (if, for, while, ...) later on. |
| 52 | + |
| 53 | +### Comments |
| 54 | + |
| 55 | +It is good practise to comment your scripts, i.e give some explanation of what is does, and explain a particularly arcane method that you wrote. |
| 56 | + |
| 57 | +Comments start with a `#` and are snippets of texts that are ignored by the interpreter. |
| 58 | + |
| 59 | +### Your first script |
| 60 | + |
| 61 | +Let's start with a simple script, that copy files and append today's date to the end of the file name. We'll call it `datecp.sh` |
| 62 | + |
| 63 | +In your `~/bin` folder: |
| 64 | + |
| 65 | +``` |
| 66 | +touch datecp.sh |
| 67 | +chmod u+x datecp.sh |
| 68 | +``` |
| 69 | + |
| 70 | +and let's start writing our script |
| 71 | + |
| 72 | +`nano datecp.sh` |
| 73 | + |
| 74 | +``` |
| 75 | +#!/usr/bin/env bash |
| 76 | +
|
| 77 | +# this script will copy a file, appending the data and time to |
| 78 | +# the end of the file name |
| 79 | +
|
| 80 | +``` |
| 81 | + |
| 82 | +Next, we need to declare a variable. A variable allows us to store and reuse information (characters, the date or the command `date`). Variables have a name, but can **expend** to their content when referenced if they contain a command. |
| 83 | + |
| 84 | +Variables can hold strings and characers, like this: |
| 85 | + |
| 86 | +`my_variable="hippopotamus"` |
| 87 | + |
| 88 | +or a command. In bash, the correct way to store a command in a variable is within the syntax `$()`: |
| 89 | + |
| 90 | +`variable=$(command –options arguments)` |
| 91 | + |
| 92 | +Store the date and time in a variable. Test the date command first in your terminal, then when you got the right format, store it in a variable in your script. |
0 commit comments