User Tools

Site Tools


This is an old revision of the document!

Programming in BASH

So far we've made a pretty cool script that takes the reverse complement of a DNA sequence.

Let's build on our previous work to make a full-fledged script.

Now, let's make it executable. That means that we don't have to type “bash” anymore. This will be important when we use our script as a command.

For this part,

  1. open your Terminal
  2. cd to Bash_Scripts
  3. type 'ls' to see your previous work

So far, the only way to run our script is by

$ bash revComp

and we must be in the same directory as revComp.

What we're really doing is executing bash, and giving our file as the argument to the bash program.

We're going to make a change to the permissions so we can execute our script directly.

Executable permissions

It's not executable yet. We can easily make it executable with chmod.

$ ls -l revComp
-rw-r--r-- 1 fileowner filegroupname 62 Jul 16 12:32 revComp
$ chmod 755 hello_world
$ ls -l hello_world 
-rwxr-xr-x 1 fileowner filegroupname 62 Jul 16 12:32 revComp

8-O Notice the x in the 4th, 7th, and 10th positions. This means that the file has execute permissions for the file owner, the file group, and everyone, respectively.

:!: Two common permission settings for scripts are:

  • 755 means everyone can execute,
  • 700 means only the file owner can execute.

To learn more about file permissions and these numbers, see file permissions.

$ chmod 755 revComp
reverse complement: GTGTACCGTACAGT
sequence length: 14

We removed the “bash” but replaced it with dot slash “./” which might seem like the same amount of work.

Final Steps

Currently, our script only works in the directory ~/Bash_Scripts.

For example:

$ cd
$ bash revComp TAG
bash: revComp: No such file or directory

When you log on, you start in your home directory. So you'll get the same result as above.

So how do you make the script work from anywhere?

We are going to:

  1. cd into your home directory
  2. make a new directory for finished scripts
  3. copy the finished script to the new directory
  4. make the directory visible to our shell

1. Go to your home directory.

$ cd
$ pwd # should be your home directory

2. Make the new directory for finished scripts. By convention, it is called bin

$ mkdir bin
$ ls # should see the new directory bin, plus Bash_Scripts and anything you created before

3. Copy the finished script to the new directory. Use the TAB key to help you not type so much.

$ cp Bash_Scripts/revComp bin/
$ ls bin

Is revComp there?


wiki/2018programming_in_bash.1531784055.txt.gz · Last modified: 2018/07/16 17:34 by david