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
169 changes: 124 additions & 45 deletions GRPA/Week3.md
Original file line number Diff line number Diff line change
@@ -1,63 +1,142 @@
# GRPA 1
Add the string “EOF alpha” at the end of the file(starting at a new line) alpha.txt then append the contents of the file numbers.txt at the end of the file(starting at a new line) alpha.txt. alpha.txt and numbers.txt are located in the current working directory.
## Solution
```shell
script() {
echo "EOF alpha" >> alpha.txt; cat numbers.txt >> alpha.txt

## **GrPA 1**
### **Question:**
Print the number of lines present in `file1` and `file2` combined, your solution should not print anything else.
`file1` and `file2` are located in the current working directory.

**Hint:** Multiple files can be given as arguments to the `cat` command.

### **Solution:**
```bash
script() {
cat file1 file2 | wc -l
}
```
# GRPA 2
Print the number of lines present in ‘file1’ and ‘file2’ combined, your solution should not print anything else. ‘file1’ and ‘file2’ are located in the current working directory.</br>
Hint: Multiple files can be given as argument to ‘cat’ command.
## Solution
```shell
script() {
cat file1 file2 | wc -l

# Solution 2
# cat file2 >> file1; wc -l file1
----------

## **GrPA 2**

# Solution 3
# cat file2 >> file1; cat file1 | wc -l
### **Question:**

There are three files `master.txt`, `half1.txt`, and `half2.txt` in the current working directory.
Add the first 2 lines of `half1.txt` to the file `master.txt` at the end (starting at a new line),
then append the last 3 lines of the file `half2.txt` to the file `master.txt` at the end (starting at a new line).
Append the lines in the sequence mentioned.

### **Solution:**

```bash
script() {
head half1.txt -n2 >> master.txt
tail half2.txt -n3 >> master.txt
}
```
# GRPA 3
There are three files master.txt, half1.txt and half2.txt in the current working directory. Add first 2 lines of half1.txt to the file master.txt at the end(starting at a new line) then append the last 3 lines of the file half2.txt to the file master.txt at the end(starting at a new line). Append the lines in the sequence mentioned.
## Solution
```shell

----------

## **GrPA 3**

### **Question:**

Print to the output containing the name of the shell being used, its PID, and the flags
in the following format (no spaces in the string):

```
Shell:<shell>|PID:<pid>|Flags:<flags>

```

### **Solution:**

```bash
script() {
head half1.txt -n2 >> master.txt
tail half2.txt -n3 >> master.txt
echo "Shell:$(readlink -f /proc/$$/exe)|PID:$$|Flags:$-"
}
```
# GRPA 4
An observer wrote a script named createTwingle that produces a file twingle containing names of all the visible stars present in the sky at that instant. Every line in the file twingle is the name of a star. In your current directory the file twingle may or may not be present.
If the file twingle is present in the directory then print the number of lines in the file, else execute the command createTwingle it will create the file twingle in the current working directory then print the number of lines in the file twingle.</br>
Hint: Try to use operators discussed in the lectures to give a single line solution for the task.</br>
Note: stderr will not be displayed
## Solution
```shell

----------

## **GrPA 4**

### **Question:**

An observer wrote a script named `createTwingle` that produces a file `twingle`
containing names of all visible stars in the sky at that instant.
Every line in the file `twingle` is the name of a star.

In your current directory, the file `twingle` may or may not be present.
If the file `twingle` is present in the directory, print the number of lines in the file.
Else, execute the command `createTwingle` (it will create the file `twingle`),
then print the number of lines in the file `twingle`.

**Hint:** Try to use operators discussed in the lectures to give a single-line solution for the task.
**Note:** `stderr` will not be displayed.

### **Solution:**

```bash
script() {
wc -l twingle || (createTwingle && wc -l twingle)
wc -l twingle || (createTwingle && wc -l twingle)
}
```
# GRPA 5
Print the number of directories in the current working directory. Do not print anything else.</br>
Hint: One solution is to make use of 'ls', 'wc' and pipes('|').
## Solution
```shell

----------

## **GrPA 5**

### **Question:**

Print the number of directories in the current working directory.
Do not print anything else.

**Hint:** One solution is to make use of `ls`, `wc`, and pipes (`|`).

### **Solution:**

```bash
script() {
ls -d */ | wc -l
ls -d */ | wc -l
}
```
# GRPA 6
The script test will print some text to the standard output, it can be run similar to any other command and does not accept any arguments.</br>
Your task is to print the output after running test on the screen and also append the output at the end(starting at new line) of the file log. File log is located in the current working directory.</br>
Hint: To solve it in one line check the man page of tee command for appending to the file.
## Solution
```shell

----------

## **GrPA 6**

### **Question:**

The script `test` will print some text to the standard output.
It can be run similar to any other command and does not accept any arguments.

Your task is to print the output after running `test` on the screen
**and also append the output at the end (starting at new line) of the file `log`.**
The file `log` is located in the current working directory.

**Hint:** To solve it in one line, check the `man` page of the `tee` command for appending to a file.

### **Solution:**

```bash
script() {
test | tee templog
cat templog >> log
test | tee -a log
}
```

----------

## **GrPA 7**

### **Question:**

Add the string `EOF alpha` at the end of the file (starting at a new line) `alpha.txt`
then append the contents of the file `numbers.txt` at the end of the file (starting at a new line) `alpha.txt`.
Both `alpha.txt` and `numbers.txt` are located in the current working directory.

### **Solution:**

```bash
script() {
{ echo "EOF alpha"; cat numbers.txt; } >> alpha.txt
}
```
67 changes: 67 additions & 0 deletions GRPA/Week5.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,71 @@
# GRPA 1
Write a command that runs in a child shell, prints "hello" and exits with the exit code 179.
## Solution
```shell
script() {
(echo hello; exit 179)
}
```
# GRPA 2
The file `Pincode_info.csv` has information on the pin codes of some places. A sample output of the command `head -5 Pincode_info.csv` is given below. The first line of this file gives the information about the sequence of fields in each line of the file following it.
```
Circle Name,Region Name,Division Name,Office Name,Pincode,OfficeType,Delivery,Division Code
Andhra Pradesh Circle,Kurnool Region,Anantapur Division,A Narayanapuram B.O,515009,BO,Delivery,01
Andhra Pradesh Circle,Kurnool Region,Anantapur Division,Akuledu B.O,515731,BO,Delivery,01
Andhra Pradesh Circle,Kurnool Region,Anantapur Division,Allampuram B.O,515766,BO,Delivery,01
```

Assume that there are only 10 states for which this system works and the first digit of the pin code is unique for each state. That means for all the places in the entire state the first digit will be the same. You are given a small variable named `state` that contains a state name (Example: `state="Punjab"`). Display the number of pin codes available in the file `Pincode_info.csv` within the state given in the variable `state` that has the same first and last digit. For e.g., if the value of `state = "Andhra Pradesh"`, one such pin code is `515005` (for the file given above).

**Hint:** First find the first digit that represents the given state.

## Solution
```shell
script() {
awk -F, -v st="$state" '
NR==1 {next}
$9==st {digit=substr($5,1,1)}
$9==st && substr($5,1,1)==substr($5,6,1) {cnt++}
END {print cnt+0}
' Pincode_info.csv
}
```

# GRPA 3
In a course, the instructor asked the students to submit their projects in a single file named as the student’s roll number. A typical roll number of a student is a 10 character string which is a combination of a four digit(decimal) year and six character hexadecimal number, e.g. "20201f3acd". The instructor specified that the name of the file should be in lower case but some students mistakenly used uppercase for their file names. Each file name is either entirely in lower case or entirely in upper case with numbers.

Your task is to create two arrays(shell variables) named lower and upper. Array lower should not contain the file names that have upper case letters and array upper should contain all the file names that have upper case letters.

Note: The project files are located in the current directory

Hint:

arr=(`ls`) # Each element in arr corresponds to the output from the ls
## Solution
```shell
script() {

files=(*)

# Initialize empty arrays
lower=()
upper=()

# Iterate through all files
for file in "${files[@]}"; do
# Check if file contains any uppercase letters (A-F for hex part)
if [[ "$file" =~ [A-F] ]]; then
upper+=("$file")
else
lower+=("$file")
fi
done
}
```

---
# Older GrPA Questions
# GRPA 1
Write a bash script which takes one argument as the name of a file and prints Yes if the file has read permission only for the owner and no other permissions for owner or other users, else do not print anything. The file given in the argument will be present in the current working directory.
## Solution
```shell
Expand Down
92 changes: 92 additions & 0 deletions Graded Assignments/Week 3 Graded Assignment.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,95 @@ Which of the following is the correct syntax to store the output of the command
(3) ``` dirs=`ls` ```

(4) ``` dirs=$(ls) ```

## Additional Questions

### **Question 1:**
**What does the following command do?**

```bash
tr 'foo' 'bar' < file.txt > temp && mv temp file.txt

```

#### **Options:**

- A) Replaces all occurrences of the string "foo" with "bar" in file.txt

- B) Replaces each occurrence of the character `f` with `b`, and each `o` with `a` or `r` in file.txt

- C) Reads the contents of file.txt, modifies it, and overwrites file.txt with the result

- D) Uses tr to translate entire words from "foo" to "bar"


#### ✅ **Final Answer:**

**B) Replaces each occurrence of the character `f` with `b`, and each `o` with `a` or `r` in file.txt**

----------

### **Question 2:**

**Which of the following commands provide a visual representation or tree structure showing the parent-child relationship between processes in Linux?**

#### **Options:**

- A) `pstree`

- B) `ps -ejH`

- C) `ps --forest`

- D) `ps auxf`


#### ✅ **Final Answer:**

All of the above:
**A, B, C, and D** — All show process hierarchies in a tree or visual format.

----------

### **Question 3:**

**On running the following command in the bash shell:**

```bash
echo "Random Text" | tee file1 | tee file2

```

**"Random Text" will be redirected to?**

#### **Options:**

- A) The file `file1` only

- B) The file `file1`, and the terminal

- C) The file `file1`, file `file2` and the terminal

- D) The file `file1` and the terminal


#### ✅ **Final Answer:**

**C) The file `file1`, file `file2` and the terminal**

----------

### **Question 4:**

**How many directories will be created by the command:**

```bash
mkdir dir-{0..9} && mkdir dir-100 || mkdir dir-101

```

#### ✅ **Final Answer:**

**11 directories** — `dir-0` to `dir-9` and `dir-100` (assuming none of them already exist).

----------
Loading