This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Scripting 101

Chat log Class 2

hemanth hm's picture
Sun, 2010-09-19 19:37

<hemanth> has changed the topic to: Why scripting ?

<hemanth>    Hello hackers :)

<jmuni>    Hello!

<hob>    hey there

<hemanth>    redguy and hobs you are new comer, hope you have read the logs, please introduce yourselves

redgu21    hemanth: yes, I read the logs

<hob>    any specific information you are looking for hemanth?

<hob>    I did read the logs

<hemanth>    just a one line bio, about you is good

29redgu    my name is Mateusz and I am a linux administrator from Poland. I want to polish my python and learn a bit of ruby scripting.

<hob>    I am 24 year old tech support trainer, looking to script all my work away

<hob>    also my name is hobs/constantine

<hob>    you can call me hobs

<hemanth>    redguy, that's nice, it would better if you could say GNU/Linux administrator :)

<hemanth>    hobs, kool

<jmuni>    haha

<hemanth>    welcome hobs and redguy  hope you have also forked the repos

<hob>    rms will be sad otherwise

<hob>    yep

redgu21    hemanth: indeed, GNU/Linux is more precise :-)

<hob>    its a really good idea to have class stuff in a git repo hemanth btw

<hob>    good idea

<hemanth>    thanks, just an extra second by saying GNU, we make a great contribution :)

<hemanth>    okies, now lets start with todays topic

<hemanth>    "Why scripting?"

<hemanth>    Scott Adams in Dilbert Principle says, "In contrast to 'normal' people, engineers have rational objectives for social interactions: (1) Get it over with as soon as possible; (2) Avoid getting invited to something unpleasant; (3) Demonstrate mental superiority and mastery of all subjects."

<hob>    lul

<hemanth>    I wont talk about 3, but 1 and 2 is very true, when it comes to getting things done, even in programming aspects

3*    23laksh has quit (23Client Quit23)

<hemanth>    Scripting mainly came save all the hackers with there sys administration

<hemanth> Scripting is the easier, smarter and faster way of getting things done

<hemanth> common-sense approach to making languages easier to use is to make them more powerful

<hemanth> Scripting metaphors tend to be easier, very similar to how we think, very much like a direct translation

<hemanth> The other major use of scripting is the batch jobs

<hemanth> that is doing a similar operations on large data

<hemanth> or sometimes even the day to day jobs we do again and again

<hemanth> can be made easy with a simple script

<hemanth> The third interface metaphor is the object-event model

<hemanth> this is very much higher when compared to the rest

<hemanth> it includes, users doing n/w programming, sockets, data crunching, mining and even GUI with scripts

<hemanth> no, wonder why the so called 'G'iants of the s/w filed, like scripting very much ;)

<hemanth> as Larry Wall said, “a script is what you give the actors. A program is what you give the audience.”

<hemanth> and indeed, when it come to pure unix based stuff, i would feel blank if i not quote, "UNIX is very simple. But It just needs a genius to understand its simplicity"---  Dennis Ritchie.

<hemanth> So where do we see scripting ?

<hemanth> Job control languages and shells

<hemanth> GUI Scripting

<hemanth> Application-specific languages use them

<hemanth> Even Web browsers!

<hemanth> Text processing languages, indeed are very very useful

<hemanth> General-purpose dynamic languages

<hemanth> Extension/embeddable languages

<hemanth> even some the major market analysis happens with scripts

<hemanth> sever deployments and more

<hemanth> so almost in all the areas its useful

<hemanth> as they say. "If you have a hammer, every problem looks like a nail" !

<hobs> :)

<jmuniz> haha

<hemanth> So, if you were used to on particular language and are trying to solve, everything with it, it would be a real pain and that not how smart people work

<adam_collado> like duct tape

<hemanth> nice mention, the duct tape, was an accidental invention, if i'm not wrong ;)

<hobs> pretty sure you are right

<adam_collado> i didn't know that

<hemanth> So, now would it not be nice to have many tools, than just a hammer ?

