Linux Shell Scripting Essentials

Book Description

Learn shell scripting to solve complex shell-related problems and to efficiently automate your day-to-day tasks

About This Book

  • Familiarize yourself with the terminal by learning about powerful shell features
  • Automate tasks by writing shell scripts for repetitive work
  • Packed with easy-to-follow, hands-on examples to help you write any type of shell script with confidence

Who This Book Is For

This book is aimed at administrators and those who have a basic knowledge of shell scripting and who want to learn how to get the most out of writing shell scripts.

What You Will Learn

  • Write effective shell scripts easily
  • Perform search operations and manipulate large text data with a single shell command
  • Modularize reusable shell scripts by creating shell libraries
  • Redirect input, output, and errors of a command or script execution to other streams
  • Debug code with different shell debugging techniques to make your scripts bug-free
  • Manage processes, along with the environment variables needed to execute them properly
  • Execute and embed other languages in your scripts
  • Manage creation, deletion, and search operations in files

In Detail

Shell scripting is a quick method to prototype complex applications or problems. Shell scripts are a collection of commands to automate tasks, usually those for which the user has a repeated need, when working on Linux-based systems. Using simple commands or a combination of them in a shell can solve complex problems easily.

This book starts with the basics, including essential commands that can be executed on Linux systems to perform tasks within a few nanoseconds. You'll learn to use outputs from commands and transform them to show the data you require. Discover how to write shell scripts easily, execute script files, debug, and handle errors.

Next, you'll explore environment variables in shell programming and learn how to customize them and add a new environment. Finally, the book walks you through processes and how these interact with your shell scripts, along with how to use scripts to automate tasks and how to embed other languages and execute them.

Style and approach

This book is a pragmatic guide to writing efficient shell programs, complete with hands-on examples and tips.

