Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
19 views19 pages

Shell Prog

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views19 pages

Shell Prog

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Perfect You’ve shared an exam-style sheet (Set A) with 5 shell programming questions.

You now want me to prepare a big collection of 50 questions (like an assignment/practice


set) along with solutions + shell programs.

Here’s how I’ll structure it:

50 Shell Programming Questions with


Solutions

Set A (Basic Programs)

1. Fibonacci series up to 100 ✅


2. Factorial of a number ✅
3. Multiplication table of a number ✅
4. Check odd/even ✅
5. Basic Linux commands (cat, ls -l, wc, grep) ✅

Set B (Number Based Programs)

6. Find largest of 3 numbers.


7. Check if a number is prime.
8. Print prime numbers between 1–100.
9. Print sum of digits of a number.
10. Reverse a number.

Set C (Series & Patterns)

11. Generate arithmetic progression series.


12. Generate geometric progression series.
13. Print factorial series up to n.
14. Print first n natural numbers.
15. Print even numbers between 1–50.

Set D (String Handling)

16. Count number of characters in a string.


17. Reverse a string.
18. Check if string is palindrome.
19. Count vowels and consonants in a string.
20. Convert lowercase to uppercase.

Set E (File Handling)

21. Count number of lines in a file.


22. Count number of words in a file.
23. Find number of occurrences of a word in a file.
24. Display only even-numbered lines of a file.
25. Merge two files into one.

Set F (Loops & Conditions)

26. Print numbers from 1 to n using while loop.


27. Print numbers from n to 1 using until loop.
28. Sum of n natural numbers using for loop.
29. Check Armstrong number.
30. Print Armstrong numbers between 1–1000.

Set G (System / Environment)

31. Display current logged in user.


32. Display current date and time.
33. Display system uptime.
34. Display current shell being used.
35. Display process ID of the script.

Set H (Operators & Expressions)

36. Simple calculator using case statement.


37. Compare two numbers using relational operators.
38. Demonstrate logical operators (&&, ||).
39. Demonstrate shift command with arguments.
40. Show positional parameters $0, $1, $*.

Set I (Practical / Utility Scripts)

41. Backup a file with timestamp.


42. Delete all .tmp files in current directory.
43. Rename all .txt files to .bak.
44. Display disk usage using df.
45. Display top 10 processes using ps.
Set J (Advanced)

46. Find GCD of two numbers.


47. Find LCM of two numbers.
48. Generate Pascal’s triangle.
49. Menu-driven program (calculator, reverse, palindrome, exit).
50. Student marksheet program (average, pass/fail).

Example Programs & Solutions

Q1: Fibonacci Series up to 100


#!/bin/bash
a=0
b=1
echo "Fibonacci series up to 100:"
echo $a
echo $b
while [ $b -le 100 ]
do
c=$((a+b))
if [ $c -le 100 ]
then
echo $c
fi
a=$b
b=$c
done

Q2: Factorial of a Number


#!/bin/bash
echo "Enter a number:"
read n
fact=1
for ((i=1;i<=n;i++))
do
fact=$((fact*i))
done
echo "Factorial of $n is $fact"

Q3: Multiplication Table


#!/bin/bash
echo "Enter a number:"
read n
for ((i=1;i<=10;i++))
do
echo "$n x $i = $((n*i))"
done

Q4: Odd or Even


#!/bin/bash
echo "Enter a number:"
read n
if (( n % 2 == 0 ))
then
echo "$n is Even"
else
echo "$n is Odd"
fi

Q5: Linux Commands

A) cat → Display file contents

cat filename.txt

B) ls -l → List files with details

ls -l

C) wc → Count words, lines, characters

wc filename.txt

D) grep → Search text in a file

grep "hello" filename.txt

Now, do you want me to expand all 50 with ready-to-run shell scripts + explanations
in one big document (like a PDF or .sh file collection), or should I give you the list step by
step (5–10 programs at a time) so it’s easier to learn?
50 Shell Programming Questions with
Solutions

🔹 Part 1: Basics & Number Programs (Q1–Q10)

Q1. Fibonacci series up to 100


#!/bin/bash
a=0
b=1
echo "Fibonacci series up to 100:"
echo $a
echo $b
while [ $b -le 100 ]
do
c=$((a+b))
if [ $c -le 100 ]
then
echo $c
fi
a=$b
b=$c
done

Q2. Factorial of a number


