Upload files to "OS/bash"

This commit is contained in:
Aadit Agrawal 2025-01-24 10:04:28 +05:30
parent 4764107577
commit 875658414f
5 changed files with 105 additions and 0 deletions

7
OS/bash/q1.sh Normal file
View File

@ -0,0 +1,7 @@
#!/bin/sh
echo "Entered file will be copied"
cp "$1" "duplicate_$1"
echo "File duplicated"

10
OS/bash/q2.sh Normal file
View File

@ -0,0 +1,10 @@
#!/bin/sh
echo "Deleteing the following files"
for file in "$@";do
echo "$file"
rm -i "$file"
done
echo "All files deleted successfully"

26
OS/bash/q3.sh Normal file
View File

@ -0,0 +1,26 @@
#!/bin/bash
selection_sort()
{
local arr=("$@") # array declaration
local n=${#arr[@]}
for ((i=0; i<n-1; i++)); do
min=$i
for ((j=i+1; j<n; j++)); do
if [[ ${arr[j]} < ${arr[min]} ]]; then
min=$j
fi
done
temp="${arr[min]}"
arr[min]="${arr[i]}"
arr[i]="$temp"
done
for element in "${arr[@]}"; do
echo "$element"
done
}
selection_sort "$@"

24
OS/bash/q4.sh Normal file
View File

@ -0,0 +1,24 @@
#!/bin/bash
opt="$1"
file="$2"
case "$opt" in
-linecount)
result=$(wc -l < "$file")
echo "Line count in $file: $result"
;;
-wordcount)
result=$(wc -w < "$file")
echo "Word count in $file: $result"
;;
-charcount)
result=$(wc -c < "$file")
echo "Character count in $file: $result"
;;
*)
echo "Invalid option. Please use -linecount, -wordcount, or -charcount."
exit 1
;;
esac

38
OS/bash/q5.sh Normal file
View File

@ -0,0 +1,38 @@
#!/bin/bash
num_patterns=$(( $# ))
patterns=("${@:1:num_patterns}")
input_file="${!num_patterns}"
while true;do
echo "a. Search the patterns in the given input file."
echo "b. Delete all occurrences of the pattern in the given input file."
echo "c. Exit"
read choice
case "$choice" in
a)
echo "Searching patterns in $input_file:"
for pattern in "${patterns[@]}"; do
grep "$pattern" "$input_file"
done
;;
b)
echo "Deleting occurrences of patterns in $input_file:"
for pattern in "${patterns[@]}"; do
sed -i "s/$pattern//g" "$input_file"
done
echo "Occurrences deleted."
;;
c)
echo "Exiting..."
exit 0
;;
*)
echo "Invalid choice. Please enter 'a', 'b', or 'c'."
;;
esac
done