Table of Contents

  1. Linux Shell Scripting Essentials
    1. Table of Contents
    2. Linux Shell Scripting Essentials
    3. Credits
    4. About the Author
    5. About the Reviewers
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. The Beginning of the Scripting Journey
      1. Hello World in shell
        1. Interacting with shell
        2. Let's make it scripted
      2. Define variables of choice
        1. Nomenclature
        2. Assigning a value
        3. Accessing a value
        4. Constant variables
        5. Reading variables from a user input
      3. Builtin shell variables
      4. Operators
        1. The assignment operator
        2. Arithmetic operators
        3. Logical operators
        4. Comparison operators
      5. Shell expansions
        1. ~ (Tilde)
        2. * (Asterisk)
        3. ? (Question mark)
        4. [ ] (Square brackets)
        5. { } (Curly brackets)
      6. Construct commands using eval
      7. Make bash behave using set
        1. Exit on the first failure
        2. Enabling/disabling symbolic link's resolution path
        3. Setting/unsetting variables
      8. Summary
    9. 2. Getting Hands-on with I/O, Redirection Pipes, and Filters
      1. Standard I/O and error streams
        1. File descriptors
      2. Redirecting the standard I/O and error streams
        1. Redirecting standard output
        2. Redirecting standard input
        3. Redirecting standard errors
        4. Multiple redirection
      3. Pipe and pipelines – connecting commands
        1. Pipe
        2. Pipeline
      4. Regular expressions
        1. Regular expression metacharacters
        2. Character ranges and classes
          1. Character ranges
          2. Matching dates in mm-dd-yyyy format
            1. Matching a valid month
            2. Matching a valid day
            3. Matching the valid year in a date
            4. Combining valid months, days, and years regex to form valid dates
          3. Regex for a valid shell variable
      5. Filtering an output using grep
        1. Syntax
        2. Looking for a pattern in a file
        3. Looking for a pattern in multiple files
        4. A few more grep usages
          1. Searching in a binary file
          2. Searching in a directory
          3. Excluding files/directories from a search
          4. Display a filename with a matching pattern
          5. Matching an exact word
      6. Editing output using sed
        1. String substitution using s
        2. Multiple substitutions
      7. Duplicating a stream using tee
        1. Writing an output to stdout and appending to a file
        2. Sending an output to multiple commands
      8. Sorting and finding unique text
        1. Sorting an input text
          1. Sorting a single file
          2. Redirecting output to sort
        2. Filtering unique elements
        3. Unique elements in a file
      9. Character-based translation using tr
        1. Deleting input characters
        2. Squeezing to a single occurrence
        3. Inverting a character set to be translated
      10. Filtering based on lines—head and tail
        1. Printing lines using head
        2. Printing the first few lines
          1. Printing the first few bytes
        3. Printing lines using tail
          1. Checking log entries
        4. Finding any line in a file
      11. The Cut-based selection
        1. Cutting across columns
        2. Text selection in files
      12. Summary
    10. 3. Effective Script Writing
      1. Exiting from scripts and exit codes
        1. Exit codes
        2. Exit codes with a special meaning
        3. Script with exit codes
      2. Testing expressions with a test
        1. File checks
        2. Arithmetic checks
        3. String checks
        4. Expression checks
      3. Using conditional statements with if and else
        1. Simple if and else
        2. The if, elif, and else statements
        3. Nested if
      4. Indexed arrays and associative arrays
        1. Indexed arrays
          1. Array declaration and value assignment
          2. Operations on arrays
        2. The associative array
          1. The declaration and value assignment
          2. Operations on arrays
      5. Looping around with for
        1. Simple iteration
        2. Iterating over a command output
        3. Specifying a range to the for loop
        4. Small and sweet for loop
      6. The select, while, and until loops
        1. Loop using select
        2. The while loop
        3. The until loop
      7. Switch to my choice
      8. Passing stdout as a parameter using xargs
        1. Basic operations with xargs
        2. Using xargs to find a file with the maximum size
        3. Archiving files with a given pattern
      9. Using functions and positional parameters
        1. Calling a function in bash
        2. Passing parameters to functions
      10. Alias
        1. Creating alias
        2. Listing all aliases
        3. Removing an alias
      11. pushd and popd
      12. Summary
    11. 4. Modularizing and Debugging
      1. Modularizing your scripts
        1. Source to a script file
          1. Syntax
          2. Creating a shell script library
          3. Loading a shell script library
            1. Calling a shell library in bash
            2. Calling shell library in another shell script
      2. Passing command line parameters to script
        1. Reading arguments in scripts
        2. Shifting command line arguments
        3. Processing command line options in a script
      3. Debugging your scripts
        1. Debugging using echo
        2. Debugging an entire script using -x
        3. Debugging sections of a script using the set options
      4. Command completion
        1. Managing bash completion with complete
          1. Viewing the existing bash completion
          2. Modifying default bash completion behavior
          3. Removing bash completion specification
        2. Writing bash completion for your own application
          1. An example of bash completion
          2. Running the created bash completion
      5. Summary
    12. 5. Customizing the Environment
      1. Knowing the default environment
        1. Viewing a shell environment
          1. printenv
          2. env
        2. Differences between shell and environment variables
      2. Modifying a shell environment
        1. Creating environment variables
        2. Modifying environment variables
        3. Deleting environment variables
      3. Using bash startup files
        1. .bashrc
        2. .bash_profile
        3. .bash_logout
      4. Knowing your history
        1. Shell variables controlling the history
        2. The history builtin command
        3. Modifying the default history behavior
        4. Handy shortcuts for seeing the history
          1. [Ctrl + r]
          2. Up and down arrow key
          3. !!
          4. !(search_string)
          5. !?(search_string)
      5. Task management
        1. Running tasks in the background
        2. Sending a running task to the background
        3. Listing background tasks
        4. Moving tasks to the foreground
        5. Terminating tasks
      6. Summary
    13. 6. Working with Files
      1. Performing basic file operations
        1. Creating files
          1. Directory file
          2. Regular file
            1. Touch command
            2. Using the command line editors
            3. Using the cat command
            4. Redirecting the command's output
        2. Modifying files
        3. Viewing files
          1. Viewing content using cat
          2. more and less
        4. Deleting files
          1. Deleting a regular file
          2. Deleting a directory
      2. Moving and copying files
        1. Moving files
          1. Moving a directory to a new location
          2. Renaming a file
        2. Copying files
          1. Copying files locally
            1. Copying a file to another location
          2. Copying files remotely
            1. Copying files to a remote server
      3. Comparing files
        1. Files comparison using diff
          1. Example
      4. Finding files
        1. Searching files according to use case
        2. Finding and deleting a file based on inode number
      5. Links to a file
        1. Soft link
        2. Hard link
        3. Difference between hard link and soft link
      6. Special files
        1. The block device file
        2. Named pipe file
        3. Socket file
      7. Temporary files
        1. Creating a temporary file using mktemp
      8. Permission and ownership
        1. Viewing the ownership and permission of files
        2. Changing permission
        3. Changing the owner and group
          1. Changing a file's owner
          2. Changing group ownership
      9. Getting the list of open files
        1. Knowing the files opened by a specific application
        2. Listing the applications that opened a file
        3. Knowing the files opened by a user
      10. Configuration files
        1. Viewing and modifying configuration files
      11. Summary
    14. 7. Welcome to the Processes
      1. Process management
        1. Process creation and execution
        2. Process termination
          1. Using the kill command
          2. Using the killall command
        3. Using the pkill command
      2. Listing and monitoring processes
        1. Listing processes
          1. Syntax
            1. Simple process selection
            2. Process selection by list
            3. Output format control
          2. Listing all processes with details
          3. Listing all processes run by a user
          4. Processes running in the current terminal
          5. Listing processes by a command name
        2. Tree format display of processes
        3. Monitoring processes
      3. Process substitution
        1. Diffing the output of two processes
      4. Process scheduling priorities
        1. Changing scheduling priorities
          1. Using nice
          2. Using renice
      5. Signals
        1. Available signals
      6. Traps
      7. Inter-process communication
        1. Information on IPC using ipcs
          1. Listing information provided by IPCs
          2. Knowing processes' PID who recently did IPCs
      8. Summary
    15. 8. Scheduling Tasks and Embedding Languages in Scripts
      1. Running tasks at a specific time
        1. Executing scripts using at
          1. Scheduling commands
          2. Scheduling a script file
        2. Listing scheduled tasks
        3. Removing scheduled tasks
      2. Cron jobs
        1. Cron daemon
        2. Cron configuration
        3. Crontab entries
          1. Special strings in Crontab
      3. Managing the crontab entry
        1. Listing crontab entries
        2. Editing crontab entries
        3. Removing crontab entries
      4. systemd
        1. systemd units
        2. Managing services
          1. Status of a service
          2. Enabling and disabling services
          3. Start and stop a service
        3. Viewing system logs
          1. Viewing the latest log entries
          2. Viewing logs of a particular time interval
      5. Embedding languages
        1. Embedding Python language
        2. Embedding AWK language
      6. Summary
    16. Index

Product Information

  • Title: Linux Shell Scripting Essentials
  • Author(s): Sinny Kumari
  • Release date: November 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781785284441