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

Scripting 101

Chat log Class 6

hemanth hm's picture
Sun, 2010-10-03 18:48

* hemanth has changed the topic to: REDIRECTIONS
<hemanth> Input
<hemanth> Output
<hemanth> Error
<hemanth> Are the most common factors that we see in any computer languages
<hemanth> And we all try to improve on Input and Outputs and try to lessen the errors
<hemanth> there was a joke, "A cigarette box read, Warning: Injurious to health"
<hemanth> and a coder smoking it said
* vibha6bhat (75c0c342@gateway/web/freenode/ip.117.192.195.66) has joined #p2pu-webcraft/scripting-101
* lakshmi_ (516258e7@gateway/web/freenode/ip.81.98.88.231) has joined #p2pu-webcraft/scripting-101
<hemanth> we don't worry about warnings, its only the errors that matter
<hemanth> :)
* Lakshmi has quit (Quit: Page closed)
<adamcollado> lol
<hemanth> So with this, lets see what redirections are
<hemanth> redirection is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations.
* SourRust (473b12d5@gateway/web/freenode/ip.71.59.18.213) has joined #p2pu-webcraft/scripting-101
* hemanth has changed the topic to: Redirections in BASH
<SourRust> little late but im here
<hemanth> Its ok, nothing much happened so far
<SourRust> kk
<hemanth> you are here in the right time
<hemanth> file descriptors 0, 1 and 2 are the things we need to remember here
<hemanth> stdin,stdout,stderr
<hemanth> respectively
<hemanth> input stream
<hemanth> output stream
<hemanth> error output stream
<hemanth> So, we all know the I/O devices right
<SourRust> yea pretty much
<adamcollado> yeah
<hemanth> When hemanth was asked by his teacher name an Input device
<adamcollado> either that or i've been wrong for many years D:
<hemanth> he said monitor
<adamcollado> i've been guilty of that as well xD
<hemanth> the teacher said, i asked from input device, hero!
<vibha6bhat> :D
<hemanth> I said, yes its a touch screen :P
<adamcollado> oh snap
<hemanth> so, the game is play with these streams
<hemanth> few symbols that we need to know
<hemanth> and we would have used it so far
<hemanth> >
<hemanth> <
<hemanth> >>
<hemanth> <<<
<hemanth> > is redirecting o/p
<hemanth> < is redirecting i/p
<hemanth> >> appending redirections
<hemanth> >>> ; <<< are called here strings in BASH
<hemanth> we will see how it all works
<hemanth> i don't want to hurry yup with this today, unlike if else and loops; this is much more hard nut
<hemanth> and requires clear understanding
<adamcollado> yep
<hemanth> one of the most import things to know is about /dev/null
<hemanth> i call it the black hole
<hemanth> any guesses why i called it so?
<adamcollado> because everything that gets redirected to it
<adamcollado> disappears
<adamcollado> forever
<adamcollado> and ever?
<hemanth> yup :)
<hemanth> its a special file that discards all data written to it
<hemanth> so assuming that you have a file called test file
<hemanth> which has some data in it
<hemanth> cat test.txt 2>/dev/null
<hemanth> can anyone translate that to human language ?
<hemanth> what is it doing?
<adamcollado> um
<adamcollado> redirecting the output of test.txt to /dev/null?
<vibha6bhat> copy
<hemanth> it redirects its error messages (stderr) to /dev/null
<hemanth>  2>/dev/null
<adamcollado> ahh, hence the 2>
<adamcollado> okay
<hemanth> the *2* must be noted there
<hemanth> vibha6bhat, no it does not copy, /dev/null discards every thing
<hemanth> even cp test.txt /dev/null
<hemanth> but are u getting the doubt, why at all we need such a black hole?
<hemanth> no?
<adamcollado> it's an easy way to get rid of messages?
<hemanth> yup one of the main uses
<adamcollado> i just thought that because of the previous example you used
<adamcollado> with the error messages being tossed into /dev/null
<hemanth> like say echo "An Error" 1>&2
<hemanth> what does this do?
<adamcollado> it redirects "An Error" from stdin to stderr?
<hemanth> redirects its normal output (stdout) to the standard error target
<adamcollado> ah
<hemanth> 1>&2
<hemanth> noted the syntax
<hemanth> fd>&fd
<hemanth> where fd is the file descriptor
<hemanth> one of the examples i can quote is
<hemanth> checking if the current user has sudo privileges or not?
<hemanth> we could do sudo -v
<hemanth> that would bang, if the user doesn't have the perms
<hemanth> so we redirect the err to /dev/null and then check the exist status
<hemanth> this a small experiment, that you must all try as you push your codes
<hemanth> any doubts?
<SourRust> nope
<hemanth> okies
<hemanth> now lets see more redirections
<hemanth> touch 1.txt 2.txt 3.txt
* PaBLoX (c8686260@gateway/web/freenode/ip.200.104.98.96) has joined #p2pu-webcraft/scripting-101
<PaBLoX> hi =)
<hemanth> hi
* hemanth sends the chat logs to PaBLoX
<hemanth> cat 1.txt 2.txt 3.txt > 4.txt
<hemanth> puts all the data in 1,2,3 to 4
<hemanth> now, cat 5.txt > 4.txt
<hemanth> what is the current content of 4.txt??
* lakshmi_ has quit (Quit: Page closed)
* Lakshmi (516258e7@gateway/web/freenode/ip.81.98.88.231) has joined #p2pu-webcraft/scripting-101
<hemanth> getting it?
* hemanth echo echo
<adamcollado> what was in 5
<adamcollado> replaced everything in 4?
<hemanth> 5.txt is some file with contents
<adamcollado> wait, my sentence is messed up xD
<adamcollado> I mean to say "whatever was in 5, replaced everything in 4"
<hemanth> exactly
<hemanth> :)
<hemanth> now if we want to append 4.txt ( i.e the file which has the contents of {1,2,3}.txt) with contents of 5.txt
<hemanth> we do
<hemanth> cat 5.txt >> 4.txt
<hemanth> mv 4.txt all.txt ;)
<hemanth> cat all.txt
<hemanth> will now show the contents of {1,2,3,4,5}.txt
<hemanth> get it :)?
<SourRust> yup
<Lakshmi> yes
<hemanth> To append the cumulative redirection of stdout and stderr to a file
<hemanth> >> FILE 2>&1
<hemanth> is the way we do it
<hemanth> &>> FILE can also be done, provided you are on bash version 4
<hemanth> cat <<EOF
<hemanth> i'm test how to print a huge
<hemanth> chunk
<hemanth> of test
<hemanth> without putting it a file
<hemanth> and then cating it
<hemanth> EOF
<hemanth> try that :)
<hemanth> done?
<hemanth> The redirection-operator << is used together with a tag, here its EOF
<hemanth> it can be anything
<hemanth> try something like
<hemanth> cat <<EOF
<hemanth> My home is $HOME
<hemanth> EOF
<adamcollado> it gives me a warning when i put in cat <<EOF
<hemanth> as?
<adamcollado> bash: warning: here-document at line 45 delimited by end-of-file (wanted `EO')
<PaBLoX> Weird, I'm not getting any error/warning
<adamcollado> well
<adamcollado> it gave me a > prompt
<adamcollado> first
<adamcollado> maybe i'm doing it wrong xD
<hemanth> cat <<EOF
<hemanth> My home is $HOME
<hemanth> EOF
<hemanth> try that :)
<adamcollado> okay
<adamcollado> that worked :D
<adamcollado> it printed out all the text i was typing until I typed EOF
<hemanth> adamcollado, what was that you tried first
<hemanth> exactly!
<adamcollado> i just tried the command first
<adamcollado> then hit ctrl-D
<hemanth> thats what the here document does
<hemanth> :)
<hemanth> its not ctrl-D, but the tag, EOF here
<adamcollado> i see
<hemanth> can be anything
<adamcollado> interesting, i didn't know about that
<hemanth> cat <<adam
<hemanth> hello
<hemanth> hi
<hemanth> how are you?
<hemanth> adam
<hemanth> try that ;)
<PaBLoX> Hm, I can't imagine how that could be useful...
<hemanth> PaBLoX, good you asked it
<PaBLoX> Can you think an example?
<hemanth> its very useful
<hemanth> when giving help about a method to a user
<hemanth> say you need to tell the user something like
<hemanth> use the script as so and so
<hemanth> with such a syntax
<hemanth> and these are the options
<hemanth> with tabbed spaces and indentations
<hemanth> inline
<hemanth> in a code, then you can use this to make thinges better, simpler and easier
<hemanth> or else you must end up making a help file and then cating it
<Lakshmi> thats a very good example
<hemanth> hmm, its one of the main uses
<hemanth> lets make it more clear
<hemanth> cat << EOM
<hemanth> $HOME is the variable where the path to ur home is stored
<hemanth> EOM
<hemanth> cat << "EOM"
<hemanth> $HOME is the variable where the path to ur home is stored
<hemanth> EOM
<hemanth> PaBLoX, try that and tell me the outputs :)
<hemanth> all of must try that
<vibha6bhat> done
<hemanth> PaBLoX, has raised a very valid question
<hemanth> vibha6bhat, see the diff in the o/p
<vibha6bhat> yes
<hemanth> can you explain and bit on what you say
<hemanth> * on what you saw
<vibha6bhat> in the first case $HOMe 's value was printed
<vibha6bhat> in the second case it is printed as $HOME. Its value is not printed
<hemanth> indeed, that must make things clear why and how to use the here document :)
<hemanth> queries?
<hemanth> o/ if any
<hemanth> so all clear?
<Lakshmi> yes
<nmort_> o/
<hemanth> nmort_, yes please
<nmort_> does EOM stand for something?
<hemanth> its just a tag, EOM here meant End Of Message
<nmort_> ah ok
<hemanth> you can use any tag which you want
<hemanth> like the here document we have something called the Here strings
<hemanth> <<<
<hemanth> bc <<< 3+4
<hemanth> try that :)
<hemanth> BTW bc is an arbitrary precision calculator language
<hemanth> bc <<< 3*4
<hemanth> What do you notice?
<nmort_> 12
<SourRust> prints out the answer for each
<nmort_> but I do not understand the difference between < and <<<
<nmort_> from what I can see in this case <<< is giving input to the  program bc
<hemanth> here string, is used to feed in line data
<hemanth> < is also
<hemanth> but the difference is cleared with an example
<hemanth> lets see
<hemanth> tr -d "\r" < my.txt
<hemanth> will deleted all the \r from the file my.txt
<hemanth> where as bc <<< 3+4, will take the inputs as 3,+ and 4 and evaluate
<hemanth> bc < 3+4
<hemanth> will bang
<hemanth> bc
<hemanth> 3
<hemanth> will print 3
<hemanth> bc <enter>
<hemanth> 3+4
<hemanth> will print 7
<hemanth> that can be made easy with here document as bc <<< 3+4
<hemanth> even with cat <<< hi
<hemanth> get it?
<nmort_> I think so yes
<hemanth> redirections are hard to get, until we try few
<PaBLoX> yeah, quite hard D:
<hemanth> PaBLoX, even though, hard is a relative term ;)
<adamcollado> so wait, what's actually happening with <<<? is it sending it as a command from stdin?
<PaBLoX> haha, that's true
<hemanth> adamcollado, yes, you got it right
<PaBLoX> btw, here are two very good resources about bash:
<PaBLoX> http://tldp.org/LDP/Bash-Beginners-Guide/html/
<PaBLoX> http://tldp.org/LDP/abs/html/
<PaBLoX> I specially like the second
<hemanth> stdin reads its information straight from the string you put after the <<< operator
<hemanth> with this, i come to an end of redirections in BASH
<hemanth> will see the same in other languages next week
<hemanth> till then try everything
<adamcollado> sweet
<hemanth> and do push the code
<hemanth> i must appreciate SourRust and adamcollado for pushing there codes in time
<SourRust> mkay
<adamcollado> will do
<hemanth> +1 to you both :)
<adamcollado> cool ^^
<hemanth> PaBLoX, thanks for sharing the links with the class, i'm starting a thread to share only the links
<PaBLoX> thx to you for this class :)
<PaBLoX> I'll start pushing code today
<PaBLoX> I'm too late to push all the code that I haven't push today, but I'll try to catch up.
<hemanth> nice
<hemanth> So, if there are no q's i would ETC
<hemanth> ETC => End The Class ;)
<adamcollado> kk
<adamcollado> later guys!
<SourRust> cya
<hemanth> thanks you all
<nmort_> thanks hemanth
* adamcollado has quit (Quit: Page closed)
<PaBLoX> haha
<nmort_> goodbye
<hemanth> cay next week hacker
* SourRust has quit (Quit: Page closed)
<vibha6bhat> Thanks an
<PaBLoX> cyad cya :)