Simple shell scripts

From Linux 101, The beginner's guide to all things Linux.

Jump to: navigation, search

This is the forth article on bash.

Contents

[edit] Multi-line input

By now you have made some typo in the shell. Perhaps you even saw something like this:

$ echo "
> 

And the cursor just sits there. This prompt, PS2 if you remember from the previous article, is to tell you bash is expecting further input before it begins execution.

You may wonder why it would have this feature. It was designed such that you could write a complete script at the shell prompt. This is advantageous to save you time if it is a simple task.

Note: This script that you write is not saved into a file, and after it passes out of the bash history, it is gone for good. If you wish to write more long-term scripts, the next sections will address this.

[edit] Script files

Shelling script files do not require any extension, but you may see them with the .sh ending. There are two ways to call the script file:

./scriptfile.sh
sh scriptfile.sh
  • The first method requires that the script have the file permission of +x (execution) for the user.
  • The second just requires more typing.

The first method also has a requirement that the first line in the file is:

#!/bin/sh

This line tells bash what interpreter to use. Some other common first lines include:

#!/usr/bin/perl
#!/usr/bin/python
#!/usr/bin/php

You need only choose the appropriate one for whichever language you are using. For shell scripting, however, you will always want to choose the to use the sh interpreter.

[edit] The test command

test is a simple program (/usr/bin/test) that will evaluate different things as true or false (ie, is this a file? a directory?, does it even exist?, and so forth).

In shell scripting you will utilize the test command very often, though at first you would not know it. This is because there is another name, /usr/bin/[ that is also the test program. You will see it used in the examples to follow. Although it is ugly to think of a command that is called an open bracket, it is just designed to save time typing up shell scripts.

Note: because [ is an actual program, it is important to be careful when employing spacing in shell scripting. For example, if[-f /etc/make.conf] and if [ -f /etc/make.conf ] are two different things, and the latter is the proper usage.

[edit] Conditional statements

This program checks to see if a file exists which you pass to the script on the command line, and outputs a message if so.


    file="$1"
    if [ -a "$file" ] ; then
         echo "Yes, $file exists"
    fi


NOTE: the -a option tells bash to check if a file exists. Also, you can use elif, and else, if you want to check for other conditions. elif has the same parameter layout as if, and else has no parameters

invoke this script with:

sh <scriptname> <file to check for>

Step by step breakdown:

1) a variable is created called "file" and it set to the first argument you pass into the script, the <file to check for>

2) the if statement checks to see if the file exists using the -a flag. notice the spacing, this is a requirement for conditional statements.

3) the script will send a message if the file exists, if it doesn't, oh well.

4) the if statement is ended by fi, a requirement.

[edit] Looping

This script will list the contents of a directory and prepend "Directory", "File", or "Symlink" before the proper listing. this employs a for loop which cycles through the ls output of a directory you pass into the script on the command line.

    directorytols=$1
    for filename in $( ls "$directorytols")
    do
         if [ -d "$filename" ] ; then
              echo "Directory: $filename"
         elif [ -h "$filename" ] ; then
              echo "Symlink: $filename"
         else
              echo "File: $filename"
         fi
    done

invoke this script with:

$ sh <scriptname> <directory to list>


Step by step breakdown:

1) the variable "directory" is set to the first argument passed into the script

2) a for loop is setup such that it will cycle through the number of elements provided by ls output, and each loop will set the variable "filename" to it's current value

3) if the current file denoted by "filename" is a directory, then output the appropriate message

4) else if the current file denoted by "filename" is a symlnk, then output the appropriate message

5) else, it was a normal file, and output that

The Bash Articles Series

The shells and specifically bash | The bash environment | Processes and signals | Simple shell scripts | Shell scripting
Personal tools