<hobs> yes I would like a tool box please

<hobs> filled with tools

<adam_collado> different kinds of tools

<hobs> yep

<adam_collado> like a hammer, AND duct tape

<redguy> or better yet - a little tool factory

<hobs> whew, all good there

<Laks> :)

<hemanth> yup we can all our tool and make our little but a strong factory for sure :)

<hemanth> A small tool is always powerful, remember hooks law :)

<hemanth> So, its very much clear that, no matter in which field u might be, its always an extra + point

<hobs> yeah scripting will always make some aspect of your life easier unless you live in construction or something

<hemanth> As we know we would be covering most of the powerful and latest scripting languages

<hemanth> before i move on, i request all of you to take a latest pull on the repo's

<hemanth> and those who have not forked to fork them

<hobs> ok one second

<hobs> logging on to github, I just cloned

<hemanth> nice :) i have just improved the root README

<hemanth> So if you still think are Scripting Languages Any Good? You will get to know how good they are as we move on

<hobs> I think we are all excited to learn!

<hemanth> So, any queries so far?

<SourRust> none here

<hobs> nope

<hobs> hardcore forking action

<hemanth> Sound good, let move on

== hemanth changed the topic of #p2pu-webcraft/scripting-101 to: Getting Started!

<hemanth> So , now we get ready

<hemanth> basic prerequisites were mentioned in the main page of the course

<hemanth> hope all of you have read that

<hobs> yep

<SourRust> yes

<jmuniz> Yes

<rzo> yes

<adam_collado> yep

<hemanth> nice, now that you all now what programming languages we will be covering, we need to get our platforms set

<hemanth> Check Bourne-again shell or BASH

<hemanth> for most of them it would be there default shell

<hobs> yep, GNU bash, version 4.0.33(1)-release-(i486-pc-linux-gnu)

<hemanth> Next check for python, most of the GNU/Linux will have it for default

<hemanth> hobs, good can just do echo $BASH_VERSION

<hemanth> Next to check is for perl

<hemanth> and then of-course we are left with ruby

<hobs> any versions we should have, or later than any versions

<hemanth> so you must have all these installed in your system

<jmuniz> got it

<rzo> got it too

<redguy> got all of them

<hobs> got them

<SourRust> same

<lifengjie> got them too

<adam_collado> me too

<hemanth> hobs, have any version, because, later when we come across version specific  issues we will learn more

<hobs> roger

<hemanth> kool, very fast class :)

<hemanth> So, as mentioned yesterday, we will cover each topic in each session, now we have 30 mins left with us, and i think we can start with variables

<hemanth> so if all are ready, we will move on :)

<hobs> ready

<Laks> yes

<jmuniz> rdy

<vibha3bhat> ok

<rzo> ready

<lifengjie> ready

<adam_collado> ready

<SourRust> kk

<redguy> ok, ready

<hemanth> nice :)

 * hemanth knows it was pretty dry theory so far, but can't help, now starts the real fun :)

== hemanth changed the topic of #p2pu-webcraft/scripting-101 to: TOPIC 1 THE VARIABLES

<hemanth> So lets stick for a pattern

<hemanth> say BASH,Python,Ruby and Perl

<hobs> sounds good

<hemanth> for rest of the course we will follow the same

<hemanth> So how do we define a variable ? [ In simple plain English]

<hobs> a variable is an abstraction of some sort of data in memory on a computer

<hobs> thats my plain english definition

<hemanth> something that is likely to vary; something that is subject to variation; "the weather is one variable to be considered"

<hemanth> ^^ that is simple English :)

<hemanth> so anything that varies is a variable

<hobs> rgr

<hemanth> In computer programming, a variable is a facility for storing data. The current value of the variable is the data actually stored in the variable.

<hemanth> So, now, i want all of you to open your terminals

<hobs> done

<hemanth> with BASH

<SourRust> yup

<hemanth> please cd to your clones

<hobs> ok

<SourRust> k

<hemanth> cd to variables dir and cat README

<hemanth> now cd to bash dir

