# Day 6 - Shell Scripting & Linux Interview Questions for DevOps Engineers

## **1\. List some commonly used shell commands.**

**Answer:**  
“In my day-to-day DevOps work, I mostly use basic operational commands such as:

* `ls` — list files
    
* `cp`, `mv` — copy and move files
    
* `mkdir`, `touch` — create directories and files
    
* `vim` — edit files
    
* `grep`, `awk`, `sed` — filtering and text processing
    
* `find` — search for files
    
* `df`, `du` — disk usage
    
* `top`, `ps` — process monitoring
    
* `tail -f` — read logs in real-time
    

Advanced debugging commands like `netcat`, `traceroute`, `route` I use only during troubleshooting.”

---

## **2\. Write a shell script to list all processes.**

**Script:**

```plaintext
#!/bin/bash
ps -ef
```

If asked to print only **process IDs**:

```plaintext
ps -ef | awk '{print $2}'
```

---

## **3\. Write a script to print only ERROR lines from a remote log file.**

**Answer:**  
“Using `curl`, `grep`, and a pipe.”

**Example:**

```plaintext
curl -s <remote_log_url> | grep "ERROR"
```

---

## **4\. Write a script to print numbers divisible by 3 and 5 but NOT by 15 (range 1–100).**

**Script:**

```plaintext
#!/bin/bash
for i in {1..100}
do
    if { [ $((i % 3)) -eq 0 ] || [ $((i % 5)) -eq 0 ]; } \
       && [ $((i % 15)) -ne 0 ]; then
        echo $i
    fi
done
```

---

## **5\. Script to count number of occurrences of ‘s’ in “Mississippi”.**

**Script:**

```plaintext
#!/bin/bash
word="Mississippi"
echo "$word" | grep -o "s" | wc -l
```

---

## **6\. How will you debug a shell script?**

**Answer:**  
“By enabling debug mode using `set -x` at the top of the script.  
It prints each command before execution.”

```plaintext
set -x
```

---

## **7\. What is crontab? Provide an example.**

**Answer:**  
“Crontab is used to schedule recurring jobs in Linux, like backups, reports, cleanup, etc.”

**Example: run a script every day at 6 PM**

```plaintext
0 18 * * * /home/ubuntu/report.sh
```

---

## **8\. How do you open a file in read-only mode with Vim?**

```plaintext
vim -R filename
```

---

## **9\. Difference between soft link and hard link.**

### **Hard Link**

* Points directly to the inode.
    
* Original and link are identical.
    
* If original file is deleted, data remains.
    
* Cannot link directories.
    
* Cannot link across different filesystems.
    

### **Soft Link (Symbolic Link)**

* Acts like a shortcut referencing the path.
    
* If original file is deleted, soft link breaks.
    
* Can link directories.
    
* Can link across filesystems.
    

**Example:**  
Python → python3 alias is created using soft links.

---

## **10\. Difference between break and continue.**

### **break**

* Exits/terminates the loop completely.
    

### **continue**

* Skips the current iteration and continues loop execution.
    

Example use cases:

* `break`: Stop when a condition is met.
    
* `continue`: Skip unwanted values (e.g., skip multiples of 15).
    

---

## **11\. Disadvantages of shell scripting.**

**Answer:**

* Not statically typed → variables can cause runtime errors.
    
* Difficult to debug large scripts.
    
* Poor error handling compared to modern languages.
    
* No portability for platform-specific commands.
    
* Complex logic becomes unreadable.
    
* Slower execution vs. compiled languages.
    

---

## **12\. Types of loops in shell scripting.**

* **for loop** – iterate over range, list, or output
    
* **while loop** – run until a condition is false
    
* **until loop** – run until condition becomes true
    
* **select loop** – menu based input
    

---

## **13\. Is Bash statically or dynamically typed?**

**Answer:**  
“Bash is dynamically typed. Variables do not require data type declarations, and types are determined at runtime.”
