User Tools

Site Tools


ops102:bash_scripting

This is an old revision of the document!


NOTE: This page is being edited and is NOT ready for use.

Bash Scripting

What is a script?

A shell script is a computer program which is interpreted by an operating system shell.

Scipts are used to automate procedures that could be manually performed from the command line. They can potentially save a huge amount of time by eliminating repetitive commands. For example, if you're going to compile and test a program 100x, and each compilation and test cycle requires 25 steps (commands), you're looking at performing 2500 steps. It's much more efficient to create a script containing those 25 steps and run it as needed – in fact, you can even set things up so those commands execute automatically as soon as you save a new version of your program.

Basic Requirements for Shell Scripts

1. Create a text file containing shell commands. Use any text editor (nano, vi, VS Code, gnome-text-editor, eclipse, …) to create the file.

2. Tell the operating system which shell to use. Add a “shbang” line to the very top of the file, containing the text:

#!/usr/bin/bash

The first two chacters, the sharp (#) and bang (!) give this line its name. They are recognized by the operating system kernel as identifying a script. The remainder of this line is interpreted by the kernel as the name of the shell which is to be used to interpret the script. In this case, /usr/bin/bash is the absolute path of the bash shell. You can substitute other interpreters to write scripts in other shell dialects (such as the Z-shell, /usr/bin/zsh) or languages (such as python, /usr/bin/python).

Note that there must be nothing in font of the #! characters – no space and no blank lines.

3. Ensure that the script has appropriate permissions. The kernel requires execute [x] permission, and the shell requires read [r] permission. Set this up with the chmod command (for example, chmod u+rx scriptname).

Here is a simple example script using two commands, echo and date:

#!/usr/bin/bash
echo "The current date and time is:"
date

Notice the presence of the shbang line.

If this is save into the file named “now”, the permission could be set with this command:

$ chmod u+rx now

The script can then be executed. Normally, the current working directory is not searched, so to run the a script in the current directory, you will need to explicitly specify the directory name like this:

$ ./now
The current date and time is:
Sat Mar  6 12:03:32 EST 2038

Variables

Setting a Variable

To set a variable, simply type the variable name, an equal sign, and the variable value:

A=5
B=World

If the variable does not exist, it will be created. If it does exist, the previous value will be discarded.

Variable names may contain letters, digits, or underscores, but must not start with a digit.

Unlike some computer languages such as C, variables do not need to be declared. Variables are not typed – they may be used as strings, integers, or decimal values.

Accessing a Variable

To access a variable, place a dollar sign [$] in front of it, and use it in a command as an argument (or as a command name):

$ B=World
$ echo $B
World
$ echo Hello $B
Hello World

Word Splitting and Quoting

Spaces and tabs are used to split a bash command line into individual “words”. This means that if you use an argument that contains a space, it will be treated as two separate arguments:

$ mkdir test
$ cd test           # this is new so it will be empty
$ touch new file
$ ls -l
total 0                                                                                                                 
-rw------- 1 chris.tyler users 0 Mar  6 12:12 file                                                                      
-rw------- 1 chris.tyler users 0 Mar  6 12:12 new    

Notice that touch new file created two files, because new and file were treated as separate arguments.

To prevent word splitting, quote the text.

$ mkdir test
$ cd test          # this directory is new so it will be empty
$ touch "new file"
$ ls -l 
total 0                                                                                                                 
-rw------- 1 chris.tyler users 0 Mar  6 12:15 new file 

Notice that only one file was created by the command touch “new file” because the quotes prevented word splitting, and the single argument new file was used as a single filename.

You can quote text with single-quotes ['] or double-quotes [“]. Variable expansion takes place inside double quotes, but not inside single quotes:

$ B=World
$ echo "Hello $B"    # notice that $B is replaced with the value of B
Hello World
$ echo 'Hello $B'    # notice that $B used as-in
Hello $B
ops102/bash_scripting.1709745566.txt.gz · Last modified: 2024/04/16 18:10 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki