Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions practice_5.1/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Week 5 Problem 1

A school teaches from first to twelfth standard, each standard has 5 sections named as ` A ` to ` E `.
A school has grades from `1` to `12`, each with 5 sections `A` to `E`. Classroom numbers are represented in the format – `<grade><section>` (eg. `12E`, `3C`).

The class room number is represented as standard (as number) followed by the section, for example ` 12E `, ` 3C `, etc..

Your task is to create a directory for every classroom in directory ` ~/se2001/practice_5.1 `.
**Task** - Create a directory for every classroom in directory `~/se2001/practice_5.1`.
14 changes: 11 additions & 3 deletions practice_5.10/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# Week 5 Problem 10

Write a bash script ` script.sh ` to redirect the **manual** entry (.i.e. output of man page) for the command which is specified as value of the shell variable named ` cmd ` to a text file ` man_commandValue.txt `, where ` commandValue ` is the value in variable ` cmd ` (example: if the variable ` cmd ` has value ` ls `, then redirect the out of `man ls` to the file named ` man_ls.txt ` ).
The shell variable `cmd` holds the command name.

If there is an error in getting the man page of command in variable ` cmd `, then redirect the standard error to ` man_commandValue.err ` and execute the help command for the command in variable ` cmd ` and redirect the output to the file ` man_commandValue.txt `, where ` commandValue ` is the value in variable ` cmd `.
**Task** -

Hint: use operator && or || for executing help command based on exit status of first command.
Write a bash script `script.sh` to redirect the **manual** entry (i.e. output of man page) of the command in `cmd` to a text file `man_commandValue.txt`, where `commandValue` is the value of `cmd`. Example: if `cmd` has value `ls`, then redirect the output of `man ls` to the file `named man_ls.txt`.

If there is an error in getting the man page of the command in `cmd`, then redirect the standard error to `man_commandValue.err`. Additonally, execute the help command instead and redirect the output to the file `man_commandValue.txt`.

**Note** -

- The variable will be made available during evaluation.
- Run `export cmd="ls"` to create the variable for testing before evaluation.

**Hint** - Use operator && or || for executing help command based on exit status of first command.
13 changes: 11 additions & 2 deletions practice_5.11/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Week 5 Problem 11

Write a script ` script.sh ` to redirect the stderr of a command which is specified via variable ` cmd ` to a text file ` error.txt ` and print the stdout only if there is no error.
The shell variable `cmd` holds the command name.

Hint: Store the stdout to some file then based on success or failure of the command print that file.
**Task** -

Write a bash script `script.sh` to redirect the stderr of the command in `cmd` to a text file `error.txt` and print the stdout only if there is no error.

**Note** -

- The variable will be made available during evaluation.
- Run `export cmd="ls"` to create the variable for testing before evaluation.

**Hint** - Store the stdout to some file then based on success or failure of the command print that file.
13 changes: 6 additions & 7 deletions practice_5.12/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# Week 5 Problem 12

You are writing a Bash Script and in one of the task, you have to count the number of occurrences of some word in several files and finally the count should be in the variable ` ct `.
**Task** -

Write a Bash script ` script.sh ` that can be sourced in a loop to count the number of occurrences in all files, supposing that word count is stored in the variable named ` words `.
Write a Bash script `script.sh` that will be sourced in a loop over several files. In each iteration, the word count for that file is stored in `words`. Accumulate the total count in `ct` and print it at the end.

Your script will be used in a loop that will be run for each file.
**Note** -

The value of ` ct ` should be initialized to 0, at the beginning, and the number of words stored in the variable ` words ` is to be added for your command.
- The variable `words` will be made available during evaluation.
- Run `export words=5` to create the variable for testing before evaluation.

Print the value of the variable ` ct ` in the end.

Hint: Write a Bash command in ` script.sh ` that is equivalent to pseudocode "ct=ct+words", where ` ct ` and ` words ` are variables
**Hint** - Use arithmetic expansion to implement `ct = ct + words`.
8 changes: 3 additions & 5 deletions practice_5.2/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Week 5 Problem 2

A school teaches from first to twelfth standard, each standard has 5 sections named as ` A ` to ` E `. The class room number is represented as standard (as number) followed by the section, for example ` 12E `, ` 3C `, etc..
Each classroom has 40 students.
A school has grades from `1` to `12`, each with 5 sections `A` to `E`. Classroom numbers are represented in the format – `<grade><section>` (eg. `12E`, `3C`), and their directoriers exist in `~/se2001/practice_5.2`.

There is a directory for every classroom in ` ~/se2001/practice_5.2 `.

Write a Bash command to create a file for each student, name the files from 1 to 40 in every directory.
**Task** - Each classroom has 40 students and thus requires one file per student. Write a Bash command to create files named 1 to 40 in every classroom directory.

**Note** - Run `synchro init` to create the classroom directories.
9 changes: 4 additions & 5 deletions practice_5.3/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Week 5 problem 3

A school teaches from first to twelfth standard, each standard has 5 sections named as ` A ` to ` E `. The class room number is represented as standard(as number) followed by the section, for example ` 12E `, ` 3C `, etc..
Each classroom has 40 students. There is a directory for every classroom. All the directories are located inside the directory ` ~/se2001/practice_5.3 `. There is a file for every student named from 1 to 40 in every classroom directory.
A school has grades from `1` to `12`, each with 5 sections `A` to `E`. Classroom numbers are represented in the format – `<grade><section>` (eg. `12E`, `3C`), and their directoriers exist in `~/se2001/practice_5.2`. Each classroom has 40 students and thus has one file per student inside each classroom directory.

Suddenly the school experienced 40% drop in first and sixth standard so they reduced the number of students in each class room to 32.
Suddenly the school experienced a 40% drop in first and sixth standard, so they reduced the number of students in each class room to 32.

Your task is to remove the extra files in the respective directories, i.e. named from 33 to 40.
**Task** - Remove the extra files in the respective classroom directories, i.e. files named from 33 to 40.

Note : Run ` synchro init ` to initialize the directory as you did for ` practice_5.2 `.
**Note** - Run `synchro init` to initialize the directory as you did for `practice_5.2`.
19 changes: 11 additions & 8 deletions practice_5.4/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# Week 5 Problem 4

The shell variable ` logfile ` contains the absolute path to some file.
The shell variable `logfile` contains the absolute path to some file.

Write a script ` script.sh ` which performs the following task.
**Task** -

The task is to print two lines, where first line is the filename alone and the second line is the path of the directory in which the file ` logfile ` is located (print the path without the trail slash ` / `).
Write a script `script.sh` to print two lines -

Note:
- Variables are created when you run ` synchro eval `.
- The first line is the filename of `logfile`.
- The second line is the path of the directory in which the file `logfile` is located (print the path without the trailing slash `/`).

Example shell variable: ` logfile=/home/student78/daily.log `
**Note** -

**Output**
- Variables are created when you run `synchro eval`.
- Run `export logfile="/home/student78/daily.log"` to create the variable for testing your script file before evaluation.

```
**Output** -

```bash
daily.log
/home/student78
```
15 changes: 10 additions & 5 deletions practice_5.5/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# Week 5 Problem 5

A shell variable named ` TOTALCOST ` contains a string which is in the format ` XYZ_ABC_PQR `, where ` XYZ `, ` ABC ` and ` PQR ` are three digit numbers. Underscore ` _ ` is used to separate the three digit numbers.
A shell variable `TOTALCOST` contains a string in the format `XYZ_ABC_PQR`, where `XYZ`, `ABC` and `PQR` are three three-digit numbers. An underscore (`_`) is used to separate the three numbers.

Write a bash script ` script.sh ` that replaces all the underscores (` _ `) with commas (` , `) in the variable ` TOTALCOST ` and displays the final string on the screen.
**Task** -

Example: ` TOTALCOST=198_890_128 `
Write a bash script `script.sh` that replaces all underscores (`_`) with commas (`,`) in the variable `TOTALCOST` and displays the final string on the screen.

**Output**
**Note** -

```
- Variables are created when you run `synchro eval`.
- Run `export TOTALCOST=198_890_128` to create a variable for testing your script file before evaluation.

**Output** -

```bash
198,890,128
```
15 changes: 11 additions & 4 deletions practice_5.6/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# Week 5 Problem 6

Write a bash script ` script.sh ` to display only the filename without extension, whose absolute path is stored in a shell variable named ` file `. The extension should not include the dot ` . `.
A shell variable `file` stores the absolute path of a file.

Example: ` file=/home/student56/tmp/artic.jpg `
**Task** -

**Output**
Write a bash script `script.sh` to display only the filename of `file`, without the extension.

```
**Note** -

- Variables are created when you run `synchro eval`.
- Run `export file=/home/student56/tmp/artic.jpg` to create the variable for testing your script file before evaluation.

**Output** -

```bash
artic
```
23 changes: 13 additions & 10 deletions practice_5.7/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# Week 5 Problem 7

The string ` colors ` contains the name of the colors.
Convert it to array of colors.
The shell variable `colors` contains a list of colors as a string.

Your task is to write a bash script ` script.sh ` to remove all vowels present in the array and display them.
**Task** -

Example: ` colors="violet indigo blue green yellow orange red" `
Write a bash script `script.sh` to first convert `colors` from a string to an array and then remove all vowels from each color of this array. Once done, print this array of colors as a string.

Use the snippet below to convert a space separated string to an array.
**Note** -

```bash
arr=($str)
```
- Variables are created when you run `synchro eval`.
- Run `export colors="violet indigo blue green yellow orange red"` to create the variable for testing your script file before evaluation.
- Use `arr=($str)` to convert a space seperated string to an array.

**Output**
**Output** -

```
```bash
# variable example
colors="violet indigo blue green yellow orange red"

# Expected output
vlt ndg bl grn yllw rng rd
```
19 changes: 11 additions & 8 deletions practice_5.8/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# Week 5 Problem 8