<adam_collado> cd bash

<hemanth> To declare and define a variable in bash it very easy

<hemanth> i use "  " to rep a command or that is the things that you guys have to type from now on, fine?

<adam_collado> that's fine

<SourRust> sure

<hobs> sounds good

<adam_collado> lol i just realized i typed my command into here, no wonder i was still in the same folder xD

<hobs> hehe

<hemanth> :)

<hemanth> ' msg="We love p2pu" '

 * hemanth changed the notation to ' ' rather than " " for better clarity

<hemanth> now ' echo $msg '

<hemanth> Tell me what to you see :)

<SourRust> We love p2pu

<hobs> hobot@localhost:~/classes/scripting101/p2pu/variables/bash$ echo $msg

<hobs> We love p2pu

<hemanth> nice, all are seeing the same?

<Laks> yes

<rzo> yep

<hemanth> so the general syntax is VARNAME="value"

<hemanth> they can be String variables

<hemanth> Integer variables

<hemanth> Constant variables

<hemanth> Array variables

<hemanth> String variables => Collection of character

<hemanth> Integer variables => Numbers

<hemanth> Constant variables => Sounds more like oxymoron, but normally they are just refereed as constants

<hemanth> ' echo $BASH_VERSION '

<hemanth> The version number of the current instance of Bash.

<hemanth> BASH_VERSION is a variable

<hemanth> a $ is used to access its value

<hemanth> for the fun of it

<hemanth> try ' echo $RANDOM '

<hemanth> ' echo $RANDOM '

<hobs> 17475

<SourRust> 12784

<hemanth> each time you echo you get a random number :)

<hemanth> Each time this parameter is referenced, a random integer between 0 and 32767 is generated. Assigning a value to this variable seeds the random number generator.

<hemanth> ' printenv '

<hemanth> or just ' env '

 * hemanth Please don't paste the o/p here :)

<hobs> lol

<SourRust> xP

<hemanth> The env or printenv commands can be used to display environment variables.

<hemanth> we can use export VARNAME="value"

<hemanth> to make variables, accessible  form all sessions of the terminal

<hemanth> set and unset can also be used to set and unset the values to the variables

<hemanth> we have shell variables like $$ $? $_ $* $@ $#

<hemanth> we will get to know more as we use them

<hemanth> if all is clear, we can move on to Python

<hemanth> any doubts ?

<SourRust> nah

<hobs> so far so good

<hemanth> now just cat > TODO in your pwd

<hemanth> and write down saying

<hemanth> need to push a simple file with #!/bin/bash the interpretive line as the first line, which all types of variable declarations

<hemanth> Done?

<adam_collado> yep

<SourRust> yea

<hobs> yep

<hemanth> If i were to be on the other end i would have asked a question now ;) How do i define constant variables ;) ?

<hemanth> readonly varName=value

<hemanth> is how we define and declare them

<hemanth> so the file you will be pushing will have all types of variables

<hemanth> try to modify constant data

<hemanth> Do let me, if i'm fast or slow

<hemanth> you can rise your hand as  o/

<hemanth> or \o

<hemanth> \o/ will also do ;)

<adam_collado> lol

<hobs> I tried setting it via set and the constants didnt change

<hobs> as expected

<hemanth> nice :)

<hemanth> now cd ../python

<hemanth> Hope you all wont mind, putting some extra time

<hemanth> after all we are doing variables here ;)

<hemanth> So, if all is fine lets move on

<hemanth> now as you all are in python dir

<hemanth> just run your shell, by saying ' python '

<hemanth> >>>

<hobs> alright

<rzo> done

<hemanth> is what you must be seeing

<phoenixCF> done

<SourRust> k

<lifengjie> k

<hemanth> >>> msg="We all love p2pu"

<hemanth> >>> msg

 * hemanth  >>> here is what i use to say what you must type in there

<hobs> right

<hemanth> as its a interpretive shell, when u say msg, you get the variables value

<hobs> 'we all love p2pu'

