Storm Hello everyone
blindndangerous heya storm
Storm Just 5 minutes to wait for stragglers lol
Bill T hi storm
Storm Ok, so it's 10:00PM EDT, time to get started.
Storm I guess the first order of business is to find out what everyone knows already.
Storm So, does everyone know basic terminal commands, how to navigate directories, create new directories, list the contents of a directory, etc?
Bill T yes
Burt sure enough
Storm Ok then, for our work, let's all create the directory intro-to-bash and change in to it.
Storm Ok, should we start with hello world, or can you all do that as well?
blindndangerous That's just echo hello world right?
Storm yes, from terminal.
Storm everyone do:
echo "hello world"
Storm hello world gets displayed to the screen.
Storm but, to put it into a file actually takes 3 lines of code.
Storm So, if you can open the file hello.sh and then switch back to this window I will show you the lines and explain them.
Storm these are the 3 lines for the hello world program.
#!/bin/bash echo "Hello world" exit 0
Storm When you have the 3 lines save the file and close it.
Storm Ok, now for the explination
Storm Each script that executes in Linux should start with the shabang
Storm that is the line that goes #!/bin/bash
Keith You mean the ! in front of the code?
Storm well it's a number sign and a !
Keith Oh yeah woops.
Storm it's the first line
Storm it tells Linux the path to the interpreter
Storm the next line, echo "hello world", is the actuall program.
Storm It prints the words Hello world to the screen.
Storm the final line exits the program and sends the return code to the OS
Storm A 0 means everything was great and the program was a success.
Storm anything else means there were errors.
Storm You set the code yourself when you exit the program. This, of course, means you have to check for errors.
Storm But, for example, you could have an exit code of 500
Storm and the 500 can be a code for whatever you like, but it's a good idea to write down your error codes so you don't forget what they stand for.
Storm So, when you write really big programs, make a chart for errors and say, for example
500 is the error for when the user didn't say pleas and hop on one leg while facing a full moon.
Storm but seriously, you could write something like 500 is the error for when a file does not exist.
Storm Ok, so now we have the 3 lines of hello.sh explained it's time to execute the program.
Storm You will find, if you type hello.sh that you will get a warning from bash that the file wasn't found and it will go back to your prompt
Storm This is one of the security strengths of Linux. Not every directory is in the execution path.
Storm So, in order for the program to execute you have to tell it in which directory to look.
Storm there are a couple of ways to do this.
Storm First, you could use the whole path name. for example:
Storm That's a lot to type though, and a lot to remember. So instead of doing it that way, you can use the relative path name. to do that, you just use the . for the current directory.
Storm so, to execute using the relative path you would type:
Storm If you now go and type ./hello.sh you may get another surprise.
Storm It goes something like, ./hello.sh: permission denied.
Storm This is another of Linux's security strengths.
Storm when you use Linux you are in control. You are the boss, nothing can run without your permission. It's one thing
that makes it so difficult to write viruses for Linux.
Storm Now we could probably spend a whole class, maybe even 2, on permissions. I don't really understand them as well as I should.
Storm But, there are 2 ways to set them. when I was taking programming classes I learned to use the numeric way
Storm there are 3 numbers in setting permission. The first number is for you personally, the second is for users on your computer, and the third is for all other users, people connecting from the internet etc.
Storm Here are the more common permissions and what they do.
Storm 700 gives you and only you read, write, and execute access.
Storm 600 gives you read and write access
Storm 777 is a big bad evil no no. It gives everyone everywhere read, write, and execute access.
Storm 666 is everyone read, and write access. It's not used all that much as far as I know.
Burt a doubt, the second number is for group, which would not be everyone who uses the computer, but could be; or am I wrong?
Storm Burt: Like I said, I don't really know everything about permissions, but I believe it is for everyone on the computer.
Storm If you are installing a program system wide it uses 755 permission
Storm this is execute access for everyone and read access for everyone too.
CJ The middle number sets the permissions for the other members of the group. for
CJ When you run 'ls' it'll give you the owner and group for each file.
Storm yep, and ls -l shows permissions and everything.
Storm For our hello.sh program though, we want all permissions for ourself and none for anyone else.
Storm To change permission use the chmod command.
Storm blindndangerous: excellent
Storm so, it would be:
chmod 700 hello.sh
blindndangerous Or if numbers aren't your thing you could do.
Storm As a side note, and I am not really prepared for going in to this, I haven't done the research, dealing with groups and ownership is done using the chown command.
CJ I think it's chown for ownership and chgrp for group.
Storm oh, I have never encountered chgrp
CJ Ah, maybe you can do it all with chown then.
blindndangerous I wouldn't touch chown unless you have to
Storm So, putting all this together, we have now set permission to 700 and we can use the relative path to execute the file.
Storm If you do it you should get Hello world.
Bill T Why?
blindndangerous The only time I use chown is when I want to give something to another user. For example, I built an idlerpg bot from source, but I didn't want to be the run to have to run it, so I did chown -R irc:irc idlerpg to give it to the user irc and the group irc.
Storm We still have about 20 minutes left, so I'll do a bit on command line arguments.
Storm If you open your hello.sh file and change the word world inside the quotes to $1 then save and close the file
Storm You can now do something pretty neat with your program.
Storm you can call it with 1 argument.
Storm An argument is something you pass into the program when you call it.
Storm so, for example, we can call it with:
Storm Now, your program outputs the words Hello Fluffy
Storm But, notice what happens when you call it with this argument:
Storm ./hello.sh fluffy the three headed dog
Storm You just get Hello fluffy
Storm Can anyone explain why?
Storm It's because that is really using 5 arguments, not just 1.
Storm arguments are delimited by spaces.
Storm You may be wondering, "But what if my argument has a space in it?"
CJ Use quotation marks?
Jeff can you string them together using - or _
Storm CJ: has the right answer.
./hello.sh "Fluffy the three headed dog"
Storm will output Hello Fluffy the three headed dog
Jeff got it I think
Storm jeff: That will work, but sometimes you want spaces and - _ just won't do lol
Storm What happens though if you are excited because fluffy suddenly became a three headed killer bunny?
CJ and I'm assuming without the quotes, the parameters are $1, $2, $3 and so on?
./hello.sh "Fluffy the three headed killer bunny!"
Storm CJ: Yep, from $1 through $9
Storm CJ: But the quotes are important, I'm not just rambling lol
Storm If you run hello.sh with that argument you will most likely get a weird error.
Storm It's because of the !
Storm So, if your argument has a ! in it you should use 'argument'
CJ Ah, because of the '!' before the trailing quotation mark?
./hello.sh 'Fluffy the three headed killer chicken!???'
Storm CJ: There shouldn't be any quotes in this one
Storm cj oh, I misread
Storm cj yes, the ! causes all the problems if used with quotes, but when used with apostrophy it is ok
CJ Ah, cool.
Storm Using the dollar number method we can accept up to 9 arguments.
Storm so, you can theoretically open your hello.sh and after $1 put a space then $2 $3 $4 etc.
Storm Each argument is delimited with a space, but you can also put each argument inside quotes and add spaces to the individual arguments that way too.
./hello.sh "mr. and mrs." Smith "Expert Assassins"
Storm how many arguments does that line contain?
Bill T 3
Storm Bill T: Awesome
Storm 2 inside quotes and one not
Storm Ok, so we have about 5 minutes left if anyone has questions before we close?
CJ ^5 Bill_T_
Storm Well, if no one has any questions, I think I will call it quits for now. I'll post the next time on Vinux mail list and on @vinuxnews twitter account.
CJ Any hints at what topics will be covered in the next class? in
Storm More command line argument stuff, variables, and if there's time the while loop
Storm We may write a spellchecker
Bill T wow
Storm we will probably discuss redirection and piping.
CJ Cool! Cool!
CJ Wow, I can't type tonight!
Storm bash is one of those languages that can be real powerful real fast
CJ or maybe it's deleting I'm having trouble with.
Storm Thanks for coming everyone, and if you have any questions pleas contact me on the #vinux channel irc.vinuxproject.org
CJ Thanks a lot storm! I'm looking forward to the next class.
Storm thanks :)