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

Scripting 101

Chat log Class 5

hemanth hm's picture
Sat, 2010-10-02 18:30

* hemanth has changed the topic to: LOOPS
<hemanth> hey hobs  Laks
<Laks> hello
<hemanth> hobs, anything special out there in US? Seeing very less guys today
<hemanth> Laks, hey wasup in UK?
* hemanth wonders if its a long weekend or something
<Laks> gloomy weather
<Laks> its  raining
<hemanth> ha, ok
<hemanth> not seeing anyone, are they thinking to read from the logs ;)
<hemanth> lets wait for few mins?
<Laks> yes
* SourRust (473b12d5@gateway/web/freenode/ip.71.59.18.213) has joined #p2pu-webcraft/scripting-101
* Laks has quit (Quit: Page closed)
<hemanth> SourRust, hey
<SourRust> hey]
* Lakshmi (516258e7@gateway/web/freenode/ip.81.98.88.231) has joined #p2pu-webcraft/scripting-101
<hemanth> there was a mass disconnection
<hemanth> so waiting for people to join in
<SourRust> o ok
<hemanth> there is a very interesting thing, i'm waiting to share
<hemanth> Okies lets start
* hemanth has changed the topic to: LOOPS
<hemanth> Hello hackers
<SourRust> hey
<Lakshmi> hi
<hemanth> in past few days, i meet with my old bash hackers and with there help i have set a bash bot for this channel, working on it to make it work for pl,rb and py; its a very tough nut to crack
<hemanth> let me demonstrate, how it works
<hemanth> # echo Hello All :)
<bashboy> hemanth: bash: syntax error near unexpected token `)'
<hemanth> # echo "Hello All :)"
<bashboy> hemanth: Hello All :)
<hemanth> saw that?
<hemanth> bashboy is our helper
<hemanth> is does everything that bash does for us ;)
<hemanth> # uname -a
<SourRust> lulz awsome
<bashboy> hemanth: Linux bunnies 2.6.27.6 #9 Sat Nov 15 16:44:53 CET 2008 i686 Pentium II (Klamath) GenuineIntel GNU/Linux
<Lakshmi> wow  cool
<hemanth> Lakshmi, SourRust, hobs  you guys try few commands :)
<hemanth> # echo try it
<bashboy> hemanth: try it
<SourRust> # echo *
<bashboy> SourRust: *
<Lakshmi> # echo   "Bash Boy"
<bashboy> Lakshmi: Bash Boy
<Lakshmi> lol  its  super  cool
<hemanth> SourRust, that was a nice try :)
<SourRust> xD
<hemanth> Lakshmi, want to try more ;)
<Lakshmi> initially i thought bashboy is a new student to this class
<Lakshmi> :)
* hemanth would have tried some system commands :D
<hemanth> ya bashboy is a good boy :)
<hemanth> # shutdown now
<bashboy> hemanth: shutdown: warning: cannot open /var/run/shutdown.pid
<bashboy> hemanth: shutdown: /dev/initctl: No such file or directory
<bashboy> hemanth: etc... ( http://pastebin.com/3HCKqpjC )
<hemanth> :D
<SourRust> pretty sweet
<Lakshmi> cal
<Lakshmi> #cal
<hemanth> so hopefully by next week bashboy would be all by :)
<hemanth> Lakshmi, there must be a space
<Lakshmi> # cal
<bashboy> Lakshmi: bash: cal: command not found
<hemanth> i have not added to binary of cal
<Lakshmi> # date
<bashboy> Lakshmi: Sat Oct  2 15:41:04 UTC 2010
<hemanth> # date -u
<bashboy> hemanth: Sat Oct  2 15:41:10 UTC 2010
<Lakshmi> oh ok
<hemanth> # who
<bashboy> hemanth: no output
<Lakshmi> # mkdir script
<bashboy> Lakshmi: no output
<hemanth> # whoami
<bashboy> hemanth: root
<Lakshmi> #  whoami
<hemanth> # cd script
<bashboy> Lakshmi: root
<bashboy> hemanth: bash: cd: script: No such file or directory
<Lakshmi> # whereis
<bashboy> Lakshmi: bash: whereis: command not found
<hemanth> ok!
<Lakshmi> #  vmstat
<bashboy> Lakshmi: bash: vmstat: command not found
<hemanth> enuf of games
<hemanth> http://pastebin.com/fvv0pYtW is the list
<hemanth> is the list of commands you can run as of now
<hemanth> fine?
<Lakshmi> ok
<hemanth> to make things faster and easier in class
<hemanth> i'm gona give general syntax of each language now
<hemanth> LOOPS
<Lakshmi> ok
<hemanth> The while loop
<hemanth> while c; do ...; done -> BASH
<hemanth> while (c) -> Perl and Ruby
<hemanth> while c: -> python
<Lakshmi> ok
<hemanth> The FOR loop
<hemanth> for i in `seq 10 -1 1`; do ...; done -> BASH
<hemanth> # Not it works with GNU machines, as some wont have "seq" in them
<bashboy> hemanth: bash: Not: command not found
<hemanth> ha bashboy  keep quit
<Lakshmi> :)
<hemanth> for (my $i = 10; $i >= 1; $i--) { ... } -> Perl
* bashboy has quit (Remote host closed the connection)
<hemanth> for i in xrange(10, 0, -1) -> Python
<hemanth> 10.downto(1) {|i| ... } -> Ruby
* hemanth we will also see examples :) don worry
<Lakshmi> ok
<Lakshmi> like the  syntax in ruby
<Lakshmi> downto
<hemanth> :) yes ruby has such kool syntax
<hemanth> So lets see examples
<Lakshmi> ok
<hemanth> $ while true
<hemanth>   do echo "Infinite loop"
<hemanth> done
<hemanth> that is a infinite loop in BASH
<hemanth>  /is/in
<hemanth> so to break the loop you need to hit ctrl^c
<hemanth> $ (( i=10 )); while (( i > 0 ))
<hemanth>    > do echo "$i empty cans of beer."     > (( i-- ))     > done
<hemanth> is the text clear?
<hemanth> $(( i=10 )); while (( i > 0 ))
<hemanth> do echo "$i empty cans of beer."
<hemanth> (( i-- )
<hemanth> done
<hemanth> hope that is clear now?
<hemanth> for (( i=10; i > 0; i-- ))
<hemanth> do echo "$i empty cans of beer."
<hemanth> done
<Lakshmi> yes
<hemanth> for i in {10..1}
<hemanth>    do echo "$i empty cans of beer."
<hemanth> done
<hemanth> so if there is no seq command, for i in {10..1} syntax is better for BASH
<hemanth> there is another useful loop construct
<hemanth> until
<hemanth> The syntax of the until command is: until test-commands; do consequent-commands; done
<hemanth> COUNTER=20
<hemanth> until [  $COUNTER -lt 10 ]; do            
<hemanth> echo COUNTER $COUNTER            
<hemanth> let COUNTER-=1        
<hemanth> done
* bashboy (~hemanth@122.167.94.232) has joined #p2pu-webcraft/scripting-101
<hemanth> x=1
<hemanth> while [ $x -le 5 ]
<hemanth> do
<hemanth> echo "Welcome $x times"
<hemanth> x=$(( $x + 1 ))
<hemanth> done
<hemanth> So, it can be derived that any loop can be used, for any kina purpose
<Lakshmi> ok
<hemanth> but sometimes we need to do something first
<hemanth> and then loop
<hemanth> some times just loop
<hemanth> its left to the hackers choice to play with them
<hemanth> all clear?
<Lakshmi> hmmm
<hemanth> LOOPS PYTHON
<hemanth> >>
<hemanth> for a in range(10):
<hemanth>    print a,
* hemanth BEWARE of the indentation
<hemanth> demolist = ['life',42, 'the universe', 6,'and',7,'everything']
<hemanth> for item in demolist:
<hemanth>    print ("The Current item is: %s" % item)
<hemanth> a = ['cat', 'window', 'defenestrate']
<hemanth> for x in a:
<hemanth> ...    print x, len(x)
<hemanth> a, b = 0, 1
<hemanth> ...    while a < n: ...        print a, ...        a, b = b, a+b
<hemanth> a, b = 0, 1
<hemanth> while a < n:
<hemanth> ...        print a
<hemanth> ...        a
<hemanth> ...       b = b, a+b
<hemanth> ...       a, b = 0, 1
<hemanth> sorry about the not so clear text
<hemanth> its the issue with the irc client, me on my vps, could get into my other machine, dude to some issues
<hemanth> can anyone let me what that previous loop does?
<hemanth> a, b = 0, 1
<hemanth>  b = b, a+b
<hemanth> a, b = 0, 1
<SourRust> prints everything inside the list
<SourRust> for the previous one
<hemanth>  b = b, a+b and a, b = 0, 1 in loop printing a
<Lakshmi> b=b,a+b
<Lakshmi> I didn't get this
<hemanth> you gotcha try this!
<hemanth> >>> a=10
<hemanth> >>> b=20
<hemanth> >>> b=b,a+b
<hemanth> >>> a
<hemanth> 10
<hemanth> >>> b
<hemanth> (20, 30)
<hemanth> >>> a=10
<hemanth> >>> b=20
<hemanth> >>> a,b=b,a
<hemanth> >>> a
<hemanth> 20
<hemanth> >>> b
<hemanth> 10
<hemanth> get the magic ;)
<hemanth> a,b=b,a is swapping a and b in python
<SourRust> it puts b and a+b into b as a tuple
<Lakshmi> yes got it
<SourRust> mkay cool
<hemanth> SourRust, yes correct :) its a 'tuple'
<hemanth> now, the  b = b, a+b and a, b = 0, 1
<hemanth> prints a series can you tell me what series :)?
<hemanth> that's what i was expecting, when i asked the o/p
<hemanth> the sequence, starts with 0,1 thats the clue ;)
<hemanth> ....
<hemanth> another clue required?
<Lakshmi> 0,1
<hemanth> yup, then
<hemanth> its in a loop
<hemanth>  b = b, a+b
<hemanth> what you b be now?
<Lakshmi> 1,2
<hemanth>  b = b, a+b
<hemanth> again
<SourRust> (1,1)
<hemanth> the biggest clue F_{n-2} = F_n - F_{n-1}
<SourRust> oh
<SourRust> fibonacci
<hemanth> :)
<hemanth> yup good you both nailed it
<hemanth> Now, RUBY LOOPS
<hemanth> 3.times do
<hemanth>  puts "This will be printed 3 times"
<hemanth> end
<hemanth> names = [ "SourRust", "Lakshmi", "Hobs" ]
<Lakshmi> one second pls
<hemanth> yes
<hemanth> query?
<Lakshmi> no was trying the python one
<Lakshmi> fine now
<Lakshmi> continue
<Lakshmi> thanks
<hemanth> >>> a, b = 0, 1
<hemanth> >>> n=10
<hemanth> >>> while a < n:
<hemanth> ...     print a,
<hemanth> ...     a, b = b, a+b
<hemanth> ...
<hemanth> 0 1 1 2 3 5 8
<hemanth> okies fine?
<Lakshmi> yep got it
<hemanth> back again to ruby
<hemanth> names = [ "SourRust", "Lakshmi", "Hobs" ]
<hemanth> names.each do|n|
<hemanth>  puts "Hello #{n}"
<hemanth> end
<hemanth> each of the name is selected as n
<hemanth> and is printed to the console
<hemanth> i = 0
<hemanth> while i < 10
<hemanth>  i += 1
<hemanth> end
<Lakshmi> yes
<hemanth> puts i
<hemanth> i = 0
<hemanth> until i == 10
<hemanth>  i += 1
<hemanth> end
<hemanth> puts i
<hemanth> so ^^ were until and while loops
<hemanth> 3.times is the uber syntax of ruby
<hemanth> fine?
<SourRust> yup
<hemanth> nice
<hemanth> LOOPS in PERL
<hemanth> for ($count=1; $count<11; $count++){
<hemanth> print "cool\n";
<hemanth> }
<hemanth> print cool 10 times
<hemanth> $count=1;
<hemanth> reseting count ^
<hemanth> while ($count<11){
<hemanth> print "$count\n";
<hemanth> $count++;
<hemanth> }
<hemanth>  @Names = ('SourRust', 'Laks', 'Hobs');
<hemanth>  foreach (@Names) {
<hemanth>   print $_;
<hemanth>  }
<hemanth> sounds good?
<Lakshmi> yes
<SourRust> yyup
* hemanth got to know that class gets boring if its like a recital, so made it crisp and bit fast, so that it would cover whats required in faster time
<hemanth> and people you will have 7 more days left to experiment
<hemanth> do feel free to create threads
<Lakshmi> :)
<hemanth> as most of them have done it already to share there ideas
<Lakshmi> ok
<hemanth> you also make thread
<hemanth> try them
<hemanth> i wont call the git stuff as home work from now...hmm home work duh...
<hemanth> its for the fun of it
<hemanth> small project like this will help you to understand how the free and open source world works
* PaBLoX (c8686260@gateway/web/freenode/ip.200.104.98.96) has joined #p2pu-webcraft/scripting-101
<PaBLoX> Sorry!
<PaBLoX> The class already ended?
<PaBLoX> D:
<hemanth> # Hi PaBLoX
<bashboy> hemanth: bash: Hi: command not found
<hemanth> # echo Hi PaBLoX
<bashboy> hemanth: Hi PaBLoX
<Lakshmi> :)
<hemanth> :D
<PaBLoX> Sorry :(
<hemanth> PaBLoX, no problem, there is bashboy now
<Lakshmi> pablox try #  bash command
<hemanth> class is almost ending
<SourRust> # echo bashboy
<bashboy> SourRust: bashboy
<PaBLoX> # echo cool :)
<bashboy> PaBLoX: bash: syntax error near unexpected token `)'
<PaBLoX> hahaha
<hemanth> # echo "cool :)"
<bashboy> hemanth: cool :)
<Lakshmi> :)
<PaBLoX> ahaha
<hemanth> quoting is very very important in bash
<PaBLoX> really fun :D
<PaBLoX> Yep
<hemanth> hopefully he will learn other languages soon :)
<hemanth> he => <bashboy>
<hemanth> with all of you contributing we can bring in a bashgal soon ;)
<Lakshmi> he=> superboy after he learns all languages
<hemanth> Lakshmi, :D
<PaBLoX> # echo "So I said: \"Bashboy is the coolest bot ever\""
<bashboy> PaBLoX: So I said: "Bashboy is the coolest bot ever"
<hemanth> I'm happy to mention that i got credits from the NY nerds http://is.gd/fwIXy
<hemanth> :)
<Lakshmi> Diaspora cool:) Congrats!
<hemanth> in the "Special thanks goes out to" section
<hemanth> i'm just a speck out there
<PaBLoX> Hey! Congrats
<Lakshmi> hemanth , maco , and drt24 who wrote the epic Ubuntu setup script.
<hemanth> Lakshmi, PaBLoX thanks :)
<hemanth> PaBLoX, i will share the logs in few mins
<hemanth> Lakhemanth> So...EOC for today
<hemanth> please do try them all
<hemanth> cya tomo hackers