LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-22-2020, 12:22 PM   #1
blason
Member
 
Registered: Feb 2016
Posts: 122

Rep: Reputation: Disabled
Unable to run my logic here, can someone please help?


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
fi
Now 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?
 
Old 01-22-2020, 12:45 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by blason View Post
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
fi
Now 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?
As you've been asked before, read the LQ Rules about text-speak and not using it; it's "please" not "pls".

Past that, this doesn't seem to make much sense at all from a usability standpoint. You've been asking about scripts for YEARS now, and know how to get input from a user at the command line. Do you honestly think that a user is going to enter 1,000 IP addresses...BY HAND....ONE AT A TIME...into a script? Would be far better to read an input file containing those addreses, and do what you'd like with each one. And why a limit of 1,000?

If all you're doing is making directories based on a list, then a very simple loop to just run mkdir is all you need...if the directory exists, it won't create another with the same name, and if not, it creates it. Simple. To address your questions:
  1. Since I am using cat >> stdin; this is not command output $? hence unable to compare No, it isn't; see your other threads where you ask about the $? operand and what it does.
  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. Exits to a prompt for me, no issues.
  3. Lastly; 1000 limit is not getting set as system picks randomly No idea what you're talking about here.
 
1 members found this post helpful.
Old 01-22-2020, 07:39 PM   #3
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 442

Rep: Reputation: 196Reputation: 196
If you want to 'catch' (or trap) things like control-c:
Use a trap
 
Old 01-22-2020, 09:26 PM   #4
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
TB0one -

Someone is posting their doubts that means he or she must have already thought about this. Not necessarily 1000 IP addresses need to be entered at once; not necessarily end-user is versed with VIM or nano or Linux to enter the IP address in the file and save. Cumulatively they should not exceed more than 1000 since my device buffer does not allow more than 1000 IPs.

And about my second doubt, I posted my script is even if more than 1000 entries are added in pre-customBL all are being added in customBL. I want as soon as 1000 limit is exceeded in the pre-customBL script should throw alert and should not allow adding further.
 
Old 01-22-2020, 10:24 PM   #5
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 442

Rep: Reputation: 196Reputation: 196
Do it as a loop then that terminates at 1000 entries no matter what, on top of any other termination events.

P.S.:
Since you're reading the IPs from a text file anyway, give your data entry noobs a running instance of nano or vim with line numbers and give them a cheat sheet on how to save the file.

In fact, you could bind save in either to 'control s', since you're teaching them about control d and c, anyway.

Even better:
Put your efforts into writing a solution that takes an input file of any length that chunks it up into batches of thousands.
That way you can keep a really long input file, sort and 'uniq' it, and then feed it in thousands to your hardware.

Last edited by Geist; 01-22-2020 at 11:07 PM.
 
Old 01-23-2020, 08:41 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by blason View Post
TB0one -
Someone is posting their doubts that means he or she must have already thought about this. Not necessarily 1000 IP addresses need to be entered at once; not necessarily end-user is versed with VIM or nano or Linux to enter the IP address in the file and save. Cumulatively they should not exceed more than 1000 since my device buffer does not allow more than 1000 IPs.
Again, what you're saying makes no sense. **NO ONE** is going to sit there and type in 1000 IP addresses into a script, when a **SINGLE MISTAKE** would force them to go back and re-enter everything. And why would an end-user be entering IP addresses to start with? Isn't this an admin task?? And a file can be created with ANY text editor, not just vi or nano....they can create it with MS Word, Notepad, Kate, or any text editor at all. If someone isn't skilled enough to run a simple text editor, they certainly shouldn't be performing admin tasks.

And again, reading a file eliminates the need for ANY buffer, at all.
Quote:
And about my second doubt, I posted my script is even if more than 1000 entries are added in pre-customBL all are being added in customBL. I want as soon as 1000 limit is exceeded in the pre-customBL script should throw alert and should not allow adding further.
Great; so again, put something in that checks. As said, your script and logic make little sense, since it seems that all you need to do is a run a mkdir command, and reading an input file and looping through to do just that is trivial. You have everything you need in order to accomplish this in your several other scripting threads, going back years now.

