Learning Linux Shell Scripting - Second Edition

Book description

Break through the practice of writing tedious code with shell scripts

About This Book
  • Learn to impeccably build shell scripts and develop advanced applications
  • Create smart solutions by writing and debugging scripts
  • A step-by-step tutorial to automate routine tasks by developing scripts
Who This Book Is For

Learning Linux Shell Scripting is ideal for those who are proficient at working with Linux and want to learn about shell scripting to improve their efficiency and practical skills.

What You Will Learn
  • Familiarize yourself with the various text filtering tools available in Linux
  • 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, dialogs to develop screens & Database administration such as MySQL or Oracle
  • Start up a system and customize a Linux system
  • Taking backup of local or remote data or important files.
  • Use existing other language scripts such as Python, Perl & Ruby in Shell Scripts
In Detail

Linux is the most powerful and universally adopted OS. 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 reads this file and acts on commands as if they were typed on the keyboard.

Learning Linux Shell Scripting covers Bash, GNU Bourne Again Shell, preparing you to work in the exciting world of Linux shell scripting. CentOS is a popular rpm-based stable and secured Linux distribution. Therefore, we have used CentOS distribution instead of Ubuntu distribution. Linux Shell Scripting is independent of Linux distributions, but we have covered both types of distros. We start with an introduction to the Shell environment and basic commands used. Next, we explore process management in Linux OS, real-world essentials such as debugging and perform Shell arithmetic fluently. You'll then take a step ahead and learn new and advanced topics in Shell scripting, such as decision making, starting up a system, and customizing a Linux environment. You will also learn about grep, stream editor, and AWK, which are very powerful text filters and editors. Finally, you'll get to grips with taking backup, using other language scripts in Shell Scripts as well as automating database administration tasks for MySQL and Oracle.

By the end of this book, you will be able to confidently use your own shell scripts in the real world.

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.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Learning Linux Shell Scripting Second Edition
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  7. Getting Started and Working with Shell Scripting
    1. Comparison of shells
    2. Tasks done by the shell
    3. Working in the shell
    4. Learning basic Linux commands
    5. Our first script – Hello World
    6. Compiler and interpreter – differences 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. The chmod command
        1. Technique one – the symbolic method
        2. Technique two – the numeric method
      3. Setting umask
      4. Setuid
      5. Setgid
      6. Sticky bit
    11. Summary
  8. 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
  9. 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
      9. The sort 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
  10. 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
  11. 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
  12. 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
        1. The here operator with the wc command
        2. The utility ed and here operator
        3. A script for sending messages to all logged-in users
        4. Using the << here operator for FTP usage and data transfer
      2. Turning off variable substitution
    3. The here string and the <<< operator
    4. File handling
      1. Introducing file handling
        1. Using exec to assign a 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 the read command on a 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 the set command
        2. The vi editor setting for debugging
      5. Good practices for Shell scripts
    6. Summary
  13. Performing Arithmetic Operations in Shell Scripts
    1. Using a declare command 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. Floating-point arithmetic
    6. Summary
  14. Automating Decision-Making in Scripts
    1. Checking the exit status of commands
    2. Understanding the test command
      1. Using the test command
      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 exit command 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. Summary
  15. Automating Repetitive Tasks
    1. Looping with the for command
    2. Exiting from the current loop iteration with the continue command
    3. Exiting from a loop with a break
    4. Working with the do – while loop
    5. Using until
    6. Piping the output of a loop to a Linux command
    7. Running loops in the background
    8. The IFS and loops
    9. Summary
  16. Working with Functions
    1. Understanding functions
      1. Displaying functions
      2. Removing functions
    2. Passing arguments or parameters to functions
    3. Sharing the data with 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
  17. 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 inside a 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 checklist box (checklist)
      8. The menu box (menu)
      9. The radiolist box (radiolist)
      10. The progress meter box (gauge)
    7. Summary
  18. 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. System-wide setting scripts
      2. User level settings – default files
    3. Summary
  19. Pattern Matching and Regular Expressions with sed and awk
    1. The basics of regular expressions
    2. sed – non-interactive 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
  20. Taking Backup and Embedding Other Languages in Shell Scripts
    1. Backup of files from command line
      1. Backup command rsync
      2. Backup across the network
      3. Automating backup activity
    2. Embedding other language codes or scripts in Bash shell scripts
      1. Embedding other language code in Bash shell script
      2. Sending output to Bash Script
      3. Storing other language output to Bash variable
      4. Sending data to an embedded language code
      5. Using data from file by embedded language
      6. Sending user input to the embedded code
      7. Embedding Python code in Bash shell Script
      8. Embedding Ruby code
      9. Embedding other language code in Bash – comparative study
      10. A summary of commands for embedding other programming languages
    3. Summary
  21. Database Administration Using Shell Scripts
    1. Introduction to database administration
    2. Working with a MySQL Database
      1. Checking the version of MySQL database
      2. Creating a database
      3. Show databases
      4. Creating a user
      5. Creating a table in MySQL
      6. Inserting data into table
      7. Retrieving data from the table
      8. Updating data
      9. Deleting data
      10. Altering a table
      11. Describing a table
      12. Drop the table
      13. Drop the database
    3. Working with Oracle Database
      1. Switching to an Oracle user
      2. Creating a user in Oracle SQL command line
      3. The Grant statement
      4. The Define command
      5. Predefined variables
      6. Create user through a shell script
      7. Creating a table
      8. Inserting the data into table
      9. Retrieving data from a table
      10. Update the data
      11. Delete the data
      12. Drop the table
    4. Summary
  22. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Learning Linux Shell Scripting - Second Edition
  • Author(s): Ganesh Naik
  • Release date: May 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788993197