0% found this document useful (0 votes)
3 views2 pages

Shell Scripting DevOps Cheatsheet

This document is a cheatsheet for shell scripting in DevOps, covering key concepts such as creating and running scripts, variable usage, control structures, and error handling. It also includes commands for scheduling scripts with cron, parsing command-line options, and automating tasks like Docker cleanup and backups. The content is aimed at preparing for interviews related to shell scripting and DevOps practices.

Uploaded by

Ali Akkas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Shell Scripting DevOps Cheatsheet

This document is a cheatsheet for shell scripting in DevOps, covering key concepts such as creating and running scripts, variable usage, control structures, and error handling. It also includes commands for scheduling scripts with cron, parsing command-line options, and automating tasks like Docker cleanup and backups. The content is aimed at preparing for interviews related to shell scripting and DevOps practices.

Uploaded by

Ali Akkas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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 script.sh`
■. `chmod +x script.sh`
■. `./script.sh`

Q: Difference between `sh script.sh` and `./script.sh`


A: `sh script.sh` → shell ■■■■ ■■■■■
`./script.sh` → direct executable file ■■■■■ (needs +x permission)

Q: What does `chmod +x script.sh` 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 script.sh` 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/script.sh`

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 `>> log.txt` 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 file.txt ]`

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).tar.gz /folder`

Q: Give a real-world automation example.


A: ■■■■ ■■■■■■■■ nginx install, ssl certbot setup – ■■ automate ■■■

You might also like