The variable ` filename ` has name of file along with the extension.
The variable `filename` has name of file along with the extension.

Find the file extension and print it.
**Task** -

Write your answer in bash script ` script.sh `.
Write a bash script `script.sh` to find the file extension and print it.

Note:
- The variable will be made available during evaluation.
**Note** -

Example,
- If the filename is ` abc.txt `, print ` txt `.
- If the filename is ` script.sh ` print ` sh `.
- Variables are created when you run `synchro eval`.
- Run `export abc.txt` to create the variable for testing your script file before evaluation.

**Output** -

- If the filename is `abc.txt`, print `txt`.
- If the filename is `script.sh` print `sh`.
19 changes: 11 additions & 8 deletions practice_5.9/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# Week 5 Problem 9

The employee ID of a certain company is in the following format: ` DDYYPXXX ` where
The employee ID of a certain company is in the following format: `DDYYPXXX` where,

- ` DD ` stands for department; ex: FN for Finance, SD for Software Development
- ` YY ` stands for work experience, which is a two digit number.
- ` P ` stands for position, which is a letter; e.g. M for Manager, etc.
- ` XXX ` stands for ID number. Which is a three digit number.
- `DD` stands for department; ex: FN for Finance, SD for Software Development
- `YY` stands for work experience, which is a two digit number.
- `P` stands for position, which is a letter; e.g. M for Manager, etc.
- `XXX` stands for ID number. Which is a three digit number.

Given three Employee IDs, extract just the Department and the ID Number of these employees. Redirect all the output to the file named ` output.txt `.
**Task** -

The names of the variables which stores the employee Ids are shell variables with names ` eid1 `, ` eid2 ` and ` eid3 `. Redirect your output in the same sequence as ` sort -n ` order of the shell variable names.
Write a bash script `script.sh` to extract just the Department and the ID Number of the employee IDs stored in the shell variables `eid1`, `eid2` and `eid3`. Redirect all the output to the file named `output.txt`.

Write your code in file ` script.sh `.
**Note** -

- Variables are created when you run `synchro eval`.
- Run `eid1="AS04M423" eid2="FF12C765" eid3="TK08W776"` to create the variables for testing your script file before evaluation.
40 changes: 40 additions & 0 deletions q-bash-5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Question

Write a shell script to simulate elevator dispatching logic for a 6-storey building (floors 0–6, where 0 is the ground floor) with 6 elevators (numbered 1–6).

**Access rules**:

- Even elevators (2, 4, 6) can only access even floors (0, 2, 4, 6)
- Odd elevators (1, 3, 5) can only access odd floors (0, 1, 3, 5)
- Floor 0 (ground floor) is accessible by all elevators

**Dispatching rules**:

- When a person calls from a floor, find all elevators that can access that floor
- Among those, dispatch the one closest to the called floor (by absolute distance)
- If there's a tie in distance, dispatch the one with the smallest elevator number

**Input**:

- `elevators.csv` — two columns: elevator_number, elevator_floor
- A single integer argument: the floor from which an elevator is called

**Output**:

The number of the elevator that should be dispatched

```bash
# sample elevators.csv
1,0
2,0
3,5
4,0
5,1
6,0

# script call
./script.sh 3

# output
3
```
22 changes: 22 additions & 0 deletions q-bash-5/evaluation_file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

test_dir="/opt/se2001/$(basename $(pwd))"

i=0

for dir in $(ls "$test_dir" | grep "test_case" | sort); do
head -6 "$test_dir/$dir/input" > elevators.csv

floor=$(sed -n '7p' "$test_dir/$dir/input")

bash script.sh "$floor" > out.txt

diff out.txt "$test_dir/$dir/output" &>/dev/null

if [[ $? -eq 1 ]]; then
echo "Test case $((i + 1)) failed"
exit 1
fi

((i++))
done
7 changes: 7 additions & 0 deletions q-bash-5/test_case_1/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1,0
2,0
3,5
4,0
5,1
6,0
3
1 change: 1 addition & 0 deletions q-bash-5/test_case_1/output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
7 changes: 7 additions & 0 deletions q-bash-5/test_case_2/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1,3
2,2
3,3
4,6
5,1
6,4
2
1 change: 1 addition & 0 deletions q-bash-5/test_case_2/output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
7 changes: 7 additions & 0 deletions q-bash-5/test_case_3/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1,1
2,1
3,1
4,1
5,1
6,1
0
1 change: 1 addition & 0 deletions q-bash-5/test_case_3/output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
7 changes: 7 additions & 0 deletions q-bash-5/test_case_4/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1,0
2,0
3,2
4,2
5,4
6,4
0
1 change: 1 addition & 0 deletions q-bash-5/test_case_4/output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
7 changes: 7 additions & 0 deletions q-bash-5/test_case_5/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1,5
2,0
3,1
4,0
5,3
6,0
5
1 change: 1 addition & 0 deletions q-bash-5/test_case_5/output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1