Saturday, December 24, 2011

Bash Lesson one - Learn the Linux command line

Bourne Again SHell, or Bash, is the shell most commonly used on Linux systems. Bash can be installed on Windows, BSD, and other variants of Unix. Most of the functionality that Bash provides really isn't bash at all, but external programs. Some examples are sudo, vi, emacs, and espeak, just to name a few. To complete this lesson you will need either vi or emacs and sudo. Vi is run from the command line like emacs, but emacs also has a graphical mode that will boot when you launch it from a terminal. Make sure that you are in the sudoers file.

Let's get started. Open up a new terminal (xterm, terminator, yakakua, etc) and type "cd /" and press enter. Cd changes directory, so you just moved from home (or whatever location you booted the terminal from) to the root directory. Now, without the next command cd isn't much good since you would be flying blind. Type "ls". This will show you everything in your current folder in a color coded fashion.

Now, we are going to try something a bit more difficult. While in this directory type "cd bin" and press enter. You can type ls in here if you want, but be prepared because this is a huge folder. While in this folder type "sudo vi "myScript.sh"" Be sure to include the quotes even though they are not needed.

This will open Vim, VI improved.Hit i on the keyboard to enter the text insertion mode. Now type the following in the file just like I have it structured.
#! /bin/bash
cd /
mkdir success
I will explain mkdir a bit later. To save the file from vi hit the escape button once followed by a capital 'Z' twice. (ex: [esc] ZZ.)This will close vi and save the file. If you were choosing to use emacs you can go to file>save or use the command C-x C-s. Now you should be back at the command prompt. This is probably the step that gets messed up the most. Now type "chmod +x myScript.sh" This just makes the shell script (which is what you made) executable.

We are now ready to run the script, but what do you think it will do? The two commands in it were cd / which you already know and mkdir success. What this does is changes directory to root and makes a directory called success.

To test this out, type "sudo myScript.sh". Running it with administrative privileges is a must because of where it makes the folder. Now, type "cd /" again and "ls". If you have done everything correctly you should see blue letters that say "success" on them.

BASH gets much more confusing than this, but for now it's a good start.




The same exercise as a non-root user
This is the same exercise from above but I will use your home directory instead. If you have already read parts of it, you can skip over right to the commands.


Let's get started. Open up a new terminal (xterm, terminator, yakakua, etc) and type "cd" and press enter. Cd changes directory, so you just moved from home (or whatever location you booted the terminal from) to the home directory. That is useful if you get lost and need to get back to a checkpoint. Now, without the next command cd isn't much good since you would be flying blind. Type "ls". This will show you everything in your current folder in a color coded fashion.

Now, we are going to try something a bit more difficult. While in this folder type "sudo vi "myScript.sh"" Be sure to include the quotes even though they are not needed.


This will open Vim, VI improved.Hit i on the keyboard to enter the text insertion mode. Now type the following in the file just like I have it structured.
#! /bin/bash
cd
mkdir success
I will explain mkdir a bit later. To save the file from vi hit the escape button once followed by a capital 'Z' twice. (ex: [esc] ZZ.)This will close vi and save the file. If you were choosing to use emacs you can go to file>save or use the command C-x C-s. Now you should be back at the command prompt. This is probably the step that gets messed up the most. Now type "chmod +x myScript.sh" This just makes the shell script (which is what you made) executable.


We are now ready to run the script, but what do you think it will do? The two commands in it were cd which you already know and mkdir success. What this does is changes directory to home and makes a directory called success.


To test this out, type "myScript.sh". Now, type "cd" again and "ls". If you have done everything correctly you should see blue letters that say "success" on them.

No comments:

Post a Comment