Scenario-Based Unix Shell Scripting Interview Questions and Answers (2025)
Top Scenario-Based Unix Shell Scripting Interview Questions and Answers (2025)
1. Scenario: Find and archive all
.log
files older than 7 days Question:
How would you write a shell script to find all
Answer:
Queries: shell script to archive log files, find old files in Unix, automate log rotation in bash.
2. Scenario: Monitor disk space usage and send an alert
Question:
Write a Unix shell script to monitor disk usage and email an alert if usage exceeds 90%.
Answer:
Queries: shell script for disk monitoring, Unix alert on high disk usage, automate server health check.
How would you write a shell script to find all
.log
files in /var/logs
older than 7 days and archive them into a .tar.gz
file?Answer:
#!/bin/bash
find /var/logs -name "*.log" -type f -mtime +7 > old_logs.txt
tar -czf archived_logs_$(date +%F).tar.gz -T old_logs.txt
Queries: shell script to archive log files, find old files in Unix, automate log rotation in bash.
2. Scenario: Monitor disk space usage and send an alert
Question:
Write a Unix shell script to monitor disk usage and email an alert if usage exceeds 90%.
Answer:
#!/bin/bash
THRESHOLD=90
USAGE=$(df / | grep / | awk '{print $5}' | sed 's/%//')
if [ "$USAGE" -ge "$THRESHOLD" ]; then
echo "Disk usage is critically high: ${USAGE}%" | mail -s "Disk Space Alert" admin@example.com
fi
Queries: shell script for disk monitoring, Unix alert on high disk usage, automate server health check.
3. Scenario: Remove duplicate lines from a file
Question:
How would you write a shell script that removes duplicate lines from a file while preserving the order?
Answer:
#!/bin/bash
awk '!seen[$0]++' input.txt > output.txt
Queries: remove duplicates in Unix file, awk to clean data, deduplicate records shell script.
4. Scenario: Rename all files with a specific extension
Question:
Write a shell script to rename all
.txt
files in a directory by adding today’s date as a
prefix.Answer:
#!/bin/bash
for file in *.txt; do
mv "$file" "$(date +%F)_$file"
done
Queries: bulk rename files bash, rename script with date, Unix shell automation tasks.
5. Scenario: Parse and extract data from a CSV file
Question:
Create a script that extracts the second column from a CSV file.
Answer:
#!/bin/bash
cut -d ',' -f2 data.csv > column2.txt
Queries: extract column from CSV Unix, bash script parse CSV, data extraction shell script.
6. Scenario: Monitor a process and restart if it fails
Question:
How can you monitor a critical process and automatically restart it if it stops?
Answer:
#!/bin/bash
PROCESS="apache2"
if ! pgrep $PROCESS > /dev/null
then
systemctl start $PROCESS
echo "$PROCESS restarted on $(date)" >> process_monitor.log
fi
Queries: auto-restart Unix process, shell script process monitoring, Linux service watch script.
7. Scenario: Compare two files and find differences
Question:
Write a shell script to compare two files and list lines that are different.
Answer:
#!/bin/bash
diff file1.txt file2.txt > differences.txt
Queries: compare files bash, find differences in Unix, diff command script example.
8. Scenario: Schedule a script to run every 15 minutes
Question:
How would you schedule your shell script to execute every 15 minutes?
Answer:
Use
crontab -e
and add:*/15 * * * * /path/to/your_script.sh
Queries: schedule script in crontab, run every 15 minutes cron, automate shell job timing.
9. Scenario: Check if a file is empty and handle accordingly
Question:
Write a script to check if a file is empty and print a message.
Answer:
#!/bin/bash
if [ ! -s filename.txt ]; then
echo "File is empty"
else
echo "File has content"
fi
Queries: check empty file Unix, shell script file validation, file condition bash.
10. Scenario: Count occurrences of a word in a file
Question:
How would you count the number of times a word appears in a log file?
Answer:
#!/bin/bash
grep -o "ERROR" logfile.txt | wc -l
Queries: count word in Unix file, grep count bash, search logs script.
Bonus: Tips to Tackle Shell Scripting Scenario Questions
Think in terms of automation and system tasks.
Use tools like
awk
, sed
, grep
, cut
, find
, xargs
.Practice combining commands with pipes (
|
) and redirection (>
, >>
).
Comments
Post a Comment