Shell Scripting for Beginners: Advanced Commands & Chains

Expanding Your Shell Scripting Toolkit
Having gained proficiency in script creation, argument utilization, and the implementation of for loops, we will now explore additional fundamental commands. This includes techniques for manipulating text files and effectively managing the flow of data between files, commands, and standard input/output streams.
Essential Commands for Text File Manipulation
The shell provides a suite of commands designed for working with text files. These tools are crucial for automating tasks involving data processing and file management.
- cat: Displays the contents of a file.
- echo: Prints text to the standard output.
- grep: Searches for specific patterns within files.
- sed: Performs text transformations and substitutions.
- awk: A powerful text processing language.
Understanding these commands allows for efficient extraction, modification, and analysis of textual data directly from the command line.
Redirecting Input and Output
A key aspect of shell scripting is the ability to control where commands receive their input from and where they send their output. This is achieved through redirection.
Input can be redirected from a file using the < operator, while output can be redirected to a file using the > operator. Appending to a file is done with >>.
For example, command < input.txt executes the command, taking its input from input.txt. Similarly, command > output.txt directs the command’s output to output.txt, overwriting any existing content.
Piping Commands Together
The pipe operator, denoted by |, allows you to chain commands together. The output of one command becomes the input of the next.
This is incredibly useful for building complex workflows. Consider cat file.txt | grep "keyword". This command displays only the lines from file.txt that contain the specified "keyword".
Piping enables a modular approach to scripting, where individual commands perform specific tasks, and the overall process is orchestrated by the shell.
Mastering these concepts – basic commands, text file manipulation, and redirection – will significantly enhance your ability to write effective and versatile shell scripts.
Essential Shell Commands
A foundational understanding of shell scripts was previously provided, along with an illustrative exploration of for loops. Should you have missed those earlier articles, revisiting our shell scripting guide is highly recommended.
The command-line interface offers numerous advantages, and redirection stands out as a particularly significant one. Without redirection, the necessity of manually recording and replicating command outputs for subsequent actions would be incredibly cumbersome. Redirection enables the capture of output for storage or its immediate utilization as input for another command. Files can also be employed as inputs for various commands.
Let's now examine several fundamental commands that prove useful across a wide range of applications.
echo -- This command functions to output, or display, its provided argument directly to the command line.
echo argument with spaces

As illustrated, certain characters require "escaping" to ensure they are interpreted literally. This is achieved by preceding the character with a backslash (\). Utilizing quotes is generally a preferable approach. The echo command also seamlessly integrates with variables.

Observe that single and double quotes exhibit distinct behaviors. Further details can be found in the article: What's the Difference Between Single and Double Quotes in the Bash Shell?
cat -- This command serves to display the contents of text files to the standard output.
cat file_to_be_read
Consider the creation of a text file using nano:

Upon executing the cat command on this file, its contents are revealed.

grep -- This command is a remarkably powerful and versatile tool within the Linux environment. Standing for Global/Regular Expression Print, it searches a file for lines matching a specified pattern. The use of "regular expressions" allows for the definition of complex search criteria. For now, however, a simple search term can be entered.
grep pattern file

While grep possesses greater capabilities, we will focus on its simpler applications for the present time.
Output Redirection Techniques
The redirection of command outputs to files is accomplished utilizing a specific symbol: the greater-than sign (>).
Let’s modify our example. Execute the following command:
echo pepperoni > list

Observe that the 'echo' command no longer displays the output on the screen. Upon inspecting the contents of the "list" file, the echoed text is present.
It’s important to note that any prior content within "list" was overwritten. Let's repeat the process:

This functionality proves valuable when a file needs to be refreshed. However, often the goal is to append data to an existing file. To achieve this, employ two consecutive greater-than symbols:
echo yellow peppers >> list

This is straightforward. Let’s expand our list using this method.

As you can see, the command-line is a useful tool for creating to-do lists and similar items. Its capabilities are further enhanced.
Now, let’s redirect the output of a command directly into a file:
ls --al / > ~/rootlist

The process of compiling file lists, refining them, and subsequently executing commands on selected entries has been greatly simplified. Furthermore, these fundamental operations within the command-line are readily applicable within scripts.
Piping and Command Chaining
The technique known as piping derives its name from the pipe symbol (|), which is typically located alongside the backslash (\) key on most keyboards. This method allows the output generated by one command to be used as the input for another command directly.
By constructing extended sequences of commands, a highly tailored result can be achieved. This is particularly useful when working with commands such as grep, offering a streamlined workflow.

Piping functions similarly to redirection using ">", but it offers greater flexibility. Multiple pipes can be linked together, and it doesn't necessarily require interaction with a text file.
It's important to note that grep is, by default, case-sensitive in its searches. The "-i" flag can be employed to instruct grep to disregard case distinctions during the search process.

Utilizing Input Redirection
It is also possible to direct inputs to commands from files by employing the less-than symbol (<).
cat < list

One might observe that this appears identical to providing an argument directly. However, the true benefit of input redirection becomes apparent when commands are linked together in a sequence.
Consider a scenario where we aim to extract all words containing "pep" from an existing file named "list" and save them into a new file called "revisions".
grep pep < list > revisions

Now, let's modify this command to include sorting of the results.
grep pep < list | sort > revisions

This process will search for the term "pep" within the "list" file, arrange the findings alphabetically – with uppercase letters preceding lowercase ones – and then store the sorted output in the "revisions" file.
To further clarify the functionality of the sort command, let's examine the following illustration:

As demonstrated, incorporating the "-f" flag with the sort command enables case-insensitive sorting. This feature simplifies the alphabetical arrangement of text file lines, disregarding capitalization when it is not a significant factor.
A Basic Scripting Example
Let us develop a script adhering to the following format:
script searchterm listfile
This script will accept a term and employ the grep command to search within a specified list file. The resulting matches will then be sorted, and the organized output will be directed to a new file.

The following directory will serve as our testing environment for this script:

A list of the contents within this directory can be generated, and subsequently used as input for the script.

This is the result! A deeper understanding of regular expressions will allow for more precise search command construction. Furthermore, any valid expression enclosed in quotes can be utilized as the initial argument.
Regarding sorting capabilities, options extend beyond simple alphabetical arrangement. Consult the manual pages for details on these commands:
- tsort – Provides a more sophisticated topological sorting functionality.
- tr – Enables character mapping and transcription between different sets.
- uniq – Eliminates duplicate entries, retaining only unique lines.
- awk – A powerful text processing language for field separation and manipulation.
- cut, paste/join – Tools for isolating fields from text files and appending data to columns.
- look – Performs searches similar to grep, utilizing a user-defined dictionary file.
- wc – Calculates word, line, and character counts, among other metrics.
Today, we explored several fundamental commands that are equally valuable when used directly on the command line or integrated into scripts. Given that text-based data frequently forms the core of our daily operations, the ability to effectively work with, search, and manipulate it is crucial.
What scripting solutions do you find particularly useful? Do you have any specialized scripts designed for handling text-based files? Please share your knowledge and insights in the comments section!