04 Bash scripting - gannurohith/devops-interview-wiki GitHub Wiki

📁 04 - Bash Scripting (Basic to Intermediate Q&A)

  1. What is a Bash script? A Bash script is a text file containing a sequence of commands that are executed by the Bash shell.

  2. How do you make a Bash script executable? Use chmod +x script.sh

  3. How do you run a Bash script? ./script.sh or bash script.sh

  4. How do you pass arguments to a Bash script? Use $1, $2, etc. inside the script. Example: ./script.sh arg1 arg2

  5. How do you read user input in a Bash script? read -p "Enter value: " var

  6. How do you perform conditional checks? if [ condition ]; then ... fi

  7. How do you compare two numbers in Bash? Use -eq, -ne, -gt, -lt, -ge, -le

  8. How do you compare two strings in Bash? if [ "$str1" = "$str2" ]; then ...

  9. How do you loop through a list of values? for i in val1 val2; do echo $i; done

  10. How do you loop through lines in a file? while read line; do echo "$line"; done < file.txt

  11. How do you define and use a function in Bash?

myfunc() {
  echo "Hello"
}
myfunc
  1. What is $? in Bash? It holds the exit status of the last command (0 = success).

  2. How do you exit a script with a specific code? exit 1 (for error), exit 0 (for success)

  3. What is set -e used for? Causes the script to exit on any command that fails.

  4. What is a shebang (#!) in Bash? It tells the system which interpreter to use: #!/bin/bash

  5. How do you comment in Bash? With #, e.g., # This is a comment

  6. How do you append to a file from within a script? echo "text" >> file.txt

  7. How do you check if a file exists? if [ -f filename ]; then ...

  8. How do you check if a directory exists? if [ -d dirname ]; then ...

  9. What is the difference between == and -eq? == is for strings, -eq is for numbers.

  10. How do you print the current date and time in a script? date

  11. How do you redirect both stdout and stderr to a file? command > output.txt 2>&1

  12. How do you trap signals in Bash? trap 'cleanup' SIGINT SIGTERM – runs cleanup function on CTRL+C or termination.

  13. How do you use case statement in Bash?

case $var in
  val1) echo "One";;
  val2) echo "Two";;
esac
  1. How do you perform arithmetic in Bash? result=$((a + b))

  2. What does [ ... ](/gannurohith/devops-interview-wiki/wiki/-...-) do in Bash? It's a more powerful test syntax than [ ... ], supporting pattern matching.

  3. How do you debug a Bash script? Run with bash -x script.sh

  4. How do you source another script? . script.sh or source script.sh

  5. How do you schedule a script to run periodically? Use crontab -e, add: 0 * * * * /path/script.sh

  6. How do you break out of a loop? Use break to exit the loop early.


04. Bash Scripting (Q&A)

  1. Write a script to monitor disk usage and alert if usage exceeds 80%. Answer:

    #!/bin/bash
    USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')
    if [ "$USAGE" -gt 80 ]; then
      echo "Disk usage is above 80%" | mail -s "Disk Alert" [email protected]
    fi
    
  2. What is the difference between $@ and $* in bash? Answer: $@ treats each quoted argument as a separate word; $* treats all arguments as a single string.

  3. Explain how to debug a Bash script. Answer: Add set -x to trace command execution, or run script with bash -x script.sh.

  4. How to run a script only if a file exists? Answer:

    [ -f /path/to/file ] && ./script.sh
    
  5. Write a script that reads a file line-by-line. Answer:

    while read -r line; do
      echo "$line"
    done < filename.txt
    
  6. Explain the difference between source script.sh and ./script.sh. Answer: source runs the script in the current shell; ./script.sh runs in a subshell.

  7. How to create and use a function in a script? Answer:

    greet() {
      echo "Hello, $1!"
    }
    greet John
    
  8. What is exit status? How do you capture it? Answer: It indicates command success/failure. Use $? to get the status of the last command.

  9. Script to archive logs older than 7 days. Answer:

    find /var/log -name '*.log' -mtime +7 -exec tar -rvf old-logs.tar {} \; -exec rm {} \;
    
  10. Write a script to check if a service is running. Answer:

if systemctl is-active nginx > /dev/null; then
  echo "Nginx is running"
else
  echo "Nginx is not running"
fi
  1. How to handle signals in bash? Answer: Use trap:
trap "echo Caught SIGINT" SIGINT
  1. What is the use of set -e and set -u? Answer: set -e exits script on error; set -u exits on unset variables.

  2. How do you pass arguments to a bash script? Answer: $1, $2, etc. for positional args. Example: ./script.sh arg1 arg2.

  3. Script to count number of users logged in. Answer:

who | wc -l
  1. How to loop through a list of servers and SSH into them? Answer:
for host in server1 server2; do
  ssh user@$host uptime
done
  1. What is a here document in bash? Answer: A block of text used as input to commands:
cat <<EOF
Hello World
EOF
  1. Difference between cron and at in scripting. Answer: cron schedules recurring tasks; at is for one-time scheduled jobs.

  2. How to use conditional expressions in bash? Answer:

if [ $a -gt 10 ]; then
  echo "greater"
fi
  1. Script to compare two files and print differences. Answer:
diff file1.txt file2.txt
  1. Explain the purpose of /dev/null. Answer: A special file that discards output. Use command > /dev/null 2>&1 to suppress output and errors.

  2. How to export variables from a script? Answer: Use export VAR=value. To export for other scripts, source the script.

  3. How do you handle multi-line input in bash? Answer: Use read -r -d '' var or cat <<EOF blocks.

  4. Script to find files modified in the last 24 hours. Answer:

find . -type f -mtime -1
  1. How to validate user input in bash? Answer:
read -p "Enter number: " num
[ "$num" =~ ^[0-9]+$ ](/gannurohith/devops-interview-wiki/wiki/-"$num"-=~-^[0-9]+$-) || echo "Invalid input"
  1. What is the shebang (#!) line? Why is it important? Answer: Indicates the interpreter to be used. Example: #!/bin/bash. It's necessary for execution via ./script.sh.

(Next: 05 – EC2 Q&A with answers coming up)