289 lines
4.0 KiB
Markdown
289 lines
4.0 KiB
Markdown
# Operating Systems Lab — Module 1 Answer Key
|
|
|
|
> **For Teaching Assistants only. Do not share with students.**
|
|
|
|
---
|
|
|
|
### 1. Navigation
|
|
|
|
List all files in `config/`, including hidden ones:
|
|
|
|
```bash
|
|
cd sisop_module1_playground
|
|
ls -la config/
|
|
```
|
|
|
|
---
|
|
|
|
### 2. Permissions
|
|
|
|
Change `archive/locked.txt` so only the owner can read it:
|
|
|
|
```bash
|
|
chmod 400 archive/locked.txt
|
|
```
|
|
|
|
Verify:
|
|
|
|
```bash
|
|
ls -l archive/locked.txt
|
|
# Expected: -r-------- 1 <user> ...
|
|
```
|
|
|
|
---
|
|
|
|
### 3. Search
|
|
|
|
Find the `"ERROR"` line in `logs/app.log` and save it:
|
|
|
|
```bash
|
|
grep "ERROR" logs/app.log > logs/error_only.txt
|
|
```
|
|
|
|
---
|
|
|
|
### 4. Count
|
|
|
|
Count files ending in `.tmp` in `data/`:
|
|
|
|
```bash
|
|
find data/ -name "*.tmp" | wc -l
|
|
```
|
|
|
|
---
|
|
|
|
### 5. Chaining
|
|
|
|
List all files in `data/` sorted in reverse alphabetical order:
|
|
|
|
```bash
|
|
ls data/ | sort -r
|
|
```
|
|
|
|
---
|
|
|
|
### 6. I/O Redirection
|
|
|
|
Create `info.txt` with the current date and username:
|
|
|
|
```bash
|
|
echo "$(date) $(whoami)" > info.txt
|
|
```
|
|
|
|
---
|
|
|
|
### 7. AWK — Print names (Column 2)
|
|
|
|
```bash
|
|
awk -F',' 'NR>1 {print $2}' data/users.csv
|
|
```
|
|
|
|
**Expected output:**
|
|
```
|
|
Budi
|
|
Siti
|
|
Agus
|
|
Dewi
|
|
Eko
|
|
Sari
|
|
```
|
|
|
|
---
|
|
|
|
### 8. AWK — Total sum of salaries (Column 4)
|
|
|
|
```bash
|
|
awk -F',' 'NR>1 {sum += $4} END {print sum}' data/users.csv
|
|
```
|
|
|
|
**Expected output:** `26200`
|
|
|
|
---
|
|
|
|
### 9. AWK — Name and Role of Active users
|
|
|
|
```bash
|
|
awk -F',' 'NR>1 && $5=="Active" {print $2, $3}' data/users.csv
|
|
```
|
|
|
|
**Expected output:**
|
|
```
|
|
Budi Admin
|
|
Agus User
|
|
Dewi Manager
|
|
Sari Admin
|
|
```
|
|
|
|
---
|
|
|
|
### 10. Bash — `scripts/check_dir.sh`
|
|
|
|
```bash
|
|
#!/bin/bash
|
|
if [ -d "$1" ]; then
|
|
echo "Directory '$1' exists."
|
|
else
|
|
echo "Directory '$1' does not exist."
|
|
fi
|
|
```
|
|
|
|
Make it executable and test:
|
|
|
|
```bash
|
|
chmod +x scripts/check_dir.sh
|
|
./scripts/check_dir.sh data/
|
|
./scripts/check_dir.sh fakedir/
|
|
```
|
|
|
|
---
|
|
|
|
### 11. Bash — Fix `scripts/hello.sh`
|
|
|
|
**Bug:** `date_now=date` assigns the string `"date"` instead of executing the command.
|
|
|
|
**Fix:** Change line 3 to use command substitution:
|
|
|
|
```bash
|
|
date_now=$(date)
|
|
```
|
|
|
|
**Corrected `hello.sh`:**
|
|
|
|
```bash
|
|
#!/bin/bash
|
|
echo "Hello eggboi"
|
|
date_now=$(date)
|
|
echo "Today is $date_now"
|
|
```
|
|
|
|
---
|
|
|
|
### 12. Bash — Loop to create 5 files
|
|
|
|
```bash
|
|
#!/bin/bash
|
|
for i in $(seq 1 5); do
|
|
touch test_${i}.txt
|
|
done
|
|
```
|
|
|
|
---
|
|
|
|
### 13. Cron — Run `hello.sh` every day at 3:30 AM
|
|
|
|
```
|
|
30 3 * * * /path/to/sisop_module1_playground/scripts/hello.sh
|
|
```
|
|
|
|
---
|
|
|
|
### 14. Cron — List all active cron jobs
|
|
|
|
```bash
|
|
crontab -l
|
|
```
|
|
|
|
---
|
|
|
|
### 15. Wildcards — Delete all `.bak` files in `data/`
|
|
|
|
```bash
|
|
rm data/*.bak
|
|
```
|
|
|
|
---
|
|
|
|
### 16. Disk Usage — Total size of the lab directory
|
|
|
|
```bash
|
|
du -sh sisop_module1_playground/
|
|
```
|
|
|
|
---
|
|
|
|
### 17. AWK — Name of the person with the highest salary
|
|
|
|
```bash
|
|
awk -F',' 'NR>1 {if ($4 > max) {max=$4; name=$2}} END {print name}' data/users.csv
|
|
```
|
|
|
|
**Expected output:** `Dewi` (salary: 7000)
|
|
|
|
---
|
|
|
|
### 18. Sed — Replace `"INFO"` with `"LOG"`
|
|
|
|
```bash
|
|
sed 's/INFO/LOG/g' logs/app.log > logs/app_log_replaced.log
|
|
```
|
|
|
|
---
|
|
|
|
### 19. Processes — Find PID of the current bash session
|
|
|
|
```bash
|
|
echo $$
|
|
```
|
|
|
|
Or alternatively:
|
|
|
|
```bash
|
|
ps -p $$
|
|
```
|
|
|
|
---
|
|
|
|
### 20. Archiving — Create a `.tar.gz` of `logs/`
|
|
|
|
```bash
|
|
tar -czvf logs.tar.gz logs/
|
|
```
|
|
|
|
---
|
|
|
|
### 21. Bash — Update, upgrade, and download image
|
|
|
|
```bash
|
|
#!/bin/bash
|
|
|
|
# Update and upgrade the system
|
|
sudo apt update && sudo apt upgrade -y
|
|
|
|
# Create the target directory if it doesn't exist
|
|
mkdir -p new_image
|
|
|
|
# Download an image from a URL into new_image/
|
|
# Replace <URL> with the actual image URL
|
|
wget -P new_image/ <URL>
|
|
|
|
#example:
|
|
wget -P new_image/ https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/2010-kodiak-bear-1.jpg/1280px-2010-kodiak-bear-1.jpg
|
|
|
|
# Alternative using curl:
|
|
# curl -o new_image/image.jpg <URL>
|
|
```
|
|
|
|
---
|
|
|
|
### 22. Search — Find `secret.txt` in `maze/`
|
|
|
|
```bash
|
|
find maze/ -name "secret.txt"
|
|
```
|
|
|
|
**Expected output:**
|
|
```
|
|
maze/level1_b/level2_d/level3_d/level4_a/secret.txt
|
|
```
|
|
|
|
Display its contents:
|
|
|
|
```bash
|
|
cat maze/level1_b/level2_d/level3_d/level4_a/secret.txt
|
|
```
|
|
|
|
**Expected output:**
|
|
```
|
|
You found it! The secret message is: sisop2026
|
|
```
|