Admin Essentials

How to script: A Bash crash course

An easy step-by-step guide to the Bash command-line shell and shell scripting

Page 6 of 6

Now, let's make this a little more complex and allow for the match pattern to be specified on the command line:

#!/bin/bash

# We declare two variables for the two directories

pattern=$1
firstdir=dir1
seconddir=dir2

# The for loop that moves the right files

for i in 'grep -l "$pattern" $firstdir/*'; do
        mv $i $seconddir
        echo $i

done

You can see here that we have a new variable called $pattern, which is set to $1. The expression $pattern=$1 tells Bash to take the first argument given to the script on the command line and place it in the $pattern variable. The value of $pattern is then used by grep to find the files. If we named this script movefiles.sh, we would run it like so:

./movefiles.sh matchpattern

Here's an alternative:

./movefiles.sh otherpattern

Now we have a script that lets us search for any pattern we like. However, there are no protections in place if the user does not enter an argument on the command line. In order to avoid problems, let's put in a little bit of error checking:

#!/bin/bash

if [ -z $1 ]; then
        echo "No pattern given."
        echo "Usage: $0 <pattern>"
        exit
fi

# We declare two variables for the two directories

pattern=$1
firstdir=dir1
seconddir=dir2

# The for loop that moves the right files

for i in 'grep -l "$pattern" $firstdir/*'; do
        mv $i $seconddir
        echo $i
done

We've added a check at the top to make sure that something was entered on the command line. We use the Bash built-in test mechanism to check if the length of $1 is nonzero. If the length of $1 is zero, then clearly nothing was entered. In that case, our if/then statement echoes that there was an error, shows the proper usage of the script, then exits. Note the $0 here. That is a special variable that contains the name of the script.

The Bash built-in test is represented by the [ and ] brackets. Bash will evaluate the statement within the brackets and determine if it's true or false or a variety of other qualifications. In this case, we use the -z flag to test the length of the text in the $1 variable. If the length of $1 is nonzero (that is, the statement in the brackets is false), the if/then statement skips processing and the script runs normally. Testing is a relatively complex evaluation mechanism, and it can be used in myriad ways. You can learn about the full scope of the tool by running man test.

The more time you spend with Bash -- or with any shell -- the more reflexes you will develop and the more natural it will feel. Eventually you will stop thinking about the specific commands or operators. Instead, you will see only the end goal, and getting there will become a simple matter indeed. We don't think about moving a mouse to click a button in a GUI, and eventually, you won't think about which flags to use with ls or grep, or how to use pipes and operators and loops to manipulate some data. You'll just do it.

This article, "How to script: A Bash crash course," was originally published at InfoWorld.com. Follow the latest developments in Linux, open source software, and the data center at InfoWorld.com. Get a digest of the key stories each day in the InfoWorld Daily newsletter. For the latest business technology news, follow InfoWorld on Twitter.

| 1 2 3 4 5 6 Page 6
From CIO: 8 Free Online Courses to Grow Your Tech Skills
View Comments
Join the discussion
Be the first to comment on this article. Our Commenting Policies