Past that, your script does no checking, since I can enter ANYTHING into it...it will accept "1.1.1.1" as well as "John Doe", or "1.65,19". Also, the word "doubt" and the word "question" mean different things.
 
1 members found this post helpful.
Old 01-23-2020, 10:14 PM   #7
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 442

Rep: Reputation: 196Reputation: 196
Quote:
Originally Posted by TB0ne View Post
when a **SINGLE MISTAKE** would force them to go back and re-enter everything.
your script does no checking, since I can enter ANYTHING into it...it will accept "1.1.1.1" as well as "John Doe", or "1.65,19".
This should be your main reasons to go for a better solution, one that involves a text editor, or a more robust entry interface.
I also agree with the notion of nobody wanting to input 1000 IP addresses (and yes, after a while the numbers start to dance...), but maybe you actually operate like that, stranger things have happened, but if they are so unskilled that they cannot be taught the usage of simple text editing, well.
Don't give them a shell where they can run scripts.

Unless you change the system to only allow them that script but that's so much hubbub for...nothing.

Anyway, if you want to stick to this, then at least make it as difficult as possible for the person unfortunate enough to do that data entry to mess up, which will happen.
Even if you sanitize and verify the structure of the input, etc. There can still be typos.
127.0.0.2 enter AWWWWSHOOT
MANAGEERRRRR
MAAAANAAAAAGERRRRRRRRRRRRRRRRRRRRRRRRRRRR
MAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANAAAAAAAAAAAAAGEEEEEEEEEEEEEEEEEEEEERRRR TYPOOOOOOO TYPOOOOOO
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH

or more realistically
127.0.0.2 enter ... pfff whatever
127.127.127.127 enter
 
1 members found this post helpful.
Old 01-27-2020, 01:33 AM   #8
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by blason View Post
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

<snip>
I see nothing in your script that is testing for N_entries <= 1000. You do have a variable set to '2000' but you mentioned '1000' four different times so the problem description is rather fuzzy.

I would take a look at the 'script(1)' command and use it to collect a terminal session of your script actually being used. Set the maximum number of IP addresses to some small number, say, "5" if you're expecting a human being to do the data entry and try:
Code:
$ script myscript.session
Script started, file is myscript.session

<run your script, type in IP addresses, etc.>

$ ^D
Script done, file is myscript.session
and post the results in "code" tags. That's a lot better than just posting that something "random" is occurring. If it's really something random, run another script session and post results that show something truly random is happening. (I think the reason you're not seeing "1000" being set is that you're not setting any limit to "1000". Read the script you posted.)

Q: will the documentation let the user know that the file "/opt/.custombl/customBL" is being created for them and could be used to feed back into your script after they've edited it for errors and length? Personally, I'd have them use some other means -- scraping them out of a log file, querying a database, etc. -- and stashing them in a file of their choosing and then have the user pass that filespec to the script. You can still do the limit checking (that you're already doing) but you're not making the user potentially repeat a lot of keyboard entry should they enter more lines than you want to handle. Your script looks like it's close to doing something like that already---all it needs is to modify where the input's coming from. Like a command line argument: "myscript ip_address.lis"
 
Old 01-27-2020, 01:44 AM   #9
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by blason View Post
Cumulatively they should not exceed more than 1000 since my device buffer does not allow more than 1000 IPs.
Just out of curiosity, what kind of device buffer are you referring to? Can you provide any details? Or is this something proprietary?
 
Old 01-27-2020, 09:00 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,859

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Quote:
Originally Posted by blason View Post
Hi Folks,
[*]If $? -eq 0 then only below should process else exit 1
use: set -o errexit (if I understand well).

I do not understand: cat >> stdin, probably would be nice to explain better
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
can't someone help me i'am trying to install idevicerestore and when i do the sudo make i get this error can someone help me please shawn7226791 Linux - Software 4 09-18-2019 07:25 AM
Logic vs "Logic" bluegospel General 153 09-03-2013 05:20 PM
LSI Logic / Symbios Logic 53c875 (rev 14) -> HP Storageworks 1/8 G2 gileravxr Linux - Hardware 0 07-21-2009 04:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration