# 1. Check Positive or Negative echo "Enter number:" read n if [ $n -ge 0 ] then echo "Positive" else echo "Negative" fi # 2. Swap Two Numbers Without Third Variable echo "Enter first number:" read a echo "Enter second number:" read b a=$((a+b)) b=$((a-b)) a=$((a-b)) echo "After swap:" echo "a=$a" echo "b=$b" # 3. Print Odd Numbers Up to N and Find Sum echo "Enter N:" read n sum=0 for ((i=1;i<=n;i+=2)) do echo $i sum=$((sum+i)) done echo "Sum = $sum" # 4. Largest Among Three Numbers 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 # 5. Prime Number Check echo "Enter number:" read n flag=0 for ((i=2;i $file echo "Written successfully" # 19. Append Content to File echo "Enter filename:" read file echo "Enter text to append:" read text echo $text >> $file echo "Appended successfully" # 20. Check Character Type echo "Enter character:" read ch case $ch in [A-Z]) echo "Uppercase" ;; [a-z]) echo "Lowercase" ;; [0-9]) echo "Digit" ;; *) echo "Special Character" ;; esac # 21. Display First 5 Lines of File echo "Enter filename:" read file head -5 $file # 22. Sort Array in Ascending Order arr=(5 2 8 1 3) for ((i=0;i<5;i++)) do for ((j=i+1;j<5;j++)) do if [ ${arr[i]} -gt ${arr[j]} ] then temp=${arr[i]} arr[i]=${arr[j]} arr[j]=$temp fi done done echo "${arr[@]}" # 23. Second Largest Element arr=(10 50 30 40 20) largest=0 second=0 for i in ${arr[@]} do if [ $i -gt $largest ] then second=$largest largest=$i elif [ $i -gt $second ] then second=$i fi done echo "Second Largest = $second" # 24. Search Word in File echo "Enter filename:" read file echo "Enter word:" read word grep "$word" $file # 25. Remove Duplicate Elements arr=(1 2 2 3 4 4 5) echo "${arr[@]}" | tr ' ' '\n' | sort -u # 26. Rename File echo "Enter old filename:" read old echo "Enter new filename:" read new mv $old $new echo "File renamed" # 27. Frequency of Elements in Array arr=(1 2 2 3 1 4 2) for i in ${arr[@]} do count=0 for j in ${arr[@]} do if [ $i -eq $j ] then count=$((count+1)) fi done echo "$i occurs $count times" done # 28. Length of String Without Built-in Function echo "Enter string:" read str count=0 for ((i=0; ;i++)) do ch=${str:$i:1} if [ -z "$ch" ] then break fi count=$((count+1)) done echo "Length = $count" # 29. Compare Two Files echo "Enter first file:" read f1 echo "Enter second file:" read f2 diff $f1 $f2 # 30. Count Lines Containing Specific Word echo "Enter filename:" read file echo "Enter word:" read word count=$(grep -c "$word" $file) echo "Lines containing word = $count"