Section A: Basics (variables, conditions, loops)
Q1. Write a script to check whether a number is even or odd.
#!/bin/bash
read -p "Enter a number: " n
if (( n % 2 == 0 )); then echo "$n is Even"; else echo "$n is Odd"; fiQ2. Write a script to find the factorial of a number.
#!/bin/bash
read -p "Enter n: " n
f=1
for (( i=2; i<=n; i++ )); do f=$(( f * i )); done
echo "Factorial of $n = $f"Q3. Accept command-line arguments and print their count and sum.
#!/bin/bash
echo "Count: $#"
sum=0
for x in "$@"; do sum=$(( sum + x )); done
echo "Sum: $sum"./sum.sh 4 8 15 gives Count: 3 and Sum: 27.
Q4. Print the first n Fibonacci numbers.
#!/bin/bash
read -p "Terms: " n
a=0; b=1
for (( i=0; i<n; i++ )); do
echo -n "$a "; t=$(( a + b )); a=$b; b=$t
done; echoQ5. Write a menu-driven calculator using case.
#!/bin/bash
read -p "Enter two numbers: " a b
echo "1.Add 2.Sub 3.Mul 4.Div"
read -p "Choice: " c
case $c in
1) echo $(( a + b )) ;;
2) echo $(( a - b )) ;;
3) echo $(( a * b )) ;;
4) if [ "$b" -eq 0 ]; then echo "Cannot divide by zero"
else echo "scale=2; $a/$b" | bc; fi ;;
*) echo "Invalid choice" ;;
esacSection B: Functions, arrays, files
Q6. Write a function to check whether a string is a palindrome.
#!/bin/bash
is_palindrome() { [ "$1" == "$(echo "$1" | rev)" ]; }
read -p "String: " s
if is_palindrome "$s"; then echo "Palindrome"; else echo "Not a palindrome"; fiQ7. Store numbers in an array and print their average.
#!/bin/bash
nums=(10 20 30 40 50)
sum=0
for n in "${nums[@]}"; do sum=$(( sum + n )); done
echo "Average: $(echo "scale=2; $sum/${#nums[@]}" | bc)"Output: Average: 30.00
Q8. Check whether a file exists and is readable, then print its lines, words and characters.
#!/bin/bash
read -p "File name: " f
if [ -f "$f" ] && [ -r "$f" ]; then
echo "Lines: $(wc -l < "$f") Words: $(wc -w < "$f") Chars: $(wc -c < "$f")"
else
echo "File not found or not readable"
fiSection C: Data analytics with awk, sed, sort, uniq
Q9. Find the number of records and columns in sales.csv.
echo "Records: $(( $(wc -l < sales.csv) - 1 ))"
awk -F, 'NR==1 {print "Columns:", NF}' sales.csvOutput: Records: 6 and Columns: 6.
Q10. Calculate total revenue (qty × price) per region.
awk -F, 'NR>1 {rev[$3] += $5*$6} END {for (r in rev) print r, rev[r]}' sales.csvOutput (order may vary): Manama 3350, Muharraq 3450, Riffa 2000.
Q11. Display the top 3 products by revenue.
awk -F, 'NR>1 {rev[$4] += $5*$6} END {for (p in rev) print p, rev[p]}' sales.csv \
| sort -k2 -nr | head -3Output: Phone 6000, Laptop 2800, Tablet 0.
Q12. Replace empty fields with NA and save to clean.csv.
awk 'BEGIN{FS=OFS=","} {for(i=1;i<=NF;i++) if($i=="") $i="NA"} 1' sales.csv > clean.csvQ13. Remove duplicate rows from a CSV file.
awk '!seen[$0]++' sales.csv > unique.csvQ14. Display the header and all rows where qty is greater than 10.
awk -F, 'NR==1 || $5 > 10' sales.csvQ15. Find the mean, minimum and maximum of the qty column, ignoring missing values.
awk -F, 'NR>1 && $5!="" {s+=$5; n++; if(min==""||$5<min) min=$5; if($5>max) max=$5}
END {print "Mean:", s/n, "Min:", min, "Max:", max}' sales.csvOutput: Mean: 7.6 Min: 3 Max: 12
Q16. From a web server log access.log (IP is the first field), list the top 5 IP addresses and count the 404 errors.
awk '{print $1}' access.log | sort | uniq -c | sort -nr | head -5
grep -c ' 404 ' access.logSection D: Automation
Q17. Write a backup script that creates a timestamped compressed archive, and schedule it daily at 2 AM.
#!/bin/bash
src="$HOME/data"; dest="$HOME/backup"
mkdir -p "$dest"
tar -czf "$dest/data_$(date +%Y%m%d_%H%M%S).tar.gz" "$src" && echo "Backup completed"Cron entry (add with crontab -e):
0 2 * * * /home/user/backup.shQ18. Rename all .txt files to .csv, then merge part_*.csv files keeping only one header.
for f in *.txt; do mv "$f" "${f%.txt}.csv"; done
head -n 1 part_1.csv > merged.csv
for f in part_*.csv; do tail -n +2 "$f" >> merged.csv; doneQuick viva questions
- What does the first line
#!/bin/bashdo? It is the shebang. It tells the system which interpreter runs the script. - How do you make a script executable?
chmod +x script.sh, then run it with./script.sh. - What is
$?? The exit status of the last command (0 means success). $*vs$@?"$@"keeps each argument separate."$*"joins them into one string.>vs>>?>overwrites the file.>>appends to it.- Single vs double quotes? Single quotes print text literally. Double quotes allow variable and command expansion.
- What does
set -edo? It stops the script as soon as any command fails. - What does a pipe
|do? It passes the output of one command as the input of the next. - Difference between
grep,sedandawk?grepsearches,sededits text streams, andawkprocesses data by fields and columns. - How do you do decimal arithmetic? Use
bcorawk, because$(( ))handles integers only.



















