Let's say we want to add two numbers together and assign them to a variable so we can do this like this
will create a variable right here on the command line we'll call it.
And we'll follow that with Dollar Sign opening parenthesis opening parenthesis and then an expression
here.
One plus two and we'll close that with these double parentheses here.
So this dollar sign parenthesis parenthesis syntax is called arithmetic expansion bash evaluates the
arithmetic expression within the double parentheses and it returns or substitutes the answer in its
place.
So now none with the variable knowm should contain the value of 3 and we can check it like so.
And sure enough it does OK let's do some subtraction will set.
No equal to 10 minus 1.
And the value of that is nine multiplication is handled with an asterisk.
So let's see that two times for
is 8.
Division is handled with a forward slash so we can do this.
Let's talk about this particular situation so we'll do Six divided by four and then bash returns 1.
So 6 divided by four is actually 1.5.
But again Bash just returned one to us.
So here's what you need to know.
BASH doesn't do any rounding.
It just drops off the decimal point and anything after it.
So really bash doesn't support floating point arithmetic.
That's usually fine because what you'll end up doing most of the time is very simple math like adding
two numbers together or incrementing a number by one during a loop and things like that.
If you need to work with numbers that contain a decimal then you're going to need to use an external
program such as B C which stands for basic calculator.
Now it might not be installed by default.
So if you do this type dash A B C and you don't see it there then what you would need to do is do yum
install this yes or dash y excuse me b c to install it.
So here I have it installed but you may not.
Again you may need to install it.
So let's get some help on this thing here Dash 8.
If you want to do floating point math with b c you have to turn on the math libraries with the dash
l option.
Also it's important to know that b c reads from standard input so you can do things like this.
We can do echo Six divided by four and we'll pipe that into B C and we'll use the dash l option and
it returns 1.5 by the way if you need a quick calculator at the command line you can just run B.C the
shell and then do that here as well 6 for one plus one or whatever you want to do and then you can type
Control D to exit out of the calculator here.
Quickly you can also use awk to do this.
It's a little bit more cumbersome but I'm just going to show you quickly how to do it and you can stack
it away if you want to use it at some time ago.
I recommend doing this but you may see this in other people's shellscript so we can use this arc again.
And then what we want to do is print six divided by 4 and that is 1.5.
So whether you're using B C or awk you get the full and complete answer.
Now let's get back to bash.
So Six divided by four according to bash is one.
There is a remainder though when you do six divided by four and you can get that remainder where the
percent sign.
Also known as Maggiolo or the modulo operator so we can do this no.
Equals 6 Maggiolo for and that is going to be 2 it prints 2 because 6 divided by 4 is 1 with 2 leftover
or a remainder of 2.
Now this comes in handy when you want to do alternating things.
For example you could treat all even numbers one way and all odd numbers in a different way.
Even numbers divided by two always have a remainder of 0 while odd numbers divided by 2 have a remainder
of 1.
Then you could use a simple IF OR Case statement to handle those different situations.
By the way you don't have to use hardcoded numbers like I've been doing here.
You can also use variables inside the parentheses too so we can do this something like this.
Dice A is equal to three dice B is equal to six and the total of those dye would be TWICE A plus B
and of course six plus three is nine.
You might have noticed here that I didn't use a dollar sign for dice or dice B.
Those are variables inside those double parentheses.
You don't do that when you're using this particular syntax.
By the way you can assign or change the value stored in a variable here you don't need the leading dollar
sign that leading dollar sign is used for substitution but we don't want to substitute the command for
its value we just want to change a variable.
Hopefully just seeing it here will make more sense and to be clear than me trying to talk through it.
So let's let's let's set Nahm equal to one and let's increment the value stored in the variable known
by one and we do this by Prentice's parenthesis none plus plus.
And then the closing parentheses here.
And let's echo this.
So now numb is two.
So we've changed the variable without doing any output when we are using the double parentheses syntax.
But it still affects the variable.
Now let's decrement the value stored in the variable number by one and we can do this with a dash dash
or minus minus and then two minus one is 1 and there we are we have one store in the variable name.
Let's add a number to the value of the variable and store that new value in the variable.
So we can do this knowm plus equals of 5 so 1 plus 5 or 6 and we assign that to none.
And that is what is stored there.
You can also do things like minus equal asterisk equal which is multiply or slash equal which is division
Maggiolo equal and so on.
Now if you wanted to you could actually use substitution to change the value of the same variable and
it would look like this.
We could do this number is equal to dollar sign.
These parentheses num plus equals five
and that is 11 because 6 plus 5 is 11.
What this does is takes the output of the nonplus equals five operation and assigns it to the variable
Nahm.
If that seems clear to you you can do it that way as well.
Or you can use the other syntax and I was just demonstrating.
Now before we wrap up our discussion on math I want to show you two other ways to perform math operations
that you might encounter in other scripts.
The first way is with the let bash built in.
You can see all the operators that are supported by doing this.
We'll just get some help on it help let And we'll pipe that to less so you can see here the post-increment
operator of plus plus the pre-increment plus plus that's before the variable name you know plus and
minus the exponentiation.
All these things that we're doing here and these things that we were doing here like plus equals minus
equals and so on.
Now this let syntax pretty much does the same thing as using double parenthesis.
So let's do this.
Let numb equals two plus three
and sure enough that sets numb to five.
You can also do this let numb plus plus and then increments it by 1.
So now we have now with a value of six.
The second way you'll see math performed in scripts is with DXP our command the double parentheses and
actually been using is built into Bash.
And that's the route I recommend if at all possible however you can rely on external programs like we
saw earlier with B C so they you export command processes an expression given to it and then since that
result to standard out so you expire one plus one it returns to now to do assignment.
We just use command substitution like we would with any other command.
So you know normal command substitution syntax
and again two plus three returns of five that is assigned.
And that is what is displayed when we access the value stored in that variable.
OK.
So that was a pretty long aside but we needed to cover it before we could get back to get ops and let's
do that now.
Get UPS does not change positional parameters.
What this means is that the options are stored in dollar sign at Sign Dollar Sign one dollar sign too
and so on.
So let's add a bit of code into our skip to kind of prove this and pointed out.
So right here after a while loop in case statement let's do this echo the number of arguments stored
in dollar sign pound sign
the contents of all the arguments is stored in our sign at sign.
The first argument is stored in dollar sign one
second argument is stored and positional parameters are assigned to.
So now let's exit our script and see what this does so we'll do this all user DeMaio 11 Desha cell 8
and we'll just type some extra stuff if you will at the end of the command line.
So here we have three arguments dash S L is stored in dollar sign 1 8 is stored in Dollar Sign 2 and
extra dash stuff is stored in dollar sign 3 in my mind once the arguments are processed they should
be removed as a positional parameter.
Now get ups doesn't do this for us but it gives us a way to do it.
So let's jump back into our code
get up sets a variable name.
I n d.
It stores the position of the next a command line argument following the options in that variable.
So let's look at its value in action.
So we'll run our same command here in OP I Indy is set to 3 because the third argument is the index
or position where the arguments start after the options if we want to remove the options that have been
processed.
That means we need to shift them down by 2.
So stated mathematically that Opt. I N D minus one.
Now let's see if this little formula holds and some other examples so let's do this let's issues dash
s and then some extra stuff.
So sure enough OP I India's too.
So if we subtract one from two that leaves us with one and if we shift by one then what's left is anything
supplied.
After the options now it's just run it with just some extra stuff without any options that we're checking
for.
Now in this case like I said there are no options and nothing needs to be shifted.
However our little formula still works because up India's one one minus one is zero.
And if we shift by zero we change nothing which is exactly what is needed for this case as well.
So let's add this little bit of code or this formula or this algorithm if you will to our script.
So we're going to shift by the number of opt in D minus one.
Now we can process dollar sign at sign dollar sign $1 sign too and so on just like we've always done.
Now let's display the contents of dollar sign at sign Xandra after the shift so we'll do this Ecko after
the shift.
And we'll just grab all these up here and print them to our screen again.
So now let's use our user Dymo 11. sh.
SL 8 extra dash stuff.
So sure enough that works the number of arguments we have at the beginning is three again dash SL is
stored in Dallas on one dollar sign two is a dollar sign three evaluates to extra stuff.
So after get UPSs done and we process all those arguments the only thing we're left with is extra dash
stuff which have to we do our shift is assigned to dollar sign one and that's exactly what we want.
So then we can do things like loop through all the other remaining arguments on the command line with
a while loop for example while dollar at sign.
We could then make people do things like after all your options then we require you to give us a file
or a username or whatever that we expect now assign one then we can test and do some work against dollar
sign one and so on and so forth.
However let's say in this particular script we want to treat anything other than the options on the
command line as an error.
And the easiest way to do that is to test the dollar sign pounce on value.
So let's jump back into our script and say if
if the number of arguments is greater than zero then we're going to give them the usage statement and
we'll get out of here again we're going to reduce the usage function since it teaches the user how to
use the script the way we want them to.
And then it exits with a non zero exit status.
And remember when you use shift the number of arguments changes and that also updates the dollar sign
pound sign variable so this is going to work.
Now before we test our updated script let's clean up our bugging and development code here all these
all echo statements and make sure everything is all cleaned up.
So let me go up here.
We'll get rid of that
and we don't need these lines as well.
Now let's do this let's run it with an argument.
So here it gives us the usage just like we want or we exit with a non-zero exit status because it's
in that usage function that we created at the top of our script.
So let's do something like this.
Let's say we'll pass a valid option and then also append some stuff to the end of the line here.
And sure enough it gets to that point we're looking at the number of arguments after we've processed
the command line options.
And that has triggered the calling of the usage function as well.
So let's go ahead and do this with something that is proper A dash s.
And sure enough we get our password with a special character appended to it.
Let's do a quick recap.
And this lesson you learn how to use get up to process command line options.
You learn how to wrap the get ups command in a while loop and use a case statement to perform different
actions for each option when you want an option to take an argument you follow that option letter with
a colon in the OP's string.
You also learn how to strip away the options from the set of positional parameters by using the OPT
I and the variable provided by get ups.
Finally you learn how to perform mathematical operations.
You learned that you could assign the result of an arithmetic evaluation to a variable by using the
dollar sign double parentheses syntax.
In addition to hard coding numbers in your expressions You also performed arithmetic using variables
you even learned how to change the value stored in the variable directly using a few different operators
including the increment decrement and the plus equal operators.