Shell Scripting and Linux Interview Questions

Preparing for Shell Scripting and Linux Interviews

Day 9 of 100 Days of DevOps Challenge: Preparing for Shell Scripting and Linux Interviews

Mastering shell scripting is a core skill for DevOps engineers, but excelling in interviews requires a clear understanding of both foundational and advanced topics. In this post, I’ve compiled common interview questions and insights.

Key Shell Scripting and Linux Interview Questions

  • What are some essential shell commands for everyday use?

    • Commands like ls, cp, mv, mkdir, and grep are widely used. Be ready to explain their usage in daily tasks or troubleshooting scenarios.
  • How do you write a shell script to list all processes running on a system?

    • Example: Use ps -ef to list processes. You can enhance the script by extracting specific fields using tools like awk to display only Process IDs (PIDs).
  • How would you retrieve and filter logs from a remote server?

    • Combine curl with grep. For example:

        bash
      
         codecurl <URL> | grep "error"
      
    • This command fetches logs and filters lines containing "error."

  • What is the difference between hard links and soft links in Linux?

    • Hard links point directly to the data on the disk, while soft links (or symbolic links) point to another file. Explain use cases for both.
  • Can you explain how you’d write a script to print numbers divisible by 3 and 5 but not by 15?

    • Use a for loop with conditional statements. Example:

        bashCopy codefor i in {1..100}; do
          if (( i % 3 == 0 && i % 5 == 0 && i % 15 != 0 )); then
            echo $i
          fi
        done
      
  • What debugging techniques do you use in shell scripting?

    • Use set -x at the beginning of the script to enable debug mode. This shows each command as it is executed.
  • How would you schedule a task to run every day at 6 PM using crontab?

    • Add this line to the crontab file:

        0 18 * * * /path/to/your/script.sh
      
  • How do you manage log files to prevent disk space issues?

    • Use logrotate to automate compression and deletion of old log files. Example configuration:

        /var/log/myapp/*.log {
            daily
            rotate 7
            compress
            delaycompress
            missingok
        }
      
  • What is the purpose of loop control statements in shell scripting?

    • Statements like break and continue help manage flow in for or while loops by exiting or skipping iterations.
  • What is the significance of using conditional logic in scripts?

    • Conditional logic (if, elif, else) enables decision-making, such as handling exceptions or branching scripts based on input.

Conclusion

Preparing for shell scripting interviews requires more than just theoretical knowledge—it demands practical application and clarity on fundamental concepts. Master these questions, practice frequently, and showcase your expertise to stand out as a DevOps professional.