[Music]
okay
um welcome everybody uh this is the
third class
of linux and electronics at raspberry pi
course
today we're going to be seeing and
learning about uh
bash scripting and python scripting a
little bit a little bit of python
scripting
where first uh we're gonna look
at patch first that's gonna that's going
to be our main thing today
and yeah so today i'm gonna be working
from vnc
as we saw in the first class i connected
myself with uh
as as you all know i connected myself
with uh
looking for the ip on the
on the on the windows command line
connecting through through end map here
his respect
my raspberry pi and
then i i connected through the ip
through vnc viewer
having already uh all the settings here
on the raspberry pi configurations
and interfaces having ssh and vnc
all set up so today we're going to be
doing a batch scripting this is
something that
most system administrators
have to do on a daily basis um they
this is a this is for automating a i
like to call it a process
a way for uh programming as operating
system this is how i like to call it
it's not technically that so
yeah um so
last time we saw a little another
example in the last class we saw a
little example
of how to create a new a a batch script
a little simple one uh it was it was
just two lines uh calling uh
the the shell that we're gonna be that
we were going to be using
and using an echo command just to do the
example
right um then
we proceed to to
to run it or do or
give give it permission and then run it
this other way
which is another way to to to run a
command
okay so what is bash bash is a shell
program is as we as we saw in the first
class
a shell is a program uh that basically
it inter uh talks to the kernel
through and between the person in the
kernel
and any any any reads commands and it
gives you
output output that we saw in the last
class called
uh a standard input standard output is
under error
um bash it stands for born again
shell that's this that's a that's a good
thing to know
and there are other shells that's not
there's not only one
there's shell two bash the difference
between bash and
shell is that bash is uh it has a couple
more features
so it's uh it's just chill but
with let's say asteroids there's there's
no many difference to talk about here
uh but just just letting you know that
bash is the standard
csh and ksh are both
other type of shells um i've only seen
this
this used by my teacher but i don't know
i don't i don't know i don't know
which are the specific use of this for
raspberry raspberry pie we're only going
to be using bash
there's no need there's not going to be
a a a
sudden need to use this uh one of these
two
um right away
so let's see how a batch script is is
structured
so everything i'm going i'm going
we're going to be seeing i'm going to be
doing here in the
vnc so let's go to the common line right
here and the command line so
so let's create i don't know if you
people can see that
i don't
okay great much better so let's create
a example script
so we see is here and also here
here
so
here how you here's how you decrea
declare
variables in bash you can do you can try
this on the command line and also you
can do them
on the script we're gonna do that
it's a common it's a good price it's a
good practice
to put the the variables on on on on
caps
that's something you need to know so let
me do this
so let's use nano to edit the script
and let's do what we
always have to do which is declare which
is the child that is going to be
running on um in a second
[Music]
okay here
let's decline it the variable here
let me change my keyboard
let me put pablo here
oops
and let's say echo name
this is how you call the variable back
this is what when when you when you're
using another program
and and you want to use a variable that
you store or you are
um storing let's say you call it by
putting a dollar sign
on on the beginning of it so let's write
this yes enter and let's run it
oh yes it's because we don't have
permissions as you can see here we don't
have
permission to execute it so let's do
that
now i was this is the way i i look for
for comments that i just typed pressing
the arrows up and down
so i now press one up and this is the
one that i just did
press another one and this is the other
one i did
so here i just run my program my batch
script
and give me this which which was the
variable that i put here
so let's continue um this is
um this is another example
um a good a good thing you you're gonna
you guys
want to do today is looking on each link
that i have below and if you if you're
interested because
each each source that i put here is very
is
really really good and maybe as even
necessary
so um
this way this way of declaring a
variable
is doing a command and the output is
going to
is going to be storing the variable so
if we do if you remember this command
pwd
which it gives you where you are the
root path of where you are
you can just change here
and let's change how do i call this this
command
let's do this
save it and let's run it
so what it did was that it run the
command
it run the command at the when when the
variable was declared so if
it goes one by one did this first and
then
store it and then it goes it went down
and did the echo
that's what what just happened this is a
way
for you to in case you need to know uh
for example
this is a good practice to do it and if
you're doing um
some script that you need to be sure
that you have
x version of your program
uh running in your computer so
let's say python v
oh wait
oops let me go back
let me open up again
okay um
okay so let's do another um
yeah let's do date okay so
if i wanna if i wanna if i want to save
the date oh
i'm not in the in the desktop
here and then none script
i can just do this
sure
there you go and if as we saw there are
more programs that you can use
to to cut whatever information you want
and you can parse it
as as as whatever you want
um this is really cool i've i've done
programs to let me know
how much storage i have left in my in my
in my
in my computer when i was running ubuntu
on my main computer
and and and i even did notifications on
ubuntu
uh which has their own it has its own
program to do notifications
so yeah uh so functions this is how you
declare a function
you just put a name on it and
a good a good practice is to use
um underscore for
for spaces
and then you just when you when you want
to call it
you just put the name of it this is not
the only way to do it
but let's let's do this example here
let's just do it right here right
oh we can lose this
so here i'm going to put the name
i'm going to put the parentheses i'm
going to put
here um
and then
calm date
um yes
no let's just run date
right now um let me cancel this
here
okay
right now um this is not being used
but let's run it anyways
let's see if it works okay it didn't
let's let's see if this way it works
no it didn't okay so let's use the
example
all right forgot
i was doing an echo to the thing
there you go so the only the there's
the way that they're showing you here
that you can do it is just is to put it
inside of a string
that you're going to call but as as you
can see here
i did call the function just by putting
the name of it
okay so have that in mind
so conditionals this is a good way to
make
a program uh to be uh
to to know when things are happening if
you're doing a backup and you want to
search
if you want to see that there's a um
a file that exists and if it's not then
it should run the program again again
because there's an error
so this way
is is is a way to you can do an if and
and
with an else if notice
and i really like this about about uh
about
bash scripting that the end of it
is just the the if or whatever
whatever you're using at the end but add
backwards
so if you want to declare at if um
then you need to you need to put it to
put it backwards
at the end
so
what can we do
what can we do what can we check
let's see what programs do we have um
let's do
okay uh we're gonna we're gonna see the
conditionals in a bit once we once we
see
once we once we see how we can uh what
this
what what these uh commodities means
what this conditionals means so you can
you can compare on on these conditionals
uh you can use equally equal for to to
see
to test if those two strings are equal
these two strings can be um
variables where you have store for
example if you run the date at one point
and you have for example
older date saved in a variable
and then you have a new date to see
that you run every time you run the
program and you see the both
are the same it means that it shouldn't
run the program again because you just
did
right because you have an all the the
the same date and the new date
both the same date if they are not equal
you can do whatever else you want to do
here there's another type of of
conditional which is uh
which is really fun because it's really
easy to to see
this this means less than this means
less
than or equal equal
greater than and greater than equal or
equal
this is another this is this is other
type of
uh conditionals which is uh
you can just uh look for a file
for example and see if if it exists
if it's if it's at the directory a file
an executable if it uh oh i put two
times
if it exists and if they are the same
file
so i wanted to see this before before i
want you to see this before
i did the program because now we can we
can use it in our
in our program right so let's use this
example
so let's do exist and let's do
am i here
dot txt
so i can use this
here
so let's test this
this
let's debug a bit
we can just do this
if it just sits there
it's probably because it's not the path
home by this
next stop
let's see if it runs
huh this is interesting
okay we're going to look at it in a bit
you can do comments you can do comments
uh
here you can do comments one lines are
here
and multiple lines are with these single
ones
okay
okay for some reason
the multiple line
okay
okay so this is this is the fun part
this is where you want to do
um cool programs and
that even takes that that they're being
as if there were comments so let's
create another
um another another program
input test let's call it input sh
let's do uh let's put the
the permissions tool to run it
okay so as every time we declare
that it's going to run the bash
here so um
let's do a program that
enter h
let's do a program to see if you can
enter to
a library right
so read and let's put the name
the age
h of
person
okay so
um
let's see if
age of person
is um
it's greater than five
now it's greater than five
so
library
and we should test if this is if this
works
let's put four
and if we put 10
there you go oh i did a really big typo
library so
this way you can do um let's say let's
see if it works
the less less
or no greater or
equal i think it was
10.
i think it didn't okay let me check that
hmm okay i did that right
okay so i i did i did this a couple of
times and i think it is something about
the syntax we we're gonna look at
look at data in the end so don't worry
about it
so we just did a program that you can
check
that the the age of a person that you
put it that you enter
you can also do as as we saw
you can instead of doing this you can
just put um
i'm going to put it in a comment here
for example
if there's a table with the
with the with the all the all the of all
of the
the ages per person you can just do the
uh cut the
let's call the h database a file
then you pipe that into grab let's look
for the name
let's call the key george
and then you know the the
the h is on the third column for example
as we saw in the last time there's
there's like a
a a little command
which is something around like this it's
not it's not like this
but it's it's something like this so
so you what you can do is
once you have that program that is one
if if you if
you put press enter you're gonna get the
h
just the h you can just do as we did
before
um h or
h of person
equal here and here
so this is going to run the command is
going to give
the the the result as a as a
as an input for this um
this variable so the only way is not
just reading by the command line but you
can just find it in another file
for example if you are on a server or
but you can just scrap it for from the
internet
so yeah that's those just a couple of
ways to do it
so arguments um this is another way that
you can do is that
this is a way to make a program more to
look like a
um yeah to look like a command
so what you want to do
is let's call this
h let's delete this
right so let's call this the same age
of person
equal
one so once you run the command with the
next
with the next argument on it which is
going to be this one
that's that's the the the
the input is going to take us an
argument so if we do
um let's do a state say it right
bash input and let's do uh five
oh wait i did this wrong
i think this is wrong
there you go so as you can see i can
i can pass many arguments and i can make
it
for example to look for uh instead of
numbers to look for uh letters
and then you you you're gonna be able to
make a command like
like ls that that it waits for
for a nail or some other later there are
good practices and there are good
examples on the internet if you want to
look for it
and it's and it's uh and it's a good way
to make a program
so let's do crontab this is
a current tab it's a process which is in
linux it's called daemon spelled this
way
that run continuously and it's only and
only going to run
the program when it when it at a given
time that you
specify it here we have a couple of
contact commands
this raspberry pi has a program that i
that i put in
um that is that it runs on the
each time each time it boots
so here
we're going to create a new one so be
aware of that
if you want to see the here we have the
the commands we're going to see each one
of them chrome tab crown tab e
is for a deed and creating a a contact
crontab e actually
opens you up the file
that has all the crontabs available
when you boot up your raspberry pi when
you start using your raspberry pi
this should be this this lines shouldn't
be
in because it should be blank
i have this one and i'm going to i'm
going to explain it in a bit
but for the time being let's do let's do
the other comments
so crontab slash hell
which is just like we if we were doing a
cat
just reading the file uh if you want to
remove it i'm not gonna do that
and if you wanna see the the last time
oh yeah this this these comments some of
them are are version
version sensitive for example if you are
in on
a this this command probably works on
ubuntu but it doesn't work here
so chrometap it this is the content
parameter this is how you
specify um when a current
contact task is going to run
so this first this first character
means the minute this is the hour
this is the day this is the month
and this is the day of the week or
weekday
and the comment to be executed
you can use um
you can use you you're gonna be using
numbers
um and you're gonna be using a specials
um oh i just repeated it
this you're gonna be using a special
characters which which are these ones
as you can see there's one called at
reboot which means that boot
which i'm using right here
here at boot so each time the
raspberry pi boots this is going to
this this program is going to be
executed
these other special characters means
something else
and we're gonna see here if you want a
program to be run
um for example
at every time every minute of every hour
of every day of every month of every day
of the week
we just need to do this just put five
and then the comment to be executed
here and as we cannot see this running
because it's going to be run on on not
this terminal
once it we we save it it's going to be
run on a terminal that doesn't have
a a an instance like this window
is going to be running each time each
minute
each um of each hour of each day of each
month
of each day of the week if you want to
change that for example
i wanted to to be used uh to be run
every 15 minutes so we're gonna use
this slash
15 minutes right
of every one every uh one every two
hours
i mean every 15 minutes or
uh of every two hours
this is this is a way to every two hours
is going to be run
this means it's going to be in the
minute zero by the way if i put a number
this just means that it's going to run
every on every two hours
and the minute is 15. so have that in
mind
if i want a range i can just use
15 30
which means it's going to be run on each
minute
from 15 to the 30
to the 30 minutes of every two hour
so it's going to be for example let's
say
zero or let's say 12 a.m it's going to
be run on
the minute 15 16 17
until 30 and then it's going to be
one hour which is going to be 1 am and
it's not gonna run
then the other hour which is going to
run
again on 15 7 16 and so on and so forth
um you can also use if if you want
specific times you can also use
uh what was the common it was a coma
right
yeah it was a coma so you just do a coma
just put 20 10 20
30. so this
program is going to run this program
right this command
again if if you want to do a program
you just need to do what we are writing
out how to do which is put python 3
or bash or
and then you put the the
the path which is going to be home pi
desktop and then you can put test.pi
for example and and andy and this or sh
okay so you need to be you need to be
careful with the name because you're not
going to be able to do
tap to autocomplete and because this is
a file
this is not a a this is not in the child
this is a file so it does not out
auto complete so yeah so if
this program is going to run on on the
minute 15
20 30 of every two hours
of every day uh of every day of the year
of every day of every day of every month
of every day of the week
so have that in mind you can also do
this um
with letters which is more a little bit
more simpler
you can do this if you want something to
happen a um
at some point weekly
then you just do that you may be asking
at what
time is going to be running at for that
yeah let's go back for that
i have a a good tool that i put down
here which is crumb type guru
you can go with me here contact
this is a really great tool if you want
to like test
what's the best time for you
so here let's do um
every five minutes
every fifth minute of the past hours
five
and if you want to do let's say what we
were doing here
doing ah okay so you can do also
i don't know if you can put it here yeah
we clean
so yes it's going to be run at zero zero
zero on sunday
this is the default uh setting that it
has for weekly
if we look for monthly
yeah zero zero zero of every
day first of the month so this is a
really good tool and i suggest you to
use it i always use it before putting it
on my on my on my contact slash e
so yeah i have that in mind so let's do
a test
and let's do a test where we run
um we run a program to
to make a file each time that
every minute so we don't have to
stay here every day so let's let's put
here
right okay there you go
so let's put five co five uh asterix
which means
every day i mean every minute every hour
of
every day of every day of the month of
every week every day of the week
okay here
if you're confused you can also use just
a day of the month
right month every month of every day of
the week
every day of the month
so let's do create a in home
pi desktop remember you cannot
have um typos here if if that happens
then
the command is gonna run anyways but
it's going to give you an error and it's
not going to show it
it's going to only going to show in a in
a log
that had the crown has or if you want as
we
show you how to do on the second class
which was
how to output the error of a command
into a file you can also do that here
so that home desktop pipe desktop yeah
uh
crown tab just checks it
yes so let's
wait i think i don't
it's probably um
let's see if if it works
let me show you what meanwhile this uh
this command which means watch
which me which is for um
to to see when something is happening
let me use this oh here
it works so it just made it i'm going to
delete it
move to trash yes and then we're going
to wait
until the other one happened ah
and so these comments is for
this command is for watching every
one second
here maybe here
so this is every second this is oh
wait i'm sorry
okay so what what i just did was a
command to
run every second this command so if i
want to see if something happened here
i can just do that command and i'm going
to
see when when this changes as you saw
the crown tab was the contact 5 was
generated and then
it appears it appeared right here
so i can just go to contract e if i if i
want to save a crontab
you don't have to delete it you just put
you just make it make it a comment
and then we are not going to have that
program run
in a bit or whenever you initiate it
again
this is what this is this
crontab is something you can use for
again if you're doing a
computer program to be run every time
the the raspberry pi boots up you put it
at boot
if you have uh backups that need to be
done
for example most people are i don't know
if most people but i've seen that that
people with uh raspberry pi projects do
it's they put a current contact to
sip a couple of files and folders and
direct our directories
and then move them up to the cloud
that is something that that is something
that you can do i've done it with mega
they have the their own api or i i
remember
if i used the third party which is
really cool
um so yeah
okay so python script we're gonna see a
little bonus
um for this python script um
this and the other and the other
subjects are going to be bonus
uh because we're going to see some more
depth in the next class
but python script is a as
uh we're going to see how to make a
simple python script on on raspberry pi
to control the gpio so here we have
something that is
really important which is the the the
numbering declaration
which the gpio if you don't know
or you have a couple of pins which are
uh color golden on the raspberry pi
which is where you connect things that
you want the electronics that you want
to
to to interface with so there are
two types or two ways to
call each one of these these pins which
are gpio.board
and gpio.vcm
gpio dot board is is uh
the board the raspberry pi scheme is is
uh a numbered scheme that they they they
went with
and and they created but the bmc is the
broad broadcom
chip specific pin numbers which is when
when you're in electronics each
microcontroller or single board computer
uh has uh a processor or a
microcontroller
the microcontroller per se has pin
numbers
so the broadcom processor that they're
using where they are connecting the pins
to
those pins has their own numbers so
whichever you want to use
your feel free to use to use it this is
uh
this this is what the
the the pin numbers both of them look
um here is the board
the in the middle so you can see one two
three four five it's just
number as as as it goes down
but for the bmc you're using these ones
gpio 14 for example
or um 17 instead of 11.
so yeah if you want to do a program
where you can um
you can do it right now uh we're gonna
there's something missing here we're
gonna see it in in the next slide
but what we what you're gonna do is
import
the gpio library which is gonna be in
the other slide
uh you're gonna set the mode for example
to bcm
and then you're gonna set up the pin 18
as an output
there's uh we're gonna look that in into
more detail in the next class
and you then you put it
high which means that you there's going
to be a positive
uh if if you measure positive
output on this gpio so the eight
the 18 pin of the bmc
is going to be this one if we wanted to
do it
on the board board number which it would
probably
be 12. so have that in mind
so this is the the descript bonus
which is going to be a blinky using an
led from your from your
from your um kits
first of all we're gonna see that here
the
the leds if in case you don't know have
a polarity which means that they they
they can
they only can be used one way you cannot
connect it in the other way
because it's either going not going to
work
or probably going to burn not
not right not with this kind of current
by the way but i mean if
if you're doing a special application
it's probably going just to shut down
and never
never turn again that's what i mean by
burn by the way
so this is a way to connect using your
breadboard
i'm going to explain because the
breadboard seems seems a little scary at
first because you don't know
what each of those uh holes mean
so here each line
each line um here
are connected so this is like basically
a wire
all of these lines as you can see here
and here
is like a cable so this is connected
and on the sides depending on the type
of breadboard that you have
you probably have in the middle cutoff
so for example here if you connect
something here on this pin
and something oh let's just use this
here on this pin
and here on this pin is is going to look
like it's going to use like a cable
these are typically used for voltage
so you can put here and the red one the
uh positive voltage either
the five volts or the 3.3 volts that
you're using and here then
the ground or the negative and but
you're not required to use this is just
to make your life easier
but in case you run out just in case you
run out of this
you can use this ones and what i was
saying was that
some breadboards come with this cut in
the middle
so you can connect something here and
something here it's not going to be on a
cable
until you connect it in the middle okay
and until you jump
it so yeah
so you're going to identify which is
going to be the
smaller is going to be the longer uh pin
of the of the of the uh of the led
but i want you to put the led uh
in front of a uh between you
and the lamp so you can see these two
parts
the little one is the an is the anode
which is the positive
pin if you're going to
[Music]
for example turn this led on by sending
negative
then the positive should be should be
connected right on
right on top of here for example you're
gonna be connecting here on the
five volts we're going to see that
in case you didn't understand it we're
going to see that in more detail in the
in the next class
so yeah so here's the example program
that i was telling you about
this is a python program so what you
need to do
i'm going to do it here with you in case
you're doing it with me right
in case in the case okay so
so let's create a program a file called
blinky
dot pi we're going to give it
the the permission
to be executed there you go
so we do ls l and we're going to
see we're going to see that blinky has
executed permissions right so
uh let's let's enter
by the way you can also use if you're
using the raspberry pi
uh gui right you can also use
here the this ide
which is basically a specialized if you
don't know what an ide
is it's basically a
text it's a notepad or it's a text text
reader
but with asteroids so it's going to help
you to
to develop in x or y language
this one is specifically for python
so we're going to see blinky right
let me see because i have it okay there
you go okay
so yeah i'm going to import the
raspberry pi the
gpio
as gpio so what i'm doing here i'm going
to explain this briefly
here is what you call the library this
library is
basically a program that helps you
to talk to whatever you need uni or
or do whatever you need to do this is
just basically com
com compact programs that you call for
you use on this program
so i'm just when i use as
gpio i mean that whenever i'm going to
call this i'm going to call it like this
so for example um
instead of doing raspberry pi like this
raspberry
pi gpio instead of doing this
dot again set warnings
false
instead of doing this i can just do gpio
set warnings
false right
there you go so that's what i'm doing
right here like
you can just change the name it doesn't
need to be either you can just do
for example let's call it sun and then
you can just call it like that
um this is case sensitive by the way
as you can see that even the eye of the
raspberry pi is case sensitive
so from the library time
i'm going to import sleep
which is going to help us to do the the
timing for the blinking
so here i i said that the first line
which is uh
set the the warnings off if if you have
it on
in the in your in your uh whenever
you're whatever you're running the
the program is going to is is going to
give you a couple of warnings so
you just said so set them the them up
for the time being
gpio set mode
so gpio board
let's use the board one
then use gpio setup oops
set up
eight gpio out
and then you can even put the initial
state
like that but i but i wanna
for you to understand it better so you
don't have uh like too many things
in that statement i just do uh
gpio dot output
and then i just set it manually low
so there if you save this program
and you run it um
yeah this is it's just going to run and
it's going to go
and and and set that pin off and this
and that
then the program is just going to stop
so in order to do the blinky we need to
do something called
a a function in order to for it to run
like forever which is while
it's wild true
missing space
then we can do okay so let's turn
this pin on
let's sleep for one second this is
something that
this is like we call this in the
programming
uh of our work we call this a delay this
is something that
um when you're doing timing things it's
just
you're telling the the cpu of of your
computer which is running
really really fast a couple of times
many times per second
you just doing do don't do nothing
do nothing for a second and wait for the
next command
so the next command is going to be this
one
which is go which is we're just gonna
set
here so
now if i save this and run it then it's
just
going to to be forever here and doing
the on and off
i want you to try that to connect it to
the to the
to the pin number eight which is going
to be
uh on the board will be this one or the
14th
in the bmc i want you to try that and
let me know
how does it works the result should be
something like this
should be turning on on and off um
so yeah that's
that's it for the class of today