Shell Scripting for DevOps – Interview Cheatsheet
Q: What is a shell script?
A: Shell script ■■ ■■■■■■ Linux command-■■ ■■■■■■, ■■ ■■■■ `.sh` ■■■■■ ■■■■ ■■ ■■■ shell ■■■■■■■ ■■
Q: How do you create and run a shell script?
A: ■. `nano [Link]`
■. `chmod +x [Link]`
■. `./[Link]`
Q: Difference between `sh [Link]` and `./[Link]`
A: `sh [Link]` → shell ■■■■ ■■■■■
`./[Link]` → direct executable file ■■■■■ (needs +x permission)
Q: What does `chmod +x [Link]` do?
A: ■■■■■■■■ executable ■■■, ■■■ ■■■■■■■■■■■ ■■■ ■■■■■■ ■■■■
Q: How to define and use a variable?
A: `name="Ali"`
`echo "Hello $name"`
Q: What are `$@` and `$#`?
A: `$@` = ■■ arguments
`$#` = ■■■■■■ argument ■■■■■
Q: How to write if-else in bash?
A: `if [ "$x" -gt 5 ]; then ... else ... fi`
Q: How to write a for loop?
A: `for i in 1 2 3; do echo $i; done`
Q: How to compare numbers and strings?
A: `-eq`, `-ne`, `-lt`, `-gt`, `-ge`, `-le` → ■■■■■■■ ■■■■
`==`, `!=` → ■■■■■■■ ■■ ■■■■
Q: What is the difference between `[` and `[[`?
A: `[[` is bash-specific (more powerful), `[` is POSIX standard
Q: How to debug a shell script?
A: `bash -x [Link]` or use `set -x` in script
Q: How to handle errors in bash?
A: `set -e` → error ■■■ ■■■■■■■■■ ■■■■
`$?` → exit status check
Q: How to schedule script with cron?
A: `crontab -e`
Example: `0 2 * * * /path/to/[Link]`
Q: How to pass arguments to a script?
A: Use `$1`, `$2`, ... or loop through `$@`
Q: What is `getopts`?
A: Used for parsing command-line options like `-f -h`
Q: How to write functions in bash?
A: `myfunc() { echo Hello; }`
Q: How to handle logs in script?
A: Use `>> [Link]` or `logger`
Q: Monitor disk usage and alert?
A: `df / | awk` ■■■■ usage ■■■ ■■■, limit ■■■■■■ `mail` ■■ `echo` ■■■■ alert
Q: What is the difference between subshell and current shell?
A: `( commands )` → subshell
`{ commands; }` → current shell
Q: How to check if a file exists and not empty?
A: `[ -s [Link] ]`
Q: Where do you use shell in CI/CD?
A: Build, test, deploy step automate ■■■■
Q: How to clean docker containers?
A: `docker container/image/volume prune -f`
Q: Dynamic backup script?
A: `tar -czf backup_$(date).[Link] /folder`
Q: Give a real-world automation example.
A: ■■■■ ■■■■■■■■ nginx install, ssl certbot setup – ■■ automate ■■■