#!/bin/bash
echo "Enter a number:"
read n
fact=1
for ((i=1;i<=n;i++))
do
fact=$((fact*i))
done
echo "Factorial of $n is $fact"

Q3. Multiplication table


#!/bin/bash
echo "Enter a number:"
read n
for ((i=1;i<=10;i++))
do
echo "$n x $i = $((n*i))"
done
Q4. Odd or Even
#!/bin/bash
echo "Enter a number:"
read n
if (( n % 2 == 0 ))
then
echo "$n is Even"
else
echo "$n is Odd"
fi

Q5. Largest of three numbers


#!/bin/bash
echo "Enter three numbers:"
read a b c
if [ $a -ge $b ] && [ $a -ge $c ]
then
echo "$a is largest"
elif [ $b -ge $a ] && [ $b -ge $c ]
then
echo "$b is largest"
else
echo "$c is largest"
fi

Q6. Prime number check


#!/bin/bash
echo "Enter a number:"
read n
flag=0
for ((i=2;i<=n/2;i++))
do
if (( n%i == 0 ))
then
flag=1
break
fi
done

if [ $flag -eq 0 ] && [ $n -gt 1 ]


then
echo "$n is Prime"
else
echo "$n is Not Prime"
fi
Q7. Prime numbers between 1–100
#!/bin/bash
echo "Prime numbers between 1 and 100:"
for ((n=2;n<=100;n++))
do
flag=0
for ((i=2;i<=n/2;i++))
do
if (( n%i == 0 ))
then
flag=1
break
fi
done
if [ $flag -eq 0 ]
then
echo -n "$n "
fi
done
echo

Q8. Sum of digits of a number


#!/bin/bash
echo "Enter a number:"
read n
sum=0
while [ $n -gt 0 ]
do
digit=$((n%10))
sum=$((sum+digit))
n=$((n/10))
done
echo "Sum of digits = $sum"

Q9. Reverse a number


#!/bin/bash
echo "Enter a number:"
read n
rev=0
while [ $n -gt 0 ]
do
digit=$((n%10))
rev=$((rev*10+digit))
n=$((n/10))
done
echo "Reversed number = $rev"
Q10. Armstrong number check
#!/bin/bash
echo "Enter a number:"
read n
num=$n
sum=0
while [ $n -gt 0 ]
do
digit=$((n%10))
sum=$((sum+digit*digit*digit))
n=$((n/10))
done
if [ $sum -eq $num ]
then
echo "$num is an Armstrong number"
else
echo "$num is not an Armstrong number"
fi

Part 2: Series & Strings (Q11–Q20)

Q11. Arithmetic Progression (AP) series


#!/bin/bash
echo "Enter first term (a), common difference (d), and number of terms
(n):"
read a d n
echo "AP series:"
for ((i=1;i<=n;i++))
do
echo -n "$a "
a=$((a+d))
done
echo

Q12. Geometric Progression (GP) series


#!/bin/bash
echo "Enter first term (a), common ratio (r), and number of terms (n):"
read a r n
echo "GP series:"
for ((i=1;i<=n;i++))
do
echo -n "$a "
a=$((a*r))
done
echo
Q13. Factorial series up to n
#!/bin/bash
echo "Enter n:"
read n
fact=1
for ((i=1;i<=n;i++))
do
fact=$((fact*i))
echo "Factorial of $i = $fact"
done

Q14. First n natural numbers


#!/bin/bash
echo "Enter n:"
read n
for ((i=1;i<=n;i++))
do
echo -n "$i "
done
echo

Q15. Even numbers between 1–50


#!/bin/bash
echo "Even numbers between 1–50:"
for ((i=2;i<=50;i+=2))
do
echo -n "$i "
done
echo

Q16. Count characters in a string


