Role-Specific Questions (Developer, Administrator, DevOps)
Developer: How would you debug a shell script that is failing intermittently without clear error messages?
Answer:
I would start by adding set -x at the beginning of the script to enable tracing, which shows commands and their arguments as they are executed. For more targeted debugging, I'd use echo statements to print variable values at critical points. Redirecting stderr to a file (2> error.log) can also help capture elusive errors.
Developer: Explain the difference between $() and `` (backticks) for command substitution.
Answer:
Both $() and ``(backticks) perform command substitution, executing a command and replacing it with its output. However,$() is generally preferred because it allows for nesting without complex escaping and is more readable. Backticks require escaping nested backticks, making them harder to manage.
Developer: Write a shell script to find all files larger than 10MB in a given directory and its subdirectories, then list them by size in descending order.
Answer:
find /path/to/dir -type f -size +10M -print0 | xargs -0 du -h | sort -rh
This command uses find to locate files, xargs to pass them to du for size reporting, and sort -rh to sort by human-readable size in reverse order.
Administrator: How would you monitor disk space usage on a Linux server and set up an alert if it exceeds 90%?
Answer:
I would use df -h to check disk space. To automate alerts, I'd write a script that parses df output, checks the percentage for critical partitions, and then uses mail or a messaging API (like Slack webhook) to send an alert if the threshold is crossed. This script would be scheduled via cron.
Administrator: Describe the steps to automate a daily backup of a specific directory to a remote server using SSH.
Answer:
First, ensure SSH key-based authentication is set up between the source and destination servers to avoid password prompts. Then, use rsync -avz /source/dir/ user@remote:/destination/dir/ within a shell script. Schedule this script to run daily using a cron job, ensuring proper logging and error handling.
Administrator: What is the purpose of the /etc/fstab file, and what are common issues you might encounter with it?
Answer:
/etc/fstab defines static file systems to be mounted at boot time. Common issues include incorrect device paths, wrong file system types, or invalid mount options, which can lead to boot failures or partitions not being mounted. Using nofail can prevent boot issues for non-critical mounts.
DevOps: How do you ensure idempotency in your shell scripts for infrastructure provisioning?
Answer:
Idempotency means running a script multiple times yields the same result as running it once. I achieve this by checking for the existence of resources before creating them (e.g., if [ ! -f /path/to/file ]; then ... fi). For package installations, I use package managers that handle idempotency (e.g., apt install -y package only installs if not present). Configuration management tools like Ansible or Puppet inherently provide idempotency.
DevOps: Explain how you would use a shell script in a CI/CD pipeline to deploy an application.
Answer:
In a CI/CD pipeline, a shell script would typically handle tasks like fetching artifacts, stopping existing services, deploying new code (e.g., copying files, extracting archives), running database migrations, and starting services. It would include error handling and logging, often interacting with systemctl or docker commands. Environment variables would be used for configuration.
DevOps: What are some best practices for writing robust and maintainable shell scripts in a team environment?
Answer:
Best practices include using set -euo pipefail for error handling, adding comments, using functions to modularize code, consistent naming conventions, validating inputs, and providing clear usage instructions. Version control, linting tools (like ShellCheck), and thorough testing are also crucial for team collaboration and maintainability.
DevOps: How would you handle secrets (e.g., API keys, passwords) in shell scripts within a CI/CD context?
Answer:
Secrets should never be hardcoded. In CI/CD, I would use environment variables provided by the CI/CD platform (e.g., Jenkins credentials, GitLab CI/CD variables). For more sensitive or complex scenarios, I'd integrate with a secrets management system like HashiCorp Vault or AWS Secrets Manager, retrieving secrets at runtime rather than storing them in scripts or repositories.