Article 4Y9EM Unable to run my logic here, can someone please help?

Unable to run my logic here, can someone please help?

by
blason
from LinuxQuestions.org on (#4Y9EM)
Hi Folks,

Here I want to present STDINput to the user so that they can enter the IP addresses.
  1. If $? -eq 0 then only below should process else exit 1
  2. Then I do not want them to enter more than 1000 IPV4 IP addresses.
  3. if the count goes above 1000 it should not accept and should display the message.
  4. Or only first 1000 entries should be picked up.
Here is my script but dang that one is not working
Code:#!/bin/bash
_WORKDIR="/opt/.custombl"
DASHDATE=`date +%Y-%m-%d`
_PRE_CUSTOM_BL="$_WORKDIR/pre-customBL"
_CUSTOM_BL="$_WORKDIR/customBL"
GREENS="\033[1;32m"
GREENE="\033[0m"
QUIETNS="-qq -y"
REDS="\e[91m"
REDE="\e[0m"
DIGIT=2000

clear
#Checking DIR Structure if not then create it else go there
if [[ -d $_WORKDIR ]];then
echo "*********************************************"
echo -e $GREENS"Add only IPv4 IP Address one per line"$GREENE
echo -e $GREENS"Press CTRL+D once done"$GREENE
echo -e $GREENS"Press CTRL+C If you want to cancel"$GREENE
echo "*********************************************"
echo ""
cat >> $_PRE_CUSTOM_BL
if [ $? -eq 0 ];then
COUNT=`cat $_PRE_CUSTOM_BL | wc -l`
if [ $COUNT -le $DIGIT ];then
cat $_PRE_CUSTOM_BL | head -$DIGIT | grep -v -e '^$' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | sort | uniq > $_CUSTOM_BL
fi
else
echo -e $REDS"You have exceeded IP Block List Limit"$REDE
echo -e $REDS"Could not Add IP addresses"$REDE
echo -e $REDS"Error encountered..."$REDE
exit 1
fi
else
mkdir -p $_WORKDIR
echo -e $REDS"Directory Structure is prepared"$REDE
echo -e $REDS"Re-run the script again"$REDE
exit 0
fiNow the issues I am facing with are
  1. Since I am using cat >> stdin; this is not command output $? hence unable to compare
  2. CTRL+c create a exit 130 but since this is not being captured unable to stop the user from entering the IP address does get entered.
  3. Lastly; 1000 limit is not getting set as system picks randomly
Can someone please help me on this issue; pls?latest?d=yIl2AUoC8zA latest?i=Wd5h0AZG0EI:fcHblP6UjkM:F7zBnMy latest?i=Wd5h0AZG0EI:fcHblP6UjkM:V_sGLiP latest?d=qj6IDK7rITs latest?i=Wd5h0AZG0EI:fcHblP6UjkM:gIN9vFwWd5h0AZG0EI
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments