04 Bash scripting - gannurohith/devops-interview-wiki GitHub Wiki
📁 04 - Bash Scripting (Basic to Intermediate Q&A)
-
What is a Bash script? A Bash script is a text file containing a sequence of commands that are executed by the Bash shell.
-
How do you make a Bash script executable? Use
chmod +x script.sh
-
How do you run a Bash script?
./script.sh
orbash script.sh
-
How do you pass arguments to a Bash script? Use
$1
,$2
, etc. inside the script. Example:./script.sh arg1 arg2
-
How do you read user input in a Bash script?
read -p "Enter value: " var
-
How do you perform conditional checks?
if [ condition ]; then ... fi
-
How do you compare two numbers in Bash? Use
-eq
,-ne
,-gt
,-lt
,-ge
,-le
-
How do you compare two strings in Bash?
if [ "$str1" = "$str2" ]; then ...
-
How do you loop through a list of values?
for i in val1 val2; do echo $i; done
-
How do you loop through lines in a file?
while read line; do echo "$line"; done < file.txt
-
How do you define and use a function in Bash?
myfunc() {
echo "Hello"
}
myfunc
-
What is
$?
in Bash? It holds the exit status of the last command (0 = success). -
How do you exit a script with a specific code?
exit 1
(for error),exit 0
(for success) -
What is
set -e
used for? Causes the script to exit on any command that fails. -
What is a shebang (
#!
) in Bash? It tells the system which interpreter to use:#!/bin/bash
-
How do you comment in Bash? With
#
, e.g.,# This is a comment
-
How do you append to a file from within a script?
echo "text" >> file.txt
-
How do you check if a file exists?
if [ -f filename ]; then ...
-
How do you check if a directory exists?
if [ -d dirname ]; then ...
-
What is the difference between
==
and-eq
?==
is for strings,-eq
is for numbers. -
How do you print the current date and time in a script?
date
-
How do you redirect both stdout and stderr to a file?
command > output.txt 2>&1
-
How do you trap signals in Bash?
trap 'cleanup' SIGINT SIGTERM
– runscleanup
function on CTRL+C or termination. -
How do you use
case
statement in Bash?
case $var in
val1) echo "One";;
val2) echo "Two";;
esac
-
How do you perform arithmetic in Bash?
result=$((a + b))
-
What does
[ ... ](/gannurohith/devops-interview-wiki/wiki/-...-)
do in Bash? It's a more powerful test syntax than[ ... ]
, supporting pattern matching. -
How do you debug a Bash script? Run with
bash -x script.sh
-
How do you source another script?
. script.sh
orsource script.sh
-
How do you schedule a script to run periodically? Use
crontab -e
, add:0 * * * * /path/script.sh
-
How do you break out of a loop? Use
break
to exit the loop early.
04. Bash Scripting (Q&A)
-
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
-
What is the difference between
$@
and$*
in bash? Answer:$@
treats each quoted argument as a separate word;$*
treats all arguments as a single string. -
Explain how to debug a Bash script. Answer: Add
set -x
to trace command execution, or run script withbash -x script.sh
. -
How to run a script only if a file exists? Answer:
[ -f /path/to/file ] && ./script.sh
-
Write a script that reads a file line-by-line. Answer:
while read -r line; do echo "$line" done < filename.txt
-
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. -
How to create and use a function in a script? Answer:
greet() { echo "Hello, $1!" } greet John
-
What is exit status? How do you capture it? Answer: It indicates command success/failure. Use
$?
to get the status of the last command. -
Script to archive logs older than 7 days. Answer:
find /var/log -name '*.log' -mtime +7 -exec tar -rvf old-logs.tar {} \; -exec rm {} \;
-
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
- How to handle signals in bash?
Answer: Use
trap
:
trap "echo Caught SIGINT" SIGINT
-
What is the use of
set -e
andset -u
? Answer:set -e
exits script on error;set -u
exits on unset variables. -
How do you pass arguments to a bash script? Answer:
$1
,$2
, etc. for positional args. Example:./script.sh arg1 arg2
. -
Script to count number of users logged in. Answer:
who | wc -l
- How to loop through a list of servers and SSH into them? Answer:
for host in server1 server2; do
ssh user@$host uptime
done
- What is a here document in bash? Answer: A block of text used as input to commands:
cat <<EOF
Hello World
EOF
-
Difference between
cron
andat
in scripting. Answer:cron
schedules recurring tasks;at
is for one-time scheduled jobs. -
How to use conditional expressions in bash? Answer:
if [ $a -gt 10 ]; then
echo "greater"
fi
- Script to compare two files and print differences. Answer:
diff file1.txt file2.txt
-
Explain the purpose of
/dev/null
. Answer: A special file that discards output. Usecommand > /dev/null 2>&1
to suppress output and errors. -
How to export variables from a script? Answer: Use
export VAR=value
. To export for other scripts,source
the script. -
How do you handle multi-line input in bash? Answer: Use
read -r -d '' var
orcat <<EOF
blocks. -
Script to find files modified in the last 24 hours. Answer:
find . -type f -mtime -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"
- 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)