help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Help With School Assignment, Learning Bash Scripts


From: Jesse Molina
Subject: Re: [Help-bash] Help With School Assignment, Learning Bash Scripts
Date: Wed, 05 Nov 2014 19:54:58 -0800
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:33.0) Gecko/20100101 Firefox/33.0 SeaMonkey/2.30


On 11/5/14 18:01, nick wrote:
The best approach is to check each positional parameter within the loop,
before the addition.

Not sure what positional parameters ($1, $2, $3, etc) have to do with the below. I assume that is some other part of the script (it's standard input) that you don't need help with.


You will use an "echo" to display the value of the "number" variable,
and pipe the output into a "grep".  The "grep" should search for a single
character which is NOT a digit or a plus or minus sign.  A character class,
something like "[^0-9+-]" would be the easiest way to do this.

If the "grep" is successful, then the message should be displayed and the
script terminated.

Continue to the next step to see how to start.

Sounds like the lesson here is to teach you how to validate your input. If you have a script that adds numbers, but you give it non-numeral, that's some invalid input and it would be reasonable to display an error. If you are ever going to write code, you are going to be doing a lot of this, so this is a good lesson to learn.

Your teacher wants you to use the "grep" tool to do that input validation.

Well, looks like it's time to learn about grep. I would assume that you know how to do a --help, read the man and info pages, so you should do that and find an option that will give you what you want.

That also means learning a little bit about regular expressions (regex). In this case, you would just need to know if your number variable is a real whole number (an integer). You already gave an example of such a regex. Learning regular expressions will be really valuable.

There is an option/flag to grep which matches the *opposite* of the pattern you gave it as input. I use it all the time, and you will too if you use the command line. You should read that man page and figure out what it is, and then do some experiments with it to make sure it works like you think it should.

The second difficult part of your task above is the "If the grep is successful" part. What does that mean? How are you going to test if grep is successful or not successful in finding that pattern? There are actually multiple ways of doing this with bash. This one might be tricky for a noob and you should ask your teacher for help on this one if you don't already know what to do. The reason I say that is because they might have recently taught you this and they expect it to be done in a certain way. They are going to know you cheated yourself if you use one of the other many methods.

Good luck.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]