Supports case-insensitive search, line numbers, inverted match, and robust error handling.
- Case-insensitive search (default)
- Show line numbers for matches (
-n) - Invert match to show non-matching lines (
-v) - Combinable options: e.g.,
-vn,-nv - Help flag:
--helpor-hfor usage info - Graceful error handling for missing arguments or files
./mygrep.sh [-n] [-v] <search_string> <file>-nShow line numbers for each match-vInvert the match (print non-matching lines)--helpor-hDisplay help message
Given a file testfile.txt:
Hello world
This is a test
another test line
HELLO AGAIN
Don't match this line
Testing one two three
1. Basic search:
$ ./mygrep.sh hello testfile.txt
Hello world
HELLO AGAIN2. With line numbers:
$ ./mygrep.sh -n hello testfile.txt
1:Hello world
4:HELLO AGAIN3. Invert match with line numbers:
$ ./mygrep.sh -vn hello testfile.txt
2:This is a test
3:another test line
5:Don't match this line
6:Testing one two three4. Missing search string:
$ ./mygrep.sh -v testfile.txt
Error: Missing search string or file.
Usage: ./mygrep.sh [-n] [-v] <search_string> <file>
Options:
-n Show line numbers for each match
-v Invert the match (print non-matching lines)
--help Display this help message5. File not found:
$ ./mygrep.sh -vn hello testfil.txt
Error: File 'testfil.txt' not found.
Usage: ./mygrep.sh [-n] [-v] <search_string> <file>
Options:
-n Show line numbers for each match
-v Invert the match (print non-matching lines)
--help Display this help message6. Keyword not found:
$ ./mygrep.sh -vn helloo testfile.txt
Keyword not Found in file
Usage: ./mygrep.sh [-n] [-v] <search_string> <file>
Options:
-n Show line numbers for each match
-v Invert the match (print non-matching lines)
--help Display this help message7. Help message:
$ ./mygrep.sh --help
Usage: ./mygrep.sh [-n] [-v] <search_string> <file>
Options:
-n Show line numbers for each match
-v Invert the match (print non-matching lines)
--help Display this help message- Option Parsing:
Usesgetoptsto process-n,-v, and-h(help). Sets internal flags (show_line_numbers,invert_match) accordingly. - Argument Validation:
After parsing options, checks if both a search string and filename are provided. If not, prints an error and usage. - File Check:
Verifies the file exists before running the search. - Command Building:
Dynamically builds thegrepcommand with the appropriate options (-i,-n,-v) based on user input. - No Matches Handling:
If no matches are found, prints "Keyword not Found in file" instead of just failing silently.
To support regex or additional options like -c (count), the script would:
- Extend
getoptsto recognize new flags. - Add more internal flags (e.g.,
count_matches). - Adjust the dynamic
grepcommand to include new options. - Possibly use a
caseblock after parsing to organize how different combinations of options affect behavior. - This modular approach would make the script scalable for more features.
The most challenging aspect was handling option combinations (like -vn or -nv) and building the dynamic grep command correctly.
Ensuring robust error handling for invalid or missing arguments, so the script never crashes or behaves unexpectedly, also required careful logic and validation.
-
Check what DNS servers are configured:
cat /etc/resolv.conf
-
Try resolving using system DNS:
dig internal.example.com
-
Try resolving with a public DNS (e.g., Google):
dig @8.8.8.8 internal.example.com
- If resolution works with 8.8.8.8 but not with system DNS, then internal DNS might be broken.
- If it fails on both, maybe it's not a DNS issue or maybe the dashboard is only internal and not visible to public DNS like 8.8.8.8.
- Check if the website is reachable using curl to test HTTP(S) responses:
curl -v http://internal.example.com curl -v https://internal.example.com
-
DNS:
- Incorrect DNS entry for internal.example.com
- DNS server misconfigured or down
- Firewall blocking DNS requests
- Caching issues
-
Network:
- Firewall blocking traffic to and from the server
- Routing issue
- NAT misconfigurations
-
Web Server:
- Web server up but bound to wrong IP/interface
- TLS misconfiguration
-
Incorrect DNS Entry or Misconfigured DNS Server:
- If
dig @internal-dns internal.example.comreturns nothing or wrong IP, SSH into DNS server and fix the zone entry, then restart the service:
sudo nano /etc/bind/zones/internal.example.com.zone sudo systemctl restart named
- If
-
Firewall Blocks DNS or Web Traffic:
- Check firewall rules on client and server:
sudo iptables -L -n
- Allow traffic on ports 80 and 443:
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
-
Configure /etc/hosts for Quick Testing
- Edit
/etc/hoststo bypass DNS:
sudo nano /etc/hosts # Add line <resolved_ip> internal.example.com
- Test with:
curl -v http://internal.example.com curl -v https://internal.example.com
- Edit
-
Make DNS Settings Persistent
- Using systemd-resolved:
sudo mkdir -p /etc/systemd/resolved.conf.d sudo nano /etc/systemd/resolved.conf.d/dns_servers.conf # Add: [Resolve] DNS=10.0.0.2 Domains=example.com # Restart service sudo systemctl restart systemd-resolved
-
DNS Resolution Verification
-
Checked configured nameserver in
/etc/resolv.conf:nameserver 10.255.255.254
-
Tested DNS resolution using
dig:dig internal.example.com
Result: NXDOMAIN — no answer returned.
-
Cross-checked with public DNS (
8.8.8.8):dig @8.8.8.8 internal.example.com
Result: NXDOMAIN.
-
Verified using
nslookup:nslookup internal.example.com
Result: server can't find internal.example.com: NXDOMAIN.
-
-
Service Reachability Check
- Could not test service reachability (ports 80/443) as domain resolution failed.
- DNS server (
10.255.255.254) does not resolveinternal.example.com. - Public DNS (
8.8.8.8) also does not recognize it. - Issue is isolated to DNS resolution failure.
| Layer | Possible Cause |
|---|---|
| DNS Layer | Missing DNS record for internal.example.com |
| DNS Layer | Incorrect DNS server configuration on clients |
| DNS Layer | Internal DNS server outage or misconfiguration |
| DNS Layer | Firewall blocking DNS queries to the internal DNS server |
| Application Layer | (Less likely) Service moved to a different domain without DNS update |
| Step | Action | Command Example |
|---|---|---|
| 1 | Confirm correct internal DNS server IP address | (Contact network team) |
| 2 | Temporarily add static IP in /etc/hosts if IP address is known |
sudo nano /etc/hosts |
| 3 | Update /etc/resolv.conf with the correct DNS server (temporary test) |
sudo nano /etc/resolv.conf |
| 4 | Persist DNS settings with systemd-resolved or NetworkManager if needed | sudo resolvectl dns eth0 <correct_dns_server> |
- Missing correct IP address for
internal.example.com. - Need either:
- Internal DNS server IP that knows the record, or
- Direct IP address of
internal.example.com.
Status: Issue isolated to DNS resolution.
Waiting for: IP address or internal DNS server information.
sudo nano /etc/hosts
# Add the line:
192.168.x.x internal.example.comsudo nano /etc/resolv.conf
# Replace with correct nameserver
nameserver <correct_internal_dns>sudo resolvectl dns eth0 <correct_internal_dns>
sudo resolvectl domain eth0 example.com