Upload files to "OS/bash/Week4"
Signed-off-by: Aadit Agrawal <tech@aaditagrawal.com>
This commit is contained in:
parent
3d5dfd6481
commit
5b0eda8b4e
5 changed files with 108 additions and 0 deletions
10
OS/bash/Week4/q2.sh
Normal file
10
OS/bash/Week4/q2.sh
Normal 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/Week4/q3.sh
Normal file
26
OS/bash/Week4/q3.sh
Normal 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/Week4/q4.sh
Normal file
24
OS/bash/Week4/q4.sh
Normal 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/Week4/q5.sh
Normal file
38
OS/bash/Week4/q5.sh
Normal 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
|
10
OS/bash/Week4/solutions.md
Normal file
10
OS/bash/Week4/solutions.md
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
## Lab 4
|
||||||
|
|
||||||
|
### Lab Exercises
|
||||||
|
Q1. `q1.sh` - Duplication - `cp` of `$1` arg (first argument).
|
||||||
|
Q2. `q2.sh` - Deletion - `rm` of all `$@` files using `for` loop.
|
||||||
|
Q3. `q3.sh` - String Sort - Selection sort using loops and `if` and `echo`.
|
||||||
|
Q4. `q4.sh` - File TypeCounter - Word/Line/CharCount of file via switch `case` statement and passing various commands.
|
||||||
|
Q5. `q5.sh` - Pattern Recognition - `grep` for finding the pattern, `sed` for deleting the pattern, and `exit` for killing the pattern.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue