O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning Linux Shell Scripting

Book Description

Unleash the power of shell scripts to solve real-world problems by breaking through the practice of writing tedious code

About This Book

  • Learn how to efficiently and effectively build shell scripts and develop advanced applications with this handy book
  • Develop high quality and efficient solutions by writing professional and real-world scripts, and debug scripts by checking and shell tracing
  • A step-by-step tutorial to automate routine tasks by developing scripts from a basic level to very advanced functionality

Who This Book Is For

This book is ideal for those who are proficient at working with Linux and who want to learn about shell scripting to improve their efficiency and practical skills. By the end of this book, you will be able to confidently use your own shell scripts in the real world.

What You Will Learn

  • Familiarize yourself with the various text filtering tools available in Linux
  • Combine the fundamental text and file processing commands to process data and automate repetitive tasks
  • Understand expressions and variables and how to use them practically
  • Automate decision-making and save a lot of time and effort of revisiting code
  • Get to grips with advanced functionality such as using traps and signals and using dialogs to develop screens
  • Start up a system and customize a Linux system
  • Take an in-depth look at regular expressions and pattern matching to understand the capabilities of scripting

In Detail

Linux is the one of the most powerful and universally adopted OSes. Shell is a program that gives the user direct interaction with the operating system. Scripts are collections of commands that are stored in a file. The shell can read this file and act on the commands as if they were typed on the keyboard. Shell scripting is used to automate day-to-day administration, and for testing or product development tasks.

This book covers Bash, GNU Bourne Again SHell, preparing you to work in the exciting world of Linux shell scripting. We start with an introduction to the Shell environment and explain basic commands used in Shell. Next we move on to check, kill, and control the execution of processes in Linux OS. Further, we teach you about the filter tools available in Linux and explain standard output and standard errors devices.

Then we will ensure you understand Shell’s interpretation of commands and get a firmer grasp so you use them in practice. Next, you’ll experience some real-world essentials such as debugging and perform Shell arithmetic fluently. Then you’ll take a step ahead and learn new and advanced topics in Shell scripting, such as starting up a system and customizing a Linux system. Finally, you’ll get to understand the capabilities of scripting and learn about Grep, Stream Editor, and Awk.

Style and approach