<hemanth> but when it's in a script, we need to say print msg

<hemanth> so...

<hemanth> >>> print msg

<hobs> it removes the ''

<hemanth> >>> msg='We all love p2pu'

<hemanth> >>> type(msg)

<hemanth> <type 'str'>

<hemanth> >>> red = 5

<hemanth> >>> blue = 10

<hemanth> >>> print (red, blue)

<hobs> thats a really nice printing function

<hemanth> >>> print ('I am a single quoted string')

<hemanth> >>> print ("I am a double quoted string")

<hemanth> what do u see?

<Laks> no difference

<hobs> yep, except for the words single and double

<hemanth> good :)

<hemanth> >>> print ("So I said, What\"do you see?\"")

<phoenixCF> string with double quoted

<hemanth> now what "do you see" ? ;)

<Laks> " visible in the  output

<hobs> So I said, What"do you see?"

<phoenixCF> backslash escape

<hemanth> >>> print (' You gotcha try this, won\'t you ?')

<hemanth> phoenixCF, correct

<hemanth> these are called escape sequences

<hobs>  You gotcha try this, won't you ?

<hemanth> now can anyone tell me, how do i print a \ ?

<lifengjie> \\

<hemanth> lifengjie, correct :)

 * hemanth super class this is :)

<lifengjie> print '\\'

<hemanth> now how do i print " ?

<SourRust> \"

<hemanth> try :)

<hobs> >>> print "\""

<hobs> "

<rzo> error

<hemanth> rzo, error?

<hobs> print '"'

<rzo> with this one >>> print(\")

<hemanth> >>> print "\""

<hobs> I think you used an escape sequence in not a string rzo

<hemanth> is or as hobs said

<hemanth> >>> print ("""The double quotation mark (") is used to indicate direct quotations.""")

<hemanth> >>> print ("echo, " * 10)

<hobs> echo, echo, echo, echo, echo, echo, echo, echo, echo, echo,

<hemanth> nice

<hemanth> same things can be done with numbers

<hemanth> >>> print (5 * 10)

<hemanth> print ('5' * 10)

<hobs> o/

<hemanth> notice the difference ?

<hemanth> hobs, yes, tell me

<hobs> what if I want to print 50 5 times? (the result of 10 x 5)

<hemanth> >>> print ('50' * 5)

<hobs> right

<hobs> I mean to say

<hobs> how to I print

<hobs> print (5*10)*5

<hobs> sort of

<hobs> so I evaluate and then print

<hobs> basically

<SourRust> print str(5*10)*5

<hobs> oh cool convert it

<hobs> ok

<hobs> thanks SourRust

<SourRust> yup

<hemanth> yes, that right, to make it more clear

<hemanth> >>> type(5)

<hemanth> >>> type('5')

<hemanth> what do u notice ?

<SourRust> one is an int and other str

<hemanth> unless BASH, here its strictly typed

<hemanth> *unlike BASH

<hobs> I will keep that in mind

<hemanth> >>> msg1="We love"

<hemanth> >>> msg2="p2pu"

<hemanth> >>> msg = msg1 + mgs2

<hemanth> >>> msg

<hobs> msg2*

<hemanth> ya **msg2

<hobs> 'We lovep2pu'

<hemanth> NameError: name 'mgs2' is not defined

<hemanth> i wanted u to noticed that

<hobs> you got us

<adam_collado> nice one xD

<hemanth> ;)

<hemanth> in BASH it would have be empty

<hemanth> okies, now in python folder also touch a file as you done in BASH

<hobs> any specific name?

<hemanth> TODO will do ;)

<hemanth> cos its for your reference

<hobs> alright then I will issue 'rm hemanth_told_me_to'

== hemanth [~hemanth@122.167.86.110]

==  realname : hemanth

==  channels : #p2pu-webcraft/scripting-101

==  server   : barjavel.freenode.net [Paris, FR]

==  account  : hemanth

== End of WHOIS

<hemanth> Please note : The pull requests must be only for the source code files and not your TODO's :)