#!/bin/bash
echo "Enter a string:"
read str
len=${#str}
echo "Length of string = $len"

Q17. Reverse a string


#!/bin/bash
echo "Enter a string:"
read str
len=${#str}
rev=""
for ((i=$len-1;i>=0;i--))
do
rev="$rev${str:$i:1}"
done
echo "Reversed string = $rev"
Q18. Palindrome string check
#!/bin/bash
echo "Enter a string:"
read str
rev=$(echo $str | rev)
if [ "$str" = "$rev" ]
then
echo "$str is Palindrome"
else
echo "$str is Not Palindrome"
fi

Q19. Count vowels and consonants in a string


#!/bin/bash
echo "Enter a string:"
read str
vowels=0
consonants=0
for ((i=0;i<${#str};i++))
do
ch=${str:$i:1}
case $ch in
[aeiouAEIOU]) vowels=$((vowels+1));;
[a-zA-Z]) consonants=$((consonants+1));;
esac
done
echo "Vowels = $vowels"
echo "Consonants = $consonants"

Q20. Convert lowercase to uppercase


#!/bin/bash
echo "Enter a string:"
read str
upper=$(echo $str | tr '[:lower:]' '[:upper:]')
echo "Uppercase string = $upper"
Part 3: File Handling & Loop Programs
(Q21–Q30)

Q21. Count number of lines in a file


#!/bin/bash
echo "Enter filename:"
read file
if [ -f "$file" ]
then
lines=$(wc -l < "$file")
echo "Number of lines in $file = $lines"
else
echo "File not found!"
fi

Q22. Count number of words in a file


#!/bin/bash
echo "Enter filename:"
read file
if [ -f "$file" ]
then
words=$(wc -w < "$file")
echo "Number of words in $file = $words"
else
echo "File not found!"
fi

Q23. Find occurrences of a word in a file


#!/bin/bash
echo "Enter filename:"
read file
echo "Enter word to search:"
read word
if [ -f "$file" ]
then
count=$(grep -o "$word" "$file" | wc -l)
echo "Word '$word' occurs $count times in $file"
else
echo "File not found!"
fi
Q24. Display only even-numbered lines of a file
#!/bin/bash
echo "Enter filename:"
read file
if [ -f "$file" ]
then
awk 'NR%2==0' "$file"
else
echo "File not found!"
fi

Q25. Merge two files into one


#!/bin/bash
echo "Enter first filename:"
read f1
echo "Enter second filename:"
read f2
echo "Enter output filename:"
read f3
cat "$f1" "$f2" > "$f3"
echo "Files merged into $f3"

Q26. Print numbers from 1 to n using while loop


#!/bin/bash
echo "Enter n:"
read n
i=1
while [ $i -le $n ]
do
echo -n "$i "
i=$((i+1))
done
echo

Q27. Print numbers from n to 1 using until loop


#!/bin/bash
echo "Enter n:"
read n
i=$n
until [ $i -lt 1 ]
do
echo -n "$i "
i=$((i-1))
done
echo
Q28. Sum of n natural numbers
#!/bin/bash
echo "Enter n:"
read n
sum=0
for ((i=1;i<=n;i++))
do
sum=$((sum+i))
done
echo "Sum of first $n natural numbers = $sum"

Q29. Check Armstrong number


#!/bin/bash
echo "Enter a number:"
read n
num=$n
sum=0
while [ $n -gt 0 ]
do
digit=$((n%10))
sum=$((sum+digit*digit*digit))
n=$((n/10))
done
if [ $sum -eq $num ]
then
echo "$num is Armstrong"
else
echo "$num is Not Armstrong"
fi

Q30. Armstrong numbers between 1–1000


#!/bin/bash
echo "Armstrong numbers between 1 and 1000:"
for ((num=1;num<=1000;num++))
do
n=$num
sum=0
while [ $n -gt 0 ]
do
digit=$((n%10))
sum=$((sum+digit*digit*digit))
n=$((n/10))
done
if [ $sum -eq $num ]
then
echo -n "$num "
fi
done
echo
Part 4: System / Environment +
Operators (Q31–Q40)

Q31. Display current logged in user


#!/bin/bash
echo "Current logged in user: $USER"

Q32. Display current date and time


#!/bin/bash
echo "Current date and time: $(date)"

Q33. Display system uptime


#!/bin/bash
echo "System uptime:"
uptime

Q34. Display current shell


#!/bin/bash
echo "Current shell: $SHELL"

Q35. Display process ID of the script


#!/bin/bash
echo "Process ID of this script: $$"

Q36. Simple calculator (using case)


#!/bin/bash
echo "Enter two numbers:"
read a b
echo "Choose operation: 1) Add 2) Subtract 3) Multiply 4) Divide"
read ch
case $ch in
1) echo "Result = $((a+b))" ;;
2) echo "Result = $((a-b))" ;;
3) echo "Result = $((a*b))" ;;
4) echo "Result = $((a/b))" ;;
*) echo "Invalid choice" ;;
esac
Q37. Compare two numbers
#!/bin/bash
echo "Enter two numbers:"
read a b
if [ $a -gt $b ]
then
echo "$a is greater"
elif [ $a -lt $b ]
then
echo "$b is greater"
else
echo "Both are equal"
fi

