KSS Unit -2.pdf
Document Details
Uploaded by Deleted User
Tags
Full Transcript
Kali Linux and shell scripting Ms. Pranjali Bhoyar Cyber security trainer CHAPTER-2 Advanced Shell Scripting Techniques: Working with files and directories in shell scripts: Working with files and directories in shell scripts involves using command-line instructions...
Kali Linux and shell scripting Ms. Pranjali Bhoyar Cyber security trainer CHAPTER-2 Advanced Shell Scripting Techniques: Working with files and directories in shell scripts: Working with files and directories in shell scripts involves using command-line instructions to perform tasks such as creating, reading, writing, moving, copying, and deleting files and directories. These operations are executed using scripts, which are text files containing a series of commands that the shell interprets and executes. 1. Creating Directories Command: ‘mkdir’ Purpose: Creates a new directory. Syntax: ‘mkdir directory_name’ Example: This command creates a directory named "my_directory". 2. Navigating Directories Command: cd Purpose: Changes the current working directory. Syntax: cd directory_name Example: This command changes the current directory to "my_directory" 3. Checking If a Directory Exists Test Expression: [ -d "directory_name" ] Purpose: Checks if a directory exists. Syntax: [ -d "directory_name" ] Example: This script checks if "my_directory" exists and prints a message accordingly. 4. Listing Files and Directories Command: ls Purpose: Lists files and directories in the current directory. Syntax: ls (simple listing), ls -l (detailed listing) Examples: Lists all files and directories. Lists files and directories in long format, showing permissions, number of links, owner, group, size, and modification date. 5. Creating and Writing to Files Command: echo with Redirection > and >> Purpose: Creates a file and writes text to it. Syntax: Overwrite: echo "text" > file_name Append: echo "text" >> file_name Examples: Creates ‘hello.txt’ and writes “Hello, World!” to it. Appends “Additional text.” to hello.txt 6. Reading from Files Command: cat Purpose: Displays the content of a file. Syntax: cat file_name Example: Displays the content of ‘hello.txt’ 6. Reading from Files Reading Line by Line: Syntax: Example: Reads hello.txt line by line and prints each line. 7. Deleting Files and Directories Command: rm Purpose: Deletes files. Syntax: rm file_name Example: Deleting Directories: Syntax: rm -r directory_name Example: Deletes my_directory and all its contents. 8. Copying and Moving Files Command: cp Purpose: Copies files. Syntax: cp source_file destination_file Example: Copies hello.txt to hello_copy.txt. 8. Copying and Moving Files Command: mv Purpose: Moves or renames files. Syntax: mv old_name new_name Example: Renames hello.txt to hello_world.txt. 9. Changing File Permissions Command: chmod Purpose: Changes file permissions. Syntax: chmod permissions file_name Example: Sets the permissions of hello.txt to 644 (read/write by owner, read by group and others). 10. Changing File Ownership Command: chown Purpose: Changes the ownership of a file. Syntax: ‘chown user:group file_name’ Example: Changes the owner of ‘hello.txt’ to ‘username’ and group to ‘groupname’. 11. Finding Files Command: find Purpose: Searches for files and directories. Syntax: By name: find. -name "pattern" By modification time: find. -type f -mtime -n Examples: Finds all.txt files in the current directory and subdirectories. Finds files modified in the last 7 days. Input/Output Redirection Redirection is the process of sending the output of a command to a location other than the default (usually the screen) or taking input for a command from somewhere other than the default (usually the keyboard). 1. Output Redirection Overwrite (>): Syntax: command > file Purpose: Redirects the output of the command to a file, overwriting the file if it already exists. Input/Output Redirection Example: This command writes "Hello, World!" to hello.txt. If hello.txt exists, its content is overwritten. Input/Output Redirection Append (>>) Syntax: command >> file Purpose: Redirects the output of the command to a file, appending to the file if it already exists. Example: This command appends "Hello again!" to hello.txt. Input/Output Redirection 2. Input Redirection Syntax: command < file Purpose: Redirects the input of the command from a file instead of the keyboard. Example: This command reads each line from hello.txt and prints it. Input/Output Redirection 3. Error Redirection Standard Error (2>): Syntax: command 2> file Purpose: Redirects the error messages of the command to a file. Example: This command tries to list a non-existent file and redirects the error message to error.log. Input/Output Redirection Combining Standard Output and Error (>&): Syntax: command > file 2>&1 or command &> file Purpose: Redirects both the standard output and error messages to the same file. Example: This command redirects both the output and error messages to all_output.log. Piping Piping allows you to send the output of one command as input to another command using the pipe symbol (|). Basic Pipe Syntax: command1 | command2 Purpose: Takes the output of command1 and uses it as the input for command2. Example: This command lists files in long format and then filters the output to show only lines containing ".txt". Combining Pipes with Redirection You can combine pipes with redirection to create more complex command sequences. Example: This command reads file.txt, filters lines containing "pattern", and writes the result to result.txt. Practical Examples Example 1: Redirect Output to a File This command writes "Hello, World!" to hello.txt, creating the file if it doesn't exist or overwriting it if it does. Example 2: Append Output to a File This command appends "Hello again!" to hello.txt. Example 3: Read Input from a File This command reads each line from hello.txt and prints it to the screen. Practical Examples Example 4: Redirect Error Messages This command attempts to list a non-existent file and redirects the error message to error.log. Example 5: Pipe Commands This command lists files in long format and then filters the output to show only lines containing ".txt". Example 6: Combined Output and Error Redirection This command redirects both the output and error messages to all_output.log. Advanced Text Processing Using grep, sed, and awk Text processing is a fundamental aspect of shell scripting, enabling the manipulation and analysis of text data efficiently. Three powerful tools commonly used for advanced text processing are grep, sed, and awk. Each of these tools serves a unique purpose and can be combined to perform complex text processing tasks Advanced Text Processing Using grep grep is a command-line utility for searching plain-text data for lines that match a regular expression. It is commonly used to filter and find specific patterns within files or input. The basic syntax of grep is grep [options] pattern [file]. For example, the command grep "error" logfile.txt searches for lines containing the word "error" in the file logfile.txt. Options like -i (ignore case), -v (invert match to show non-matching lines), and -r (recursive search in directories) enhance its functionality, making it versatile for various text searching needs. Advanced Text Processing Using grep Purpose: Searches for specific words or patterns in files. Syntax: grep [options] pattern [file] Basic Usage: Finds all lines in file.txt that contain "pattern". Options: -i: Ignore case (searches without worrying about uppercase or lowercase). -v: Invert match (shows lines that don't match the pattern). -r: Recursive search (searches through all files in directories). Advanced Text Processing Using sed sed, short for stream editor, is used for modifying text in a stream or file. It can perform basic text transformations on an input stream (a file or input from a pipeline). The syntax for sed is sed [options] 'command' [file]. A common use of sed is the substitution command (s), which replaces occurrences of a pattern with a replacement string. For instance, sed 's/old/new/' file.txt replaces the first occurrence of "old" with "new" in each line of file.txt, while sed 's/old/new/g' file.txt replaces all occurrences. Additionally, sed can delete lines matching a pattern using the delete command (d), such as sed '/pattern/d' file.txt, which removes lines containing "pattern". Advanced Text Processing Using sed Purpose: Edits text automatically. Syntax: sed [options] 'command' [file] Basic Commands: Substitute (s): Changes the first "old" to "new" in each line of file.txt. Global Substitute (g): Changes all "old" to "new" in each line of file.txt. Delete (d): Deletes lines that match "pattern" in file.txt. Advanced Text Processing Using awk awk is a powerful programming language for text processing and data extraction, especially useful for working with structured text like CSV files. It allows you to define patterns and actions to be performed on text lines that match those patterns. The basic syntax of awk is awk 'pattern { action }' [file]. For example, awk '{ print $1 }' file.txt prints the first column of each line in file.txt. awk supports various operations, such as printing specific fields, performing calculations, and conditional processing. For instance, awk '$3 > 100 { print $0 }' file.txt prints lines where the third column value is greater than 100. The field separator can be specified with the -F option, enabling easy handling of different file formats, such as CSV files. Advanced Text Processing Using awk Purpose: Works with structured text (like tables). Syntax: awk 'pattern { action }' [file] Basic Usage: Prints the first column of each line in file.txt. Common Patterns and Actions: Print Specific Field: Prints the second column of each line. Conditional Processing: Prints lines where the third column is greater than 100. Advanced Text Processing Using awk Field Separator: Uses a comma as the separator and prints the first column from a CSV file. Examples of Advanced Text Processing grep Example: Finds all lines with "error" (ignoring case) in logfile.txt. sed Example: Changes all numbers to # in file.txt. awk Example: Prints the first and third columns from data.csv, using comma as the separator. Function in Shell Scripting A function is a collection of statements that execute a specific task. Its main goal is to break down a complicated procedure into simpler subroutines that can subsequently be used to accomplish the more complex routine, Benefits: Assist with code reuse Enhance the programs readability Modularize the software Allow for easy maintence. Function in Shell Scripting The basic structure of a function in shell scripting looks as fallows: Function_name() { // body of the function } The function name can be any valid string and the body can be any sequence of valid statements in the scripting languange Debugging Techniques Common Issues: Syntax Errors: Mistyped commands or incorrect syntax Logical Errors: Correct syntax but incorrect logic leading to unexpected results. Runtime Errors: Errors that occurs when the script is executed, such as missing files. Debugging Techniques Basic Debugging with ‘echo’ Insert ‘echo’ statement to print variable value and checkpoints. Example: Simple and effective for small script. Helps track the flow and variable states Debugging Techniques Enable Debugging: ‘set –x’ to print each command before execution. ‘set +x to disable debugging. Benefits: Provide detailed command execution trace. Useful for identifying where the script fails. Debugging Techniques Using: set –e’ and ‘set –u’ ‘set –e’ Exist the script immediately if any command returns a nn-zero exit status. Debugging Techniques Using: set –e’ and ‘set –u’ Set –u Treats unset variables as an error and exits immediately. Example: Debugging Techniques Using ‘trap’ for Debugging Execute a command when the script receives a single or encounters an error. Useful for debugging and cleanup Exit Codes Exit Codes: Definition: Numeric codes returned by commands to indicate success(0) or failure(non-zero). Checking Exit Codes: Use ;$?’ to check the exit status of the last command. Error Handling Conditional Checks: Using ‘trap’ for cleanup: Best Practices and Conventions Code Readability: Comments: Use comment to explain the purpose and logic of code. Naming Convention: Use meaningful names for variable and function. Indentation: Consistent indentation for better readability. Best Practices and Conventions Safety: Quoting variables: Prevent globbing and word splitting. ‘set –e’ : Exit script on any command failure. Best Practices and Conventions Performance: Avoid Unnecessary Subshells: Use Built-in Command: Prefer built-in shell commands over external ones for efficiency. References ❖ https://www.youtube.com/watch?v=emhouufDnB4 ❖ https://github.com/awesome-lists/awesome-bash?tab=readme- ov-file#books-and-resources www.paruluniversity.ac.in