Shell Scripting & Linux Interview Questions for DevOps Engineers | Bash Zero to Hero | #devops
Shell Scripting Interview Questions
Introduction to Shell Scripting Series
- Abhishek introduces the third episode of his shell scripting series, focusing on interview questions.
- He emphasizes the importance of watching previous videos covering basic and intermediate shell scripting concepts before tackling interview questions.
- The video will cover 20 commonly asked shell scripting interview questions gathered from various sources and personal experiences.
Commonly Asked Questions in Interviews
Question 1: List Common Shell Commands
- Interviewers often start by assessing candidates' understanding of shell scripting through common commands used daily.
- Candidates should mention frequently used commands like
ls,cp,mv,mkdir,touch, andgrep.
- It's advised to avoid mentioning advanced debugging commands unless relevant, as they are not typically used in day-to-day operations.
Question 2: Write a Simple Shell Script to List All Processes
- This question tests practical knowledge; candidates can use the command
ps -efto list all processes running on a machine.
- If asked for specific information like process IDs, candidates should explain how to filter output using tools like
awk.
- The explanation includes identifying fields in the output and using field separators effectively with commands.
Advanced Log Handling
Writing Scripts for Remote Logs
- Abhishek discusses writing scripts that can print errors from remote logs stored in cloud services like Google Storage or S3 buckets.
How to Use Curl and Grep for Log File Analysis
Introduction to Curl and Grep
- The discussion begins with the use of the
curlcommand to retrieve output from a remote file, emphasizing that instead of fetching the entire content, one can filter specific error messages.
Filtering Output with Grep
- An example is provided using
curl google.com, illustrating how to obtain specific lines such as an error message or a hyperlink reference (href).
- By piping (
|) the output ofcurlinto thegrepcommand, users can isolate desired information, like error messages.
Practical Application with Log Files
- A dummy log file is referenced from a Git repository for practice purposes. It contains various log types including info logs and trace logs.
- To extract only trace logs from this large file, one can use
curlfollowed by the URL and pipe it intogrepwith appropriate keywords.
Command Combination Explained
- The combination of commands—
curl, pipe (|), andgrep—is highlighted as essential for efficient data retrieval in interviews or practical scenarios.
- Each command has its significance:
- Curl retrieves the full log file,
- Grep filters out unwanted sections,
- Pipe connects these commands allowing seamless data flow.
Importance of Pipe Command
- Without using a pipe, executing both commands results in receiving the entire log file rather than filtered outputs. This demonstrates how critical pipes are for effective command execution.
Creating Shell Scripts for Number Filtering
Overview of Script Requirements
- The task involves writing a shell script that prints numbers divisible by three and five but not fifteen. This combines multiple common programming interview questions into one exercise.
Understanding Divisibility Conditions
- The question is broken down into three parts:
- Numbers must be divisible by 3,
- Numbers must be divisible by 5,
- Numbers should not be divisible by 15.
Example Breakdown
- Using numbers from 1 to 15 as an example:
- Divisible by three: 3, 6, 9, 12, 15
- Divisible by five: 5, 10, 15
How to Write a Shell Script for Divisibility
Understanding the Problem
- The task is to write a shell script that prints numbers divisible by 3 and 5 but not by 15. The interviewer should clarify the range of numbers (e.g., from 1 to 100).
Setting Up the Script
- Begin by creating a shell script file named
sample_script.sh. Use a shebang (#!/bin/bash) at the top of the script.
- Include metadata in your script, such as author information and purpose, although this part is skipped for brevity.
Breaking Down the Logic
- Explain your approach clearly to the interviewer: identify conditions for divisibility by 3 and 5 while ensuring it’s not divisible by 15.
- Clarify that you will check if numbers are divisible by both 3 and 5, while also confirming they are not multiples of 15.
Writing Conditions in the Script
- Start with an
ifstatement to check if a number is divisible by either condition using logical operators.
- Implement two
ifconditions: one for checking divisibility by 3 and another for divisibility by 5. Use anoroperator between them.
Finalizing Conditions
- Add an additional condition using an
andoperator to ensure that numbers are not divisible by 15.
- Combine these conditions logically: use
&&for "not divisible" checks alongside existing conditions.
Defining the Range
- Use a
forloop to define your range (1 to 100). This loop will iterate through each number within this specified range.
Executing and Testing the Script
- Change permissions on your script using
chmod +x sample_script.sh, then execute it.
How to Write a Shell Script for Counting Characters
Writing Scripts for Character Counting
- The speaker encourages practicing writing scripts for various tasks, such as counting even and odd numbers or multiples of different integers. This practice helps improve scripting skills.
- A specific example is given: counting the occurrences of the letter 's' in the word "Mississippi." The choice of this word is due to its complexity and common usage in interviews.
- The expected output for "Mississippi" is 4 (for four 's' characters), while for "Singapore," it would be 1. This illustrates how to approach character counting in strings.
Basic Structure of a Shell Script
- The script begins with a shebang (
#!/bin/bash), which indicates that it should be run in a Bash shell. Starting with basic commands is emphasized during interviews.
- It's important to communicate that you are using simple methods first before suggesting more efficient approaches, especially when discussing shell commands rather than programming complexities.
Using Commands Effectively
- To count occurrences, define the target word (e.g.,
x="mississippi"). Use thegrepcommand with options like-oto filter only instances of 's'.
- The command structure involves piping output from
grepintowc -l, which counts lines. This method effectively counts how many times 's' appears by treating each occurrence as a line.
Debugging Shell Scripts
- To debug shell scripts, use
set -xat the beginning of your script. This enables debugging mode, allowing you to trace execution step-by-step.
Understanding Cron Jobs
- A cron job automates tasks at scheduled times (e.g., sending reports daily at 6 PM). It eliminates manual execution by setting up automated scripts through cron scheduling.
How to Use Vim and Understand Links in Linux
Using Vim in Read-Only Mode
- To open a file in read-only mode, use the command
vim -R filename, which prevents any modifications to the file.
Understanding Soft Links vs. Hard Links
- In Linux, there are two types of links: soft links (symbolic links) and hard links. It's essential to understand their differences for effective file management.
- When you create a file, it is saved on disk memory. If you want multiple references to this file without duplicating its content, hard links can be used instead of copying files with the
cpcommand.
- A hard link creates a mirror copy of the original file; if the original is deleted, the hard link remains intact, preserving access to the data.
- Soft links point to another file's location; if the target (e.g., Python 3 executable) is deleted, accessing the soft link will fail since both reference the same memory address.
- Soft links are useful for creating aliases or shortcuts (e.g., linking
pythontopython3) but do not provide data redundancy like hard links do.
Break vs. Continue Statements in Loops
- The
breakstatement terminates loop execution immediately when a specified condition is met, whilecontinueskips the current iteration and proceeds with the next one.
- The concept of "continue" can be understood as skipping certain iterations based on conditions (e.g., ignoring multiples of 15).
- For example, if searching through numbers from 1 to N and finding a specific number (like 5), using
breakallows immediate termination once found rather than continuing through all iterations.
- In practical scenarios such as searching for a student's name in a list, using
breakallows stopping further processing once that name is encountered.
Disadvantages of Shell Scripting
Shell Scripting and Interview Preparation
Understanding Shell Scripting
- When preparing for a Python interview, it's important to discuss real-time use cases of shell scripting. Highlight practical disadvantages such as the lack of static typing, which allows undeclared variables without compiler complaints.
- Discuss different types of loops in shell scripting:
for,while, anddo while. Each loop serves specific purposes, and candidates should be prepared to explain their use cases.
- Emphasize that understanding the purpose of each loop is crucial. This knowledge is commonly tested in interviews, so candidates should familiarize themselves with various loop types.
Typing in Programming Languages
- Modern programming languages like Go are statically typed, requiring variable type definitions upfront. In contrast, languages like Python and shell scripting are dynamically typed.
- In dynamic typing (e.g., shell), variables can change types without prior declaration. For instance, assigning an integer to a string variable won't raise errors until execution time.
Networking Tools for Troubleshooting
- One recommended tool for network troubleshooting is
traceroute. It helps identify slow network paths by showing the number of hops from your device to a destination (e.g., google.com).
- The command provides insights into each hop's response time, helping diagnose where delays occur in the network path.
- Another useful command is
tracepath, which functions similarly but does not require root privileges. It's effective for identifying network issues without elevated permissions.
Managing System Logs
- When asked about managing large log files generated daily by applications, mention using the
logrotatecommand. This tool efficiently handles log file rotation based on defined criteria (e.g., daily rotation).
- Explain that using logrotate prevents disk space issues caused by accumulating logs over time. Candidates should understand how to configure it according to application needs.
Sorting Data Efficiently
Shell Scripting and Log Management
Overview of Log File Management
- The speaker discusses the process of compressing log files, mentioning various formats such as gzip, zip, and tar for creating compressed versions.
- A recommendation is made to delete log files after a specified duration (30 days), emphasizing the importance of managing storage effectively.
- The speaker highlights common commands related to shell scripting that are often encountered in interviews, indicating their relevance in practical applications.
Engagement with Audience
- Viewers are encouraged to ask questions in the comment section, with the speaker committing to respond to each inquiry received on their videos.
- A call-to-action is made for viewers to like and subscribe to the channel, promoting community engagement and support for future content.