Simple Bash Scripts

Simple Bash Scripts

·

4 min read

Shell scripting is a useful task for any engineer. While shell scripts get a lot of flack for not being a great scripting language, it is nevertheless a useful skill to have, and not just for DevOps engineers. Here are a few basic scripts, and commands for use in your shell.

Shebang and executable files.

In bash, when the path to a command is put in the terminal, it will be executed. An example would be the ls command, /bin/ls. Of course, the file would have to exist and be executable. Before a file is executed, it would need to have executable permissions. The user should also be allowed to execute that file. Now, the user can get these permissions by being in a group that holds them, or simply being given those permissions, or giving himself those permissions. As a matter of fact, the common Bash commands that we use are executed by the shell providing a path to their location and asking bash to execute them. All users seem to have access to these commands hence it is not a big deal.

In the same manner, when you write a script, you can make it executable and give its path to the shell to have it executed. To change the permissions, see the chmod command (man chmod). However, you can simply give all users permission to your file (script) by running chmod a+x file.

Another way is by using the bash command. In this way, you type bash followed by the file you want to execute, or its path.

By using bash, the file may not be an executable but it would still run.

Simple Scripts and the shebang

The shebang (#!) is a directive at the beginning of shell scripts that specifies the file is to be executed. It is followed by a path to the program that executes the proceeding lines. In this case, the program is bash hence the first line would be (#!/bin/bash).

Since it is a text file, it should end with a newline. This is because, in Unix (and its successors such as GNU/Linux, each line is considered to end with a newline. A file that does not end with a newline is therefore not a text file. You can read more below:

.

Listing directories and changing them.

While the pwd command is used for listing the path to the current directory, its script would be simple.

#!/bin/bash
pwd

To list its contents, you'd simply use the ls command

#!/bin/bash
ls

to list them in long format, or any other options, (man ls), simply add them to your script.

#!/bin/bash
ls -l

The ls command takes various option to list files in various ways as well as listing files. The -a option allows one to list hidden files. These options can be easily manipulated together so that listing files in long format along with hidden files use ls -al . To list the contents of several directories, simply give them to ls in the order you want the contents listed. e.g. to list the contents of the parent and current directory in long format including hidden files:

#!/bin/bash
ls -al . ..

On changing directories, while the cd command asks for a path to be specified, using it without any path(cd) takes the user back to their home directory. To move to the previous directory use the (-) hyphen as the argument to cd, (cd -).

To create a directory use the mkdir command. Now to create a directory in a different folder, give the path as it is needed. i.e. to create a directory "reginald" in /tmp/ directory:

#!/bin/bash
mkdir /tmp/reginald

To create a directiry within another directory that does not exists yet, use the -p flag. e.g. to create a directory james, within joy that did not exists use mkdir -p joy/james.

Moving Files

The mv command moves a file to a different location. It does this by specifying the path to the original file then giving it a path to the new location. The (.) represents the current directory while .. give the parent directory. The mv command also renames files. It works by giving the file the same path with a different name at the end.

To delete a file, the rm command is used. When used by itself, it deletes only files. However, when a directory is to be deleted, the -r flag can be used. Another way to delete directories is by using the rmdir which deletes empty directories. To delete a file in a different directory give the specified path to rm .e.g. to delete the file Betty in the directory /tmp/reginald`:

#!/bin/bash
rm /tmp/reginald/Betty

Files

To determine the type of file, use the file command. Give the path to the file whose type you want to determine to file, e.g. to determine the type of file betty in /tmp/reginald use the command file /tmp/reginald/betty.

These commands are some basic ones you'll need to perfom basic simple operations on the shell. You can practice them as you make more complex bash scripts. be sure to gain an understanding of the GNU/Linux file system for a better understanding of shell scripts and commands.