What I need
I have an existing script that pulls port information for domains and stores it into a text file called portscan.txt
. Example:
portscan.txt
file:
somedomain.com:80
somedomain.com:443
I want to delete the information only if certain conditions are met. These conditions include:
- The file with the domains should have 2 or less lines
- The ports should only be 80 or 443 (i.e., I don't want to delete the file if 8080, 8443, or any other port exists in the file).
Note: So basically, the example file provided above should be deleted, but I do not want to delete the file if there are 2 lines, but the ports are 8080 or 8443 (or any other port for that matter)
Example like so:
somedomain.com:8443
somedomain.com:443
This should not be deleted.
What I tried
I attempted scripting this out and here's what I have:
#!/bin/bash
lines=$(cat portscan.txt | wc -l)
ports=$(cat portscan.txt | grep -Pv '(^|[^0-9])(80|443)($|[^0-9])')
if [[ $lines < 3 ]] && [[ $ports != 80 ]]; then
if [[ $ports != 443 ]]; then
echo "I need to delete this"
fi
else
echo "I will NOT delete this..."
fi
This is the second rendering of the script, I attempted nested if statements because I was unable to do a condition like this:
IF portscan.txt is less than two lines AND the ports are NOT 80 OR 443
I also attempted this in a much simpler manner like so:
#!/bin/bash
lines=$(cat portscan.txt | wc -l)
ports=$(cat portscan.txt | grep -Pv '(^|[^0-9])(80|443)($|[^0-9])')
if [[ $lines < 3 ]] && (( $ports != 80||443 )); then
echo "I need to delete this"
else
echo "I will NOT delete this..."
fi
I have tried the ((
because I read that this is better to be used with arithmetic functions -- which is what I thought I needed, but I'm not as bash savvy with conditional arguments when it should be like: "This and that or that".
Hopefully this makes sense, any help would be greatly appreciated!