In this lesson will continue exploring positional parameters and you'll also learn how to create a while
loop.
Horses always I'm going to start a terminal on my system and move into the folder for the class and
then move into the folder for the project that I'm working on and then start the virtual machine.
And when it comes up we'll connect to it.
Now that we're connected to our virtual machine will move into the shared folder a Ford slash a vagrant
and this script I'm going to name all user Dymo 7. SH course we're going to start out our script with
the shebang and give it a path to the bash interpreter.
I'm also going to provide a little header here demonstrate the use of shift the shift command and while
loops and a previous lesson you learned that the first positional parameter of dollar signs 0 contains
the name of the script itself.
While dollar sign one stores the value of the first argument passed to the script on the command line
dollar sign to stores a second argument and so on.
We never actually used dollar sign one dollar sign to directly in that lesson.
So let's do that now so you get a chance to see how that works and how they behave in different situations.
So let's just display the first three parameters.
Well use an echo statement here.
And I'm just going to copy and paste
going to add an echo statement here which is just going to print a blank line.
Now I'm going to save the changes to the script.
And then exit.
Since this is the first time I'm executing this script I need to make sure that it has the proper permissions.
Now I'm just going to execute the script without any arguments.
So there are no arguments and that means there are no values to store inside the script as positional
parameters.
So of course parameter 1 2 and 3 are blank let's pass in one parameter and see what happens we'll just
call it with an argument of Apple.
So now dollar sign one is assigned the value of Apple and that gets printed.
So let's do this again with bread it's assigned to dollar sign too.
And let's say Candy.
So now dollar sign one is Apple dollar sign to his bread dollar sign three is Candy and we can directly
access those and use those in the script by using dollar sign one and so on.
If we were to add more command line arguments for example another word here let's say dumplings then
that is stored in dollar sign for.
But we didn't account for a user entering dollar sign for.
So that presents a challenge and so that's why we're going to use things like for loops and while loops
to handle command line arguments in many cases.
Next in our script we want to loop through all the positional parameters and the previous lesson we
used a for loop to do that.
But this time let's use a while loop.
So let's look at the while command.
What type is it.
Well it's a shell keyword or shell built in.
So that means we can use help against that command help a while and hit enter to get some information
about it.
Now that says while commands do commands done this executes commands as long as the final command in
the while commands has an exit status of 0.
You can think of this as being while true do while the condition that is being tested for is true then
the commands will execute.
This means that during the while loop you want to change the thing being tested for in some way so that
the condition eventually evaluates to false or you'll end up in an infinite loop.
Let's create a while loop here at the command line.
Let's use a variable called X will assign that the value of one will use.
While we're going to use our double brackets just like we did in the for loop.
Now we're going to use a conditional expression we're going to test a half dollar sign.
X is equal to 1.
If that is true then what we're going to do is echo.
This is the value of x and then simply Ecko the value of x.
Now we're just going to manipulate the variable x in some way and we'll just change it to 7 here and
then type done.
And so what happens is while and that conditional expression X equals 1.
If that's true and it is then it executes the code block between do and done.
So it echoes what you see on your screen and then X is changed to 7.
Now the while loop executes again.
So it says now is X equal to 1.
Well it is now false because X has 7 so it doesn't execute the commands below and the while loop exits.
This loop isn't really practical but it demonstrates changing a variable that is being tested against
in the conditional expression of the While loop.
By the way if the conditional expression never evaluates to true then the commands in the while loop
will never get executed.
For example right now the value of x is 7 so we can do Ekko dollar sign X sure enough at 7.
And if we execute a while loop again this back up here a couple of commands and hit enter.
Nothing happens because X dash Q 1 evaluates to false.
When you're learning about the wild loop and doing some experiments on your own you're going to end
up creating an infinite loop at some point it's just going to happen.
So let's do it on purpose and show how easy it is to break out of it before we do then I want to introduce
you to the true command.
It's only job is to return an exit status of 0.
So what type of command is true.
It is a built in as well as a program.
Of course we can get help on a built in by using help.
True and it says it just returns a successful result.
The exit status is that it always succeeds.
Now I happen to like the description of the executable better so let's look at the man page for user
been true all this to man.
True.
And it says do nothing successfully.
I just love that definition.
So in any case no matter what happens with true it just returns an exit status of 0.
So we can test this we'll just type the true command and then we can look at Ekko dollar sign questionmark
sure enough it returns 0.
If we do true blah it doesn't really matter because again it just always returns zero before we create
our infinite loop here let's look at just one more command called Sleep sleep happens to be an executable
program on the system.
It's not a shell built in.
So we'll use the man page to learn more about sleep so sleep simply delays execution for a specified
amount of time.
You can use sleep followed by a number and a suffix and that suffix can be asked for seconds and for
minutes for hours or four days.
So sleep does what it sounds like it just sleeps it doesn't do anything for a specific amount of time.
So I get to get out of the man page here and then just to sleep for one second.
Nothing happens and you return to the command line.
Of course that is the same as sleep one ass and you can even do half a second or portions of a second.
So let's do this sleep.
We're just a half a second and hit enter.
Now let's build our infinite loop so we can do while and we'll test for true true always returns 0 and
exit status of zero.
So it's always true.
So this is going to never change this while loop is going to run for ever.
So we'll just do while true.
Do then what we're going to do is just simply echo a random number
and now we're going to sleep for one second and then we'll close out our while loop here with the syntax
of done.
Now when we hit enter Echo gets executed it executes and displays a random number.
Sleep rest for a second if you will.
And then the loop repeats because while true always evaluates to true.
So this will just go on forever.
Now to break out of this loop all you have to do is hold down the control key and type C so Control
C sends an interrupt signal to the command which makes it stop.
Now this isn't something you need to the while command you can use this to stop any command running
in the foreground.
For example if you execute sleep and want to interrupt it and return to the command line sooner you
can use control.
See there as well.
So let's say we give sleep the instruction to sleep for 10 minutes and then we decide we don't want
to wait that long so we'll just go ahead and type Control-C and it breaks out of the command here on
the screen you can see the care.
See that shows that indeed these commands were interrupted with control.
See there's one more small piece of the puzzle left to cover before we use a while loop in our script
to loop over all the positional parameters.
This last piece of the puzzle is the shift the built in the type of a shift.
It's a shell built in.
Let's get some help on this here.
It says it renames the positional parameters of N plus 1 and plus 2 dollars on wandless onto and so
on.
Now what the shift command really does is that it removes positional parameters from the list starting
with the dollar sign when it just chops off dollar sign one.
So when you execute the shift command the value stored in dollar sign one is removed.
It's just gone.
What also happens is that the value stored in dollar sign to shift down to dollar sign one dollar sign
three replaces the value in dollar sign to four replaces the value in three and so on and so forth.
Also the special parameter of a dollar sign pound sign is reduced to buy one.
Now as you remember a dollar sign Palestine contains the number of arguments that were supplied on the
command line by the fall.
Shift shifts everything.
One place executing shift by itself is the same thing as executing shift space one if you want to shift
everything by two places use shift to.
In that case dollar sign pound sign will be decreased by two.
If you want to shift everything by three positions you shift three and so on.
Now if this sounds confusing Don't worry it's actually harder to explain than it is to demonstrate So
when you see it being used in a script it will make total sense.
So let's do that right now.
Let's use a shift in our script.
So we'll loop through all the positional parameters.
We'll use a while loop while double brackets here again make sure there are spaces in between while
under double brackets and a space after the double brackets.
We're going to test for a dollar sign pound sign here.
The number of parameters and we'll say if it's greater than zero.
That means if there were any arguments on the command line then this will evaluate to true and that
point what we want to do is echo the number of parameters.
Is a dollar sign pound sign and then will echo the first parameter here.
And we're just going to repeat this a couple more times here.
So will echo a dollar sign too and a dollar sign three then will echo a blank line and then what we're
going to do is use the Shift the built in command.
So just like we said shift is actually going to modify dollar sign pound sign because when it's executed
it removes one parameter from the beginning dollar sign one is gone it shifts everything down.
So that means at that point the number of positional parameters has changed.
And Dollar Sign pound sign has changed to reflect that.
So we are modifying the condition that we are testing for through this loop so we shouldn't end up in
an infinite loop here or some going to end this with done.
Save my changes and then execute the script.
So if we execute the script without any arguments then there are no positional parameters to loop through.
So that's why we only get the top portion of the script displaying some output but we don't get any
of the commands in the while loop being executed.
Now let's apply just one argument here.
That caused the while loop to execute one time.
So dollar sign one is assigned to Apple then shift occurs and so that removes dollar sign one to shift
everything down.
Well there's nothing left to shift down.
So dollar sign pound sign evaluates to 0 0 0.
Greater than zero is false.
So the while loop stops executing because it's false.
So let's go ahead and add a nother argument on the command line and see what happens.
So we'll add bread.
So the first time the loop gets executed dollar sign one contains an apple dollar sign to contains bread.
The shift command is executed.
Apple blows away the original dollar sign one goes away gets lopped off the beginning of the list.
Their bread which is stored in dollar sign to then becomes a dollar sign one.
And there are no positional parameters.
Two and Three.
So let's keep doing this and you can clearly see what's going to happen.
We'll do candy at the outset of the script dollar sign one is Apple dollar sign too is bread dollar
sign three is Candy the first time the while loop is executed and shift gets executed.
The number of parameters gets decreased by 1.
So then there are only two parameters bread and candy bread being dollar sign one candy being two.
While Luke gets executed again bread gets chopped off the beginning there and you're only left with
candy which then becomes dollar sign one the shift command is executed again.
Candy goes away and there are no more positional parameters so zero greater than zero is false in the
while loop exits.
So let's do this again with one more argument.
So as you can clearly see every time we go through the while loop and the shift command is executed
everything shifts down by one to the while loop executes four times and then exits.
It might not be obvious when you would want to use the shift command beyond our while loop that we've
demonstrated here.
So here are a couple of thoughts to keep in mind.
One time shift comes in handy is when you want to have a user supply information as arguments to your
script and there is one argument or piece of data that can contain multiple words.
For instance data you would want to use in the comment field when creating a user.
In this case you would tell the user to supply a username which would be dollar sign one then you would
shift and then what is left over becomes the comments field.
Now assign 1 2 and 3 and so on.
Now that's one example.
Also think about processing command line arguments.
For example something like this dash dash dash for Beauce.
So just keep these ideas in mind and will be coming back to them in future lessons and exercises.
To quickly recap in this lesson you used a positional parameter starting with dollar sign one explicitly.
You review the special variable of dollar sign pound sign.
You are also introduced to the true sleep and shift commands.
Finally you put all this new information together in order to create a while loop.