Shell scripting is a powerful tool that allows users to automate tasks and execute commands in a Unix or Linux environment. It provides a way to create scripts that can be executed in the shell, which is the command-line interface of an operating system.
What is Shell Scripting?
Shell scripting is the process of writing a series of commands in a script file to be executed by the shell. The shell reads these commands and executes them one by one, just as if they were entered directly on the command line. This makes it convenient for automating repetitive tasks or performing complex operations.
Example:
Let’s illustrate shell scripting with a simple example. Suppose you have a directory with multiple text files, and you want to count the number of lines in each file and display it on the screen.
To do this, you can create a shell script using your favorite text editor, such as vim or nano. Save the script with a .sh extension, like “line_count.sh”.
Creating the Shell Script
First, open your text editor and create a new file:
$ nano line_count.sh
Once the file is open, start by specifying the shell you want to use at the beginning of your script. In this case, we’ll use bash:
#!/bin/bash
This tells the system that this script should be run using the bash shell.
Next, let’s add some code to count lines in each file within a directory:
#!/bin/bash
# Set directory path
directory="/path/to/directory"
# Loop through each file in the directory
for file in "$directory"/*
do
# Check if it's a regular file
if [[ -f "$file" ]]; then
# Count lines and display output
lines=$(wc -l "$file" | awk '{print $1}')
echo "File: $file, Lines: $lines"
fi
done
Let’s break down the code:
- We set the directory path to the desired directory where our files are located.
- We use a for loop to iterate through each file in the directory.
- Inside the loop, we check if the item is a regular file using the -f flag.
- If it is, we use the wc command to count lines in that file and store it in a variable called lines. The awk command is used to extract only the line count from wc’s output.
- Finally, we echo the filename and line count on the screen.
Executing the Shell Script
Once you have saved your script, you need to make it executable. Open your terminal and navigate to the directory where your script is located. Then run:
$ chmod +x line_count.sh
This command grants execution permission to your script.
To execute your shell script, simply type:
$ ./line_count.sh
The script will start running and display output similar to this:
File: /path/to/directory/file1.txt, Lines: 10
File: /path/to/directory/file2.txt, Lines: 15
File: /path/to/directory/file3.txt, Lines: 8
Conclusion:
Shell scripting is a powerful tool that allows users to automate tasks and execute commands. With just a few lines of code, you can perform complex operations or automate repetitive tasks.
This example demonstrates how shell scripting can be used to count lines in multiple files within a directory. Remember to explore further and experiment with different shell commands to enhance your scripts.