This is an old revision of the document!
Storm Hello everyone
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, and add to it:
Storm just before the exit 0 line
Storm and then save and execute it with:
You will get
Storm the reason it only says hello on the first line is because no arguments were passed to it.
Storm The other thing I wanted to mension is $@. It refers to all arguments passed to the program. Remember how we had to pass arguments with spaces inside quotes? And how we are limited by $1 through $9?
Storm This takes away that limit. You can have as many arguments as you want.
Storm If you change all the variables $1, $2, $3, etc to $@ in hello.sh and save it you can test this for yourself. Do something like
./hello.sh it is a very beautiful day and I am glad to share it with you
Storm no quotes required. But you should get the whole message even though there are spaces.
Storm This comes in even more useful when we start getting in to arrays. Now it's time for redirection.
Storm Did you know that Linux has a black hole? You can use a thing called redirection to send text/output to different places. If you type
Storm you will get the output hello on the screen. Instead try:
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. So:
echo "hello world" > /dev/null
Storm does nothing except put you back at your prompt /dev/null is the perfect place to send say an X wife or something lol
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
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