This is an old revision of the document!
Storm Hello everyone
Storm just 3 minutes to go
Mike Cool storm, I am ready when you are. I have Vinux booted in my VM.
Storm Ok, time to get rolling I guess.
Storm So last time we left off after learning about the arguments that can be passed into scripts $1 through $9
Storm I would just like to add a couple more things to that.
Storm $0 is the name of the script itself. so, if you were to open our hello.sh file from last time. It should be in your intro-to-bash directory. Storm add echo "$0" just before the exit 0 line
Storm and then save and execute it with:
You will get
Mike I see.
Storm the reason it only says hello on the first line is because no arguments got passed to it.
Storm The other thing I wanted to mension is $@
Storm It refers to all arguments passed to the program.
Mike What would be a good example of when to use that?
Storm remember how we had to pass arguments with spaces inside quotes? And how we are limited by $1 through $9?
Mike I remember seeing that from when I viewed the log.
Storm This takes away that limit. You can have as many arguments as you want.
Mike I see.
Storm If you change all the variables $1, $2, $3, etc to $@
Storm in hello.sh and save it you can test this for yourself.
Storm do something like
./hello.sh it is a very beautiful day and I am glad to share it with you
Storm no quotes required.
Storm but you should get the whole message even though there are spaces.
Mike We should probably create a place to post pass source code for people who did not receive it, since copying from the logs can be a bit tedious.
Mike I will need to play catch-up, for example.
Storm This comes in even more useful when we start getting in to arrays.
Storm Mike: sounds like a good idea.
Storm And now it's time for redirection.
Storm Did you know that Linux has a black hole?
Mike No, what do you mean by black hole?
Storm You can use a thing called redirection to send text/output to different places.
Storm if you type
Storm you will get the output hello on the screen.
Storm now try instead
echo "hello world" > /dev/null
Storm /dev/null is the black hole I was talking about. Anything redirected to /dev/null will vanish without a trace.
echo "hello world" > /dev/null
Storm does nothing except put you back at your prompt
Mike Then what is the main purpose of using /dev/nul?
Burt so I change null for somewhere, and I don't have that hole in my pocket anymore?>?
Storm It is for things like redirecting text you don't want to see, useful in cron jobs, etc
Storm /dev/null is the perfect place to send say an X wife or something lol
Storm there, a little shameless advertising.
Storm You can also use redirection for sending output to a file.
Storm If you are back to your prompt in your intro-to-bash directory do the following
echo "I love Intro to Bash" > test.txt
Mike I need to create my intro-to-bash. mkdir intro-to-bash, correct?
Mike I can do that from my home dir?
Storm yes, in your home dir do:
Storm then to get to it
Storm when you have completed the echo command you can view the contents of the file by typing:
Storm did everyone get the I love Intro to Bash text?
Storm And now it's time for a fun little command.
Storm You have seen that
Storm shows you the contents of the test.txt file
Storm What do you think this will do instead?
Mike Probably get input from the file?
Storm So, if you do 1 > file name it overwrites any file, or creates it if it doesn't exist
Storm but what if you want to append?
Storm It's easy, just 2 > simbles.
echo "this is an example of appending test." >> test.txt
Storm then do
Storm and after that try
Mike It doesn't show you the file backwards, but shows you lines in reverse order. I thought you meant totally backwards as in right to left.
Storm yes, it is bottom line first
Mike So basically it shows you the lines bottom to top.
Storm yes,the next part is getting redirection from a file.
Jeff is it possible to enter each word on a new line
Storm cat and tac are text viewers.
Storm jeff: It is in 2 ways
Storm You could do
echo "word" echo "one" echo "word" echo "two"
Storm or you can use \n which is the newline character.
Jeff which could then be output in reverse using tac
Storm yes, and also in C, c++, PHP, and probably a lot of others
Storm When patching a file to test new code you use it.
Storm It goes something like this:
patch -p0 < my-patch.txt
Storm and the patch program takes the text from the mypatch.txt file and applies it to the program, then you compile and use it and test the patch.
Storm now, for pipes. Pipes are sometimes confused with redirection.
Storm redirection is sending text or output to a file.
Storm pipes is sending information text or what ever through another program for processing.
Storm this is a bit of an advanced thing to show you right now but it is a great example of piping.
Storm someone could probably do a whole class on sed and regexp.
Storm Lets say we have the command
echo "hello world"
Storm but we wanna change world to 3 headed chicken.
Storm we could just type 3 headed chicken, but what's the fun in that?
Storm especially when we have pipes lying around just waiting for us to use them.
echo "hello world" | sed 's/world/3 headed chicken/'
Storm the | is the pipe symbol.
Storm so, we echoed "hello world" through a pipe into the sed program which changed the word world into 3 headed chicken thus giving us: Hello 3 headed chicken.
Storm So now, it's time for our first really useful program, in fact, I have this one on my system now.
Storm You will need the program hunspell and hunspell-en-us
Storm so, if you don't think you have it, and you would like to install it for this do:
sudo apt-get update && sudo apt-get install hunspell hunspell-en-us
Storm but first let me explain how hunspell works.
Storm You can pipe a word to it for it to check.
echo "green" | hunspell
Storm because green is spelled correctly you will get * printed to your screen.
Storm if it was spelled wrong you would get & and some suggestions
Storm I just took away the need to echo and pipe
Storm I basically just changed the syntax.
Storm if you mispell the word green like greeiin, you would get this:
& griieen 6 0: grieving, griever, Griffie, grievance, grinning, grinder
Storm Now, here's the code for my program. You have enough knowledge to write this yourself, so you should be able to tell me what it does.
#!/bin/bash echo "$@" | hunspell exit 0
Storm I call the file spellcheck
Storm what does the second line do?
Mike The second line uses echo and allows you to pass as many words as you want to the script.
Mike Also, it pipes the arguments through hunspell Using $@..
Storm which is the list of all arguments passed to the script.
Mike The third line simply exits from the script.
Storm If you change permissions on the spellcheck file to 755 and move it to /usr/local/bin you can use it system wide
Storm it's nice to be able to open terminal and spellcheck things.
Storm In next class we are going to cover the while loop and variables
Storm thanks for coming everyone