This practical book will go from the very basics of shell scripting to complex, customized automation. The idea behind this book is to be as practical as possible and give you the look and feel of what real-world scripting is like.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Learning Linux Shell Scripting
    1. Table of Contents
    2. Learning Linux Shell Scripting
    3. Credits
    4. About the Author
    5. Acknowledgments
    6. About the Reviewers
    7. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    8. 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 color images of this book
        2. Errata
        3. Piracy
        4. Questions
    9. 1. Getting Started and Working with Shell Scripting
      1. Comparison of shells
      2. Tasks done by shell
      3. Working in shell
      4. Learning basic Linux commands
      5. Our first script – Hello World
      6. Compiler and interpreter – difference in process
      7. When not to use scripts
      8. Various directories
      9. Working more effectively with shell – basic commands
      10. Working with permissions
        1. Changing file permissions
        2. Command chmod
          1. Technique one – the symbolic method
          2. Technique two – the numeric method
        3. Setting umask
        4. Setuid
        5. Setgid
        6. Sticky bit
      11. Summary
    10. 2. Drilling Deep into Process Management, Job Control, and Automation
      1. Introducing process basics
      2. Monitoring processes using ps
      3. Process management
      4. Process monitoring tools – top, iostat, and vmstat
      5. Understanding "at"
      6. Understanding "crontab"
      7. Summary
    11. 3. Using Text Processing and Filters in Your Scripts
      1. Text filtering tools
        1. Head and tail
        2. The diff command
        3. The cut command
        4. The paste command
        5. The join command
        6. The uniq command
        7. The comm command
        8. The tr command
      2. IO redirection
        1. File descriptors
        2. Redirection
        3. Brace expansion
      3. Pattern matching with the vi editor
      4. Pattern searching using grep
      5. Summary
    12. 4. Working with Commands
      1. Learning shell interpretation of commands
        1. Checking and disabling shell internal commands
        2. The exit status
      2. Command substitution
      3. Command separators
        1. Command1; command2
        2. Command grouping
      4. Logical operators
        1. Command1 & command2
        2. Command1 && command2
        3. Command1 || command2
      5. Pipes
      6. Summary
    13. 5. Exploring Expressions and Variables
      1. Understanding variables
      2. Working with environment variables
        1. The local variable and its scope
        2. Exporting variables
      3. Working with read-only variables
      4. Working with command line arguments (special variables, set and shift, getopt)
        1. Understanding set
        2. Understanding shift
          1. Resetting positional parameters
      5. Understanding getopts
      6. Understanding default parameters
      7. Working with arrays
        1. Creating an array and initializing it
        2. Accessing array values
      8. Summary
    14. 6. Neat Tricks with Shell Scripting
      1. Interactive Shell scripts – reading user input
        1. Summarizing the read command with options
      2. The here document and the << operator
        1. The here operator with the sort command
        2. The here operator with the wc command
        3. The utility ed and here operator
        4. A script for sending messages to all logged-in users
        5. Using the << here operator for FTP usage and data transfer
        6. Turning off variable substitution
      3. The here string and the <<< operator
      4. File handling
        1. Introducing file handling
          1. Using exec to assign file descriptor (fd) to file
        2. Understanding the opening, writing, and closing of a file
        3. Understanding reading from a file
        4. Understanding reading and writing to a file
        5. Using command read on file descriptor (fd)
        6. Reading from one file and writing to another file
          1. Displaying the file descriptor information from the /proc folder
          2. File handling – reading line by line
          3. Executing the command and storing the results in a file
        7. Summarizing usage of the exec command
      5. Debugging
        1. Debugging mode – disabling the shell (option -n)
        2. Debugging mode – displaying commands (option -v)
        3. Debugging mode – the tracing execution (option -x)
          1. Summarizing the debugging options for the Bash shell
        4. Using the set command
          1. Summary of debugging options for set command
          2. The vi editor setting for debugging
        5. Good practices for Shell scripts
      6. Summary
    15. 7. Performing Arithmetic Operations in Shell Scripts
      1. Using a command declare for arithmetic
        1. Listing integers
      2. Using the let command for arithmetic
      3. Using the expr command for arithmetic
        1. Using an arithmetic expansion
      4. Binary, octal, and hex arithmetic operations
      5. A floating-point arithmetic
      6. Summary
    16. 8. Automating Decision Making in Scripts
      1. Checking the exit status of commands
      2. Understanding the test command
        1. Using the test command with single brackets
        2. Using the test command with double brackets
        3. String comparison options for the test command
        4. Numerical comparison operators for the test command
        5. File test options for the test command
        6. File testing binary operators
        7. Logical test operators
      3. Conditional constructs – if else
        1. Numerical handling if constructs
        2. Using the command exit and the ? variable
        3. String handling with the if construct
        4. Checking for null values
        5. File handling with the if command
        6. Multiple test commands and if constructs
        7. The if/elif/else command
        8. The null command
      4. Switching case
      5. Implementing simple menus with select
      6. Looping with the for command
      7. Exiting from the current loop iteration with the continue command
      8. Exiting from a loop with a break
      9. Working with the do while loop
      10. Using until
      11. Piping the output of a loop to a Linux command
      12. Running loops in the background
      13. The IFS and loops
      14. Summary
    17. 9. Working with Functions
      1. Understanding functions
        1. Displaying functions
        2. Removing functions
      2. Passing arguments or parameters to functions
      3. Sharing the data by many functions
      4. Declaring local variables in functions
      5. Returning information from functions
        1. Returning a word or string from a function
      6. Running functions in the background
        1. Command source and period (.)
      7. Creating a library of functions
      8. Summary
    18. 10. Using Advanced Functionality in Scripts
      1. Understanding signals and traps
      2. Using the trap command
      3. Ignoring signals
        1. Resetting signals
        2. Listing traps
      4. Using traps in function
      5. Running scripts or processes even if the user logs out
      6. Creating dialog boxes with the dialog utility
        1. Creating a message box (msgbox)
        2. Creating a message box (msgbox) with a title
        3. The yes/no box (yesno)
        4. The input box (inputbox)
        5. The textbox (textbox)
        6. A password box
        7. The menu box (menu)
        8. The checklist box (checklist)
        9. The radiolist box (radiolist)
        10. The progress meter box (gauge)
      7. Summary
    19. 11. System Startup and Customizing a Linux System
      1. System startup, inittab, and run levels
        1. The kernel startup and init process
        2. Understanding run levels
        3. System initialization boot scripts
      2. User initialization scripts
        1. Systemwide settings scripts
        2. User level settings – default files
      3. Summary
    20. 12. Pattern Matching and Regular Expressions with sed and awk
      1. The basics of regular expressions
      2. sed – noninteractive stream editor
        1. Understanding sed
        2. Understanding regular expression usage in sed
        3. Addressing in sed
        4. How to modify a file with sed
        5. Printing – the p command
        6. Deleting – the d command
        7. Substitution – the s command
        8. Range of selected lines: the comma
        9. Multiple edits – the e command
        10. Reading from files – the r command
        11. Writing to files – the w command
        12. Appending – the a command
        13. Inserting – the i command
        14. Changing – the c command
        15. Transform – the y command
        16. Quit – the q command
        17. Holding and getting – the h and g commands
        18. Holding and exchanging – the h and x commands
        19. sed scripting
      3. Using awk
        1. The meaning of awk
        2. Using awk
          1. Input from files
        3. Input from commands
        4. How awk works
        5. awk commands from within a file
        6. Records and fields
          1. Records
            1. The record separator
            2. The $0 variable
            3. The NR variable
          2. Fields
          3. Field separators
            1. The input field separator
        7. Patterns and actions
          1. Patterns
          2. Actions
        8. Regular expressions
        9. Writing the awk script file
        10. Using variables in awk
        11. Decision making using an if statement
        12. Using the for loop
        13. Using the while loop
        14. Using the do while loop
      4. Summary
    21. Index