Q38. Demonstrate logical operators (AND / OR)


#!/bin/bash
echo "Enter a number:"
read n
if [ $n -ge 10 ] && [ $n -le 50 ]
then
echo "$n is between 10 and 50"
else
echo "$n is outside range"
fi
#!/bin/bash
echo "Enter a character:"
read ch
if [ $ch = "a" ] || [ $ch = "e" ] || [ $ch = "i" ] || [ $ch = "o" ] || [
$ch = "u" ]
then
echo "$ch is a vowel"
else
echo "$ch is not a vowel"
fi

Q39. Demonstrate shift command


#!/bin/bash
echo "Before shift: $1 $2 $3"
shift
echo "After shift: $1 $2"

Run:

./script.sh 10 20 30

Output:

Before shift: 10 20 30
After shift: 20 30
Q40. Show positional parameters
#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "All arguments: $*"
echo "Number of arguments: $#"

Run:

./script.sh 5 10

Output:

Script name: ./script.sh


First argument: 5
Second argument: 10
All arguments: 5 10
Number of arguments: 2

Part 5: Utility & Advanced Shell


Programs (Q41–Q50)

Q41. Backup a file with timestamp


#!/bin/bash
echo "Enter filename to backup:"
read file
if [ -f "$file" ]
then
cp "$file" "$file-$(date +%Y%m%d%H%M%S).bak"
echo "Backup created successfully."
else
echo "File not found!"
fi

Q42. Delete all .tmp files in current directory


#!/bin/bash
echo "Deleting all .tmp files..."
rm -f *.tmp
echo "All .tmp files deleted."
Q43. Rename all .txt files to .bak
#!/bin/bash
for f in *.txt
do
mv "$f" "${f%.txt}.bak"
done
echo "All .txt files renamed to .bak"

Q44. Display disk usage


#!/bin/bash
echo "Disk usage:"
df -h

Q45. Display top 10 processes


#!/bin/bash
echo "Top 10 processes:"
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head -n 11

Q46. Find GCD of two numbers


#!/bin/bash
echo "Enter two numbers:"
read a b
while [ $a -ne $b ]
do
if [ $a -gt $b ]
then
a=$((a-b))
else
b=$((b-a))
fi
done
echo "GCD = $a"

Q47. Find LCM of two numbers


#!/bin/bash
echo "Enter two numbers:"
read a b
x=$a
y=$b
while [ $a -ne $b ]
do
if [ $a -gt $b ]
then
a=$((a-b))
else
b=$((b-a))
fi
done
gcd=$a
lcm=$(( (x*y)/gcd ))
echo "LCM = $lcm"
Q48. Generate Pascal’s Triangle
#!/bin/bash
echo "Enter number of rows:"
read n
for ((i=0;i<n;i++))
do
for ((j=0;j<=i;j++))
do
if [ $j -eq 0 ] || [ $j -eq $i ]
then
C=1
else
C=$((C*(i-j+1)/j))
fi
echo -n "$C "
done
echo
done

Q49. Menu-driven program


#!/bin/bash
while true
do
echo "Menu:"
echo "1. Calculator"
echo "2. Reverse a number"
echo "3. Palindrome check"
echo "4. Exit"
read choice

case $choice in
1) echo "Enter two numbers:"; read a b
echo "Sum = $((a+b))"
echo "Diff = $((a-b))"
echo "Prod = $((a*b))"
echo "Div = $((a/b))" ;;
2) echo "Enter number:"; read n
rev=0; num=$n
while [ $n -gt 0 ]
do
d=$((n%10))
rev=$((rev*10+d))
n=$((n/10))
done
echo "Reverse = $rev" ;;
3) echo "Enter string:"; read str
rev=$(echo $str | rev)
if [ "$str" = "$rev" ]; then
echo "Palindrome"
else
echo "Not Palindrome"
fi ;;
4) exit ;;
*) echo "Invalid choice" ;;
esac
done
Q50. Student Marksheet (average, pass/fail)
#!/bin/bash
echo "Enter marks of 5 subjects:"
read m1 m2 m3 m4 m5
total=$((m1+m2+m3+m4+m5))
avg=$((total/5))
echo "Total = $total"
echo "Average = $avg"

if [ $avg -ge 40 ]
then
echo "Result: Pass"
else
echo "Result: Fail"
fi

You might also like