Bash Scripting Interview Questions and Answers
Bash Scripting Interview Questions and Answers
examples
Sure! Here are 30 commonly asked bash scripting interview questions with answers
and examples:
```bash
#!/bin/bash
```
```bash
name="John"
```
```bash
echo "Enter your name: "
read name
```
```bash
if [ $age -gt 18 ]; then
echo "You are an adult."
else
echo "You are a minor."
fi
```
```bash
for i in {1..5}; do
echo $i
done
```
```bash
echo "First argument: $1"
echo "Second argument: $2"
```
```bash
echo "Hello, World!" > output.txt
```
```bash
if [ -e file.txt ]; then
echo "File exists."
else
echo "File does not exist."
fi
```
```bash
date=$(date +%Y-%m-%d)
echo "Today's date is $date"
```
```bash
fruits=("apple" "banana" "orange")
echo ${fruits[0]} # Output: apple
```
```bash
greet() {
echo "Hello, $
1!"
}
greet "John" # Output: Hello, John!
```
13. Q: How do you handle errors and exit codes in bash scripting?
A: You can use the `$?` variable to access the exit code of the last command and
handle errors using conditional statements. For example:
```bash
command
if [ $? -ne 0 ]; then
echo "Error: Command failed."
exit 1
fi
```
```bash
while getopts ":u:p:" opt; do
case $opt in
u)
username=$OPTARG
;;
p)
password=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
esac
done
```
```bash
echo "Home directory: $HOME"
```
```bash
name="John Doe"
echo "First name: ${name%% *}"
echo "Last name: ${name#* }"
echo "Length: ${#name}"
```
```bash
a=5
b=3
sum=$((a + b))
echo "Sum: $sum"
```
18. Q: How do you handle signals in bash scripting?
A: You can use the `trap` command to catch and handle signals in bash scripting.
For example:
```bash
trap "echo 'Signal caught!'" SIGINT
```
```bash
cp file.txt backup/file.txt
```
```bash
#!/bin/bash -x
```
These are 20 commonly asked bash scripting interview questions with answers and
examples. Remember to customize your answers and provide additional details based
on your specific experience and projects. Good luck with your interview!
Certainly! Here are 10 more commonly asked bash scripting interview questions with
answers and examples:
21. Q: How do you handle command-line arguments with spaces in bash scripting?
A: Command-line arguments with spaces can be handled by enclosing them in quotes.
For example:
```bash
# Script: myscript.sh
echo "Arguments: $@"
```
```bash
result=$(echo "scale=2; 5.5 / 2" | bc)
echo "Result: $result"
```
```bash
command
if [ $? -ne 0 ]; then
echo "Error: Command failed."
exit 1
fi
```
```bash
name="John"
if [ "$name" == "John" ]; then
echo "Name is John."
fi
```
```bash
for file in /path/to/directory/*; do
echo "Processing: $file"
done
```
```bash
name="John Doe"
if [[ $name == *"Doe"* ]]; then
echo "Substring found."
fi
```
27. Q: How do you convert lowercase to uppercase (and vice versa) in bash
scripting?
A: You can use the `tr` command to perform case conversions. For example:
```bash
text="Hello World"
upper=$(echo "$text" | tr '[:lower:]' '[:upper:]')
lower=$(echo "$text" | tr '[:upper:]' '[:lower:]')
echo "Uppercase: $upper"
echo "Lowercase: $lower"
```
Output:
```
Uppercase: HELLO WORLD
Lowercase: hello world
```
```bash
if pgrep "nginx" >/dev/null; then
echo "Nginx is running."
else
echo "Nginx is not running."
fi
```
29
```bash
if [ "$name" == "John" ] && [ "$age" -gt 18 ]; then
echo "Valid condition."
fi
```
30. Q: How do you extract specific lines from a file in bash scripting?
A: You can use the `grep` command with options like `-n` (line numbers) and `-A`
(after context) to extract specific lines from a file. For example:
```bash
grep -n "error" logfile.txt
```
Output:
```
10:Error: Something went wrong.
15:Error: File not found.
```
These are 30 commonly asked bash scripting interview questions with answers and
examples. Remember to adapt your answers to match your experience and provide
additional details as necessary. Good luck with your interview!
give me 30 most widely asked bash scripting everyday DevOps related questions and
answers with examples
Certainly! Here are 30 commonly asked bash scripting questions related to everyday
DevOps tasks, along with answers and examples:
```bash
if [ -f /path/to/file ]; then
echo "File exists."
else
echo "File does not exist."
fi
```
```bash
fruits=("Apple" "Banana" "Orange")
for fruit in "${fruits[@]}"; do
echo "Fruit: $fruit"
done
```
3. Q: How do you iterate over files in a directory and perform an action on each
file?
A: You can use a `for` loop with the `*` wildcard to iterate over files in a
directory. For example:
```bash
for file in /path/to/directory/*; do
echo "Processing file: $file"
# Perform actions on each file
done
```
```bash
curl -O http://example.com/file.txt
```
```bash
data='{"name": "John", "age": 30}'
name=$(echo "$data" | jq -r '.name')
echo "Name: $name"
```
```bash
long_running_command &
```
```bash
# Run script.sh at 3 AM every day
0 3 * * * /path/to/script.sh
```
```bash
# Script: script.sh
echo "Argument 1: $1"
echo "Argument 2: $2"
```
Output:
```
Argument 1: value1
Argument 2: value2
```
9. Q: How do you redirect standard output and error to a file in bash scripting?
A: You can use the `>` operator to redirect standard output to a file and the `2>`
operator to redirect error output to a file. For example:
```bash
command > output.txt 2> error.txt
```
You can use the `pgrep` command to check if a process is running. For example:
```bash
if pgrep "process_name" >/dev/null; then
echo "Process is running."
else
echo "Process is not running."
fi
```
11. Q: How do you search for a specific string in a file using bash scripting?
A: You can use the `grep` command to search for a specific string in a file. For
example:
```bash
grep "search_string" /path/to/file
```
12. Q: How do you count the number of lines in a file using bash scripting?
A: You can use the `wc` command with the `-l` option to count the number of lines
in a file. For example:
```bash
line_count=$(wc -l < /path/to/file)
echo "Line count: $line_count"
```
```bash
if [ "$string1" == "$string2" ]; then
echo "Strings are equal."
else
echo "Strings are not equal."
fi
```
```bash
if [[ "$string" =~ "substring" ]]; then
echo "Substring found."
else
echo "Substring not found."
fi
```
15. Q: How do you find and replace text in a file using bash scripting?
A: You can use the `sed` command to find and replace text in a file. For example:
```bash
sed -i 's/search_text/replace_text/g' /path/to/file
```
16. Q: How do you calculate the sum of numbers in a file using bash scripting?
A: You can use a combination of `cat`, `awk`, and `bc` commands to calculate the
sum of numbers in a file. For example:
```bash
sum=$(cat numbers.txt | awk '{ sum += $1 } END { print sum }')
echo "Sum: $sum"
```
17. Q: How do you sort lines in a file in ascending order using bash scripting?
A: You can use the `sort` command to sort lines in a file in ascending order. For
example:
```bash
sort file.txt
```
18. Q: How do you check the disk space usage using bash scripting?
A: You can use the `df` command to check the disk space usage. For example:
```bash
df -h
```
19. Q: How do you calculate the execution time of a command using bash scripting?
A: You can use the `time` command to calculate the execution time of a command. For
example:
```bash
time long_running_command
```
```bash
random_number=$((RANDOM % 100))
echo "Random number: $random_number"
```
```bash
echo "Enter your name:"
read name
echo "Hello, $name!"
```
```bash
if [ -d /path/to/directory ]; then
echo "Directory exists."
else
echo "Directory does not exist."
fi
```
```bash
result=$((2 + 3))
echo "Result: $result"
# Using expr
result=$(expr 2 + 3)
echo "Result: $result"
```
24. Q: How do you check the exit status of the previous command in bash scripting?
A: You can use the `$?` variable to check the exit status of the previous command.
For example:
```bash
command
if [ $? -eq 0 ]; then
echo "Command executed successfully."
else
echo "Command failed."
fi
```
```bash
mkdir /path/to/directory
```
```bash
rmdir /path/to/directory
```bash
if [ -z "$variable" ]; then
echo "Variable is empty."
else
echo "Variable is not empty."
fi
```
28. Q: How do you get the current date and time in bash scripting?
A: You can use the `date` command to get the current date and time. For example:
```bash
current_date=$(date +%Y-%m-%d)
current_time=$(date +%H:%M:%S)
echo "Current date: $current_date"
echo "Current time: $current_time"
```
```bash
if command -v git >/dev/null; then
echo "Git command is available."
else
echo "Git command is not available."
fi
```
30. Q: How do you exit a bash script?
A: You can use the `exit` command to exit a bash script. For example:
```bash
exit
```
Keep in mind that these questions and answers provide a general overview of bash
scripting concepts. It's always recommended to explore further and adapt your
knowledge to specific scenarios and
requirements.