Shell Scripting and Automation Questions
Automating tasks with the command line: Bash and shell scripting, pipes and redirection, text processing, and writing custom tooling and monitoring scripts to glue systems together. Covers the scripting fundamentals infrastructure and platform engineers use daily, including automation-oriented use of general-purpose scripting. Distinct from application programming in its throwaway-to-durable tooling mindset.
Implement a Bash script 'rotate_logs.sh <logdir> <days> <keep>' that compresses logs in the given directory older than <days>, keeps up to <keep> compressed versions per log prefix (deleting older ones), preserves file ownership and permissions when replacing files, and is safe if multiple rotations run simultaneously. Explain assumptions and how to avoid partial states.
Describe the difference between a shell variable and an environment variable. Given the snippet:
VAR=1
bash -c 'echo $VAR'
explain why the child bash prints nothing. How can you export VAR so that child processes see it? Also explain how 'sourcing' a script affects variables and the difference between export and setting a variable inline for a single command (FOO=bar cmd).
Explain key differences between POSIX /bin/sh and Bash that impact portability of SRE scripts: arrays, '[[' vs '[', process substitution, here-strings, associative arrays, regex matching operators, and function declaration differences. Provide examples of typical 'bashisms' and how to rewrite them to be compatible with /bin/sh. What guidelines would you impose team-wide to maximize portability?
Write a script that finds world-writable regular files under /var (excluding symlinks and pseudo-filesystems like /proc and /sys), logs their previous permissions and paths, and then changes their permissions to 0644. The script should avoid acting on remote-mounted filesystems (NFS) and should run with least privilege possible. Explain how you detect remote mounts and safely avoid them.
Create a small Bash script that accepts a systemd service name as an argument, checks whether the service is active using systemctl, and returns exit code 0 if active and non-zero otherwise. The script should validate input, print a human-friendly message, and avoid false positives (e.g., incorrectly reporting inactive services as active). Mention any permissions needed to run the check.
Unlock Full Question Bank
Get access to all Shell Scripting and Automation interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.