<hemanth> hobs, rm as in remove ;) ?

<hobs> yep

<hemanth> Now tells move on to ruby

<hemanth> hope you all have irb

<hobs> yeap

<hemanth> irb => interactive ruby

<rzo> got it

<hemanth> if its fine we can do ruby and perl, if not we can do it next class, say +1 to continue and 0 for next class

<hemanth> +1 ;)

<hobs> I am good for either

<Laks> 0

<hobs> so +1

<hobs> or +0

<SourRust> +1

<rzo> +1

<hemanth> its peer to peer here so its ok, say what u feel :) no issues

<adam_collado> 0

<hobs> they also might be getting a drink or something

<hemanth> ;)

<hemanth> so its a tie ?

<hemanth> <SourRust> +1 <rzo> +1

<hemanth> <adam_collado> 0 <Laks> 0

<hemanth> hobs ?

<hemanth> and others?

<hobs> yeah I will go +1

<adam_collado> i have to leave in a few, but i'll read the notes later on today

<hemanth> okies

<adam_collado> hemanth, is the python todo instructions the same as the one we put in bash?

<hemanth> yes

<hemanth> its feels, we better give it a break here

<hemanth> and see how the code is pushed to python and bash

<hobs> ok

<adam_collado> alright

<hemanth> and then proceed

<hemanth> what say?

<hobs> sounds alright to me

<Laks> yes:)

<rzo> no prob

<hemanth> this happened today cos we had both Why scripting ? and Variables

<hobs> yeah of course

<Laks> yes#

<hemanth> sorry if i have eaten yup your time :(

<adam_collado> it's fine :)

<hemanth> So, for the next class do have irb and psh ready in your systems

<Laks> its alright u had already mentioned  0(n) can exceed  upto 1:30 hr;)

<Laks> ok

<hobs> heh

<hemanth> and do late your time to push the TODO's

<Laks> ok

 * hemanth will be waiting to see your creativity

<hemanth> Laks, nice you rembr that :)

<hobs> psh is installed

<hobs> thanks again hemanth

<hemanth> so If anyone missed do to n/w issues, we are taking a break here, and doing ruby and perl next week, as we ran out of time and we need to see how people push in there creative code to the main branch

<phoenixCF> k

<hemanth> So if all is clear, its hemanth signing off here o/

<hobs> quick question

<hemanth> yes plz

<hobs> do you want us to write anything specific

<hobs> in terms of the main branch

<hobs> or just be messing with stuff

<hobs> or are you saying in the future

<hobs> that you are looking forward to our code

<hemanth> no, anything and everything wrt to variables, but must be a runnable script

<Laks> it would be also good if you could give us a comparison of different  scripting language and which one is best suited  for which scenario

<hobs> ok, thanks hemanth

<hemanth> sure, that will be done; That would be part of the notes plan

<hemanth> Laks, ^^

<Laks> ok:)

<rzo> quick question here, is there any covention to name script files?

<hemanth> rzo, lower case names with extensions

<hemanth> rzo, camel cases rarely used

<hemanth> a '-' might be used

<hemanth> the normal GNU/Linux file naming conventions

<hemanth> variables.bash, variables.py

<hemanth> do use loads of comments

<hemanth> # to comment

<hemanth> use # (hash, pound) to comment

<Laks> when is the next class?

<hemanth> as the poll was done next weekend

<Laks> oh ok

<hemanth> that is 25

<hemanth> if not happy with the timings we can make re-poll

<Laks> fine with me

<Laks> 1 week time for the assignment:)

<hemanth> So, Hacker, have a nice time with experimenting with class 2 TODO's :)

<hemanth> The logs will be up soon, cya next week \o

<adam_collado> cya hemanth!

<rzo> bye

<SourRust> cya

<Laks> thanks cya

<vibha9bhat> bye hemanth

<phoenixCF> bye

<hemanth> Thanks everyone for making class 2 uber fun :)

<hemanth> logs will be up in mins

<redguy> thanks hemanth

<redguy> bya all