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.
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


Here I want to present STDINput to the user so that they can enter the IP addresses.
- If $? -eq 0 then only below should process else exit 1
- Then I do not want them to enter more than 1000 IPV4 IP addresses.
- if the count goes above 1000 it should not accept and should display the message.
- Or only first 1000 entries should be picked up.
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
- Since I am using cat >> stdin; this is not command output $? hence unable to compare
- 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.
- Lastly; 1000 limit is not getting set as system picks randomly