Unable to run my logic here, can someone please help?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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
Hi Folks,
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.
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
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
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:
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.
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.
Lastly; 1000 limit is not getting set as system picks randomly No idea what you're talking about here.
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.
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.
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.
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
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803
Rep:
Quote:
Originally Posted by blason
Hi Folks,
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.
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"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.