Everyday Guidebooks
Cover of Shell Scripting on the Linux Command Line

Cybersecurity & IT

Shell Scripting on the Linux Command Line

A Practical Guide to Arguments, Error Handling, File Processing, and Reliable Maintenance Tasks

by Todd Michael

Available now

Ready to make your Linux command-line skills work harder? This practical guide takes you from simple command sequences to scripts that make routine maintenance repeatable. If you are studying linux command line shell scripting for an IT certification or preparing for a systems role, you will learn by building and testing useful examples—not by memorizing syntax in isolation.

Discover how to accept and validate arguments, work safely with files and directories, check command results, and write status messages that help you understand what happened. Along the way, you will see why quoting matters, how shell behavior can differ, and where common portability mistakes begin.

Each chapter combines focused explanations with exercises and worked scripts. A final maintenance project brings the skills together, giving you a foundation you can adapt to your own Linux environment. No certification exam affiliation or pass guarantee is implied.

Contents
  1. 1. From Commands to Scripts
  2. Display a short message and the current directory.
  3. Create a small file in the current working directory.
  4. 2. Variables, Expansion, and Quoting
  5. 3. Arguments and Useful Interfaces
  6. 4. Decisions and Exit Status
  7. 5. Loops and Repeated Work
  8. Repeating Work with `for`
  9. Looping Over Arguments
  10. Using Patterns for File Lists
  11. Repeating Until a Condition Changes
  12. Reading a File One Line at a Time
  13. Build a Maintenance Target Checker
  14. Practice and Review
  15. 6. Files, Paths, and Redirection
  16. A Disposable Workspace
  17. The Three Standard Streams
  18. Keep Results and Errors Separate
  19. Redirection Can Fail Before the Command Runs
  20. Check Paths for Their Intended Use
  21. Avoiding Unintended Overwrites
  22. Reading a File Line by Line
  23. Temporary Files and Cleanup
  24. Worked Example: Build a Report Before Replacing It
  25. 7. Processing Text with Command-Line Tools
  26. Prefix a relative name so awk does not mistake it for an assignment.
  27. 8. Functions and Script Organization
  28. 9. Handling Errors Deliberately
  29. 10. Status Messages and Logs
  30. 11. Portable Shell Practices
  31. Prefix relative paths so a leading hyphen is not treated as an option.
  32. 12. Worked Maintenance Scripts
  33. Prepare a practice workspace
  34. Script one: report filesystem space
  35. Script two: build a file inventory
  36. Preserve the root directory when OUTPUT_FILE is directly under /.
  37. Understand what the inventory represents
  38. Test failure cases deliberately
  39. Adapt with a clear contract
  40. 13. Testing, Troubleshooting, and Scheduling

Read a sample: 1. From Commands to Scripts

A Linux command can answer a question, create a file, or change part of a system. A shell script puts commands in a file so that you can run them again without retyping the sequence. That makes routine work easier to repeat, inspect, and eventually share.

A script is not automatically reliable simply because its commands are saved. It may run from a different directory than you expect, encounter a missing file, or finish with a result you did not check. The habits you build around a script matter as much as the commands inside it.

In this chapter, you will create a disposable practice workspace, write a small script, run it in two ways, and inspect its exit status. You will also establish a workflow you can use throughout the book: edit, run, and inspect.

Open a terminal as your regular user. You do not need administrator privileges for the examples in this chapter. Begin by checking where you are:

sh pwd

The command prints your current working directory. Paths in commands can be absolute, starting from /, or relative to the current directory. For early practice, it helps to use a clearly named directory under your home directory.

Create a workspace and move into it:

sh mkdir -p "$HOME/shell-practice/chapter1" cd "$HOME/shell-practice/chapter1" pwd

The -p option allows mkdir to create missing parent directories and does not report an error merely because the directory already exists. The quotes around the path keep it together if your home directory’s name contains spaces. You will study quoting in detail in Chapter 2; for now, keep the quotes when you type the command.

Look at the directory contents:

sh ls -la

If you have used this location before, it may contain files. Choose another directory name if you want a fresh workspace. The examples below create or overwrite files named hello.sh and sample.txt, so check before using those names. A practice directory is useful because you can recognize its contents and avoid experimenting in directories that hold important work.

Your shell keeps track of the current directory for the terminal session. A script you start from this terminal normally begins with that same working directory, even if the script file is stored somewhere else. The script’s location and its working directory are different ideas. Later chapters will show ways to make paths and working-directory assumptions explicit.

Use a text editor to create a file named hello.sh in the practice directory. For example, if nano is installed:

sh nano hello.sh

Enter these lines:

sh #!/bin/sh

More Cybersecurity & IT

Cover of The White Hat Bible, Volume 1The White Hat Bible, Volume 1Available nowCover of The White Hat Bible, Volume 2The White Hat Bible, Volume 2Available nowCover of The White Hat Bible, Volume 3The White Hat Bible, Volume 3Available nowCover of The White Hat Bible, Volume 4The White Hat Bible, Volume 4Available nowThe White Hat Bible, Volume 5Available nowCover of The White Hat Pocket GuideThe White Hat Pocket GuideAvailable now

About Todd Michael

Todd Michael is an American author and the founder of ToddMichael.tv. He writes practical, plain-English books on money, health, preparedness, technology, games, humor and more.

About the author Todd Michael · Todd Michael’s author site

Part of the ToddMichael.tv network.