Bash provides other looping methods as well, such as
while loops. You can use
while loops to keep performing functions until a certain condition is met. For instance, you might want to perform some functions only on a few files, drawing on a text file with the names of those files. The
while loop would then be:
while read filename; do mv $filename $filename.old; done < ./filelist.txt
This command uses the
< operator to redirect the contents of the filelist.txt file into the loop, where each file name is placed into the
$filename variable. As soon as there are no more lines to be read from filelist.txt, the loop exits.
Bash shell scripting
Shell scripting plays a large role in the normal functions of a Unix-like system. Shell scripts are used by many distributions to start system services at boot, and by a wide variety of software packages to perform maintenance and configuration tasks. Shell scripting essentially forms the nuts and bolts of a Unix-like system.
Bash shell scripting is relatively easy to learn, and it can be surprisingly powerful. You can use shell scripts to automate common tasks or perform any other operation that you could normally run from the command line. Every shell script starts out with the hashbang, or
The hashbang is followed by the path to the executable that should run the script. It would typically be
#!/bin/bash on most systems. The rest of the script follows.
Let's take a look at a simple script that looks in one directory for files with names matching a glob or filename pattern. It then uses
grep to check for a particular text string in those files, and if it finds it, it moves the file to a new directory.
# We declare two variables for the two directories
# The for loop that moves the right files
for i in 'grep -l matchpattern $firstdir/*'; do
mv $i $seconddir
The first thing we do is declare two variables in the script:
seconddir. These contain the directory names we'll be working with. Then we create a
for loop that uses
grep to find the names of files that contain the string
-l switch tells
grep to output only the name of files that match, rather than also displaying the match pattern itself. Thus, the
$i variable in the for loop contains the file name of a matching file every time the loop runs. If there are 10 matching files, the loop will run 10 times, with the
$i variable containing the file name of the next matching file.
During each run of the loop, the
mv command is used to move the file from
seconddir. The script then echoes the file name back to standard output. This means that if you run the script, you will see a list of files that have matched the pattern and have been moved to
Note that you can use a text editor such as pico, nano, or vim to create these scripts. Once you've created the script (let's name it
script.sh), you will need either to make the script executable or to run the script by calling Bash explicitly. You can make an executable script run simply by typing its name on the command line (assuming it has the right permissions).
You make a script executable using the
chmod +x script.sh
To run a script by calling Bash explicitly, enter: