Introduction/Overview
- Understanding how to build pipelines in Unix scripts will help you with consistency and documentation. Bash scripts can string together many processing steps in simple or complex ways. For example, neuroimaging software like FSL and AFNI provide command line tools that can be called from a shell script. Like true programs, shell scripts use variables and control structures, and can take arguments. In fact, if you've played bashcrawl, you've seen what Bash shell scripts can do!
- Because you already have experience with Unix commands and configuration, shell scripts are not a big leap.
Shell scripts should be executable and in your search path:
The shell script should be made executable, e.g.,
- The shell script should be in your search path: Often, you will put your shell scripts in the
bin
directory in your home area, and make sure that~/bin
is added to your search path. (Remember, the~
stands for your home directory). - Tom Hicks discusses Unix shell scripting concepts in this 6-minute video.
Examples of Unix Shell Scripts
In this 10-minute video, Tom walks you through some detailed examples of shell scripts, including how to set the permissions, and run the script with one or more arguments.
Arguments are numbered variables:
$0
is the name of the script itself$1
is the first argument you want to pass in.
Shell Scripts differ from Programs
Finally, Tom explains the distinction between scripting and programming in this 2-minute video clip.
What language is the Script In?
- This lesson discussed writing scripts in the Unix shell language. This means you can use the Unix commands you've been learning by putting them together in a text file.
- However, you can write scripts in many different languages! So, how can you determine what language a script is written in?
- The first line of the file is often reserved for the shebang which tells the operating system which language to use to interpret the file:
#!/bin/bash
– Execute the file using the Bash shell
#!/usr/bin/env python3
– Execute with a Python interpreter, using the env program search path to find it
#!/usr/bin/Rscript
-Execute the file using the R interpreter
Optionally, the extension on the file may indicate the associated language (e.g.,script.py
is a Python script) - Be cautious when you copy code from the internet that you know what language it is in!
Resources
- Google, grepper, and copy-paste are your friends.
- Look for examples of what you want to do online. You should be able to find solutions that are close to what you want.
- You are welcome to peruse my bitbucket tools repository as well. You may find useful examples there.
- Keep in mind that there are many programming languages. A Python script is not the same as a shell script. When you copy code, pay close attention to what language it is written in.
- You may find this Scripting cheatsheet helpful.
- Here's a brief article on loops and conditionals in Bash: Use Bash to Automate Tasks
- The following YouTube site uses video lessons to walk you through commands. Thanks to Fatima Jebahi for pointing it out!
Digest
Stop and think about these topics
program
shebang
variables
control structures