1
0 Comments

I have an existing script that I’m trying to modify to delete certain information from a file called portscan.txt. The file contains port information for domains and I want to delete the information only if certain conditions are met. The 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).

I have tried 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 || $ports != 443 )); then
    echo "I need to delete this"
else
    echo "I will NOT delete this..."
fi

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 tried using (( because I read that this is better to be used with arithmetic functions, but I’m not as bash savvy with conditional arguments when it should be like: “This AND that OR that”. Any help would be greatly appreciated!

Askify Moderator Edited question April 16, 2023