Directions |
INTRODUCTION—The numbering below corresponds to “what to type” THE VIRTUAL TERMINAL at the right is a sandbox. A sandbox is a space that may can litter and discard. Long commands may appear in two or three lines. echo 'this is s long command that do not fits a single line, but the END OF LINE goes after the last single quote' |ENTER| But you should type them without pressing enter. The text indicates when you need to press enter. echo "these are:" |ENTER| echo " two different command lines" |ENTER| |
1. ============
I'am
logged in, but what user am I?—In this sandbox you are
root! When BASH starts it sets up a personal environment for you, and declares several variables. CAPITALIZATION IS STRICT in unix: bash ≠ Bash ≠ bAsh ≠ basH ≠ BasH ≠ BASH … you can declare your own variable to store information:
juan="hola
mundo" places hola mundo in a variable named juan The double quotes " " or the single quotes ' ' tell BASH that this is a string of characters i.e. some text. |
2. ============ In UNIX everything looks like a FILE, DIRECTORIES look like files and even the memory looks like a file. When you get access you are hooked to a place in the file tree. When you write a file name, BASH will seek for it at the present location. Where in the file tree am I?—your home folder is at the "root" directory in the main "/" access point of the file hierarchy. The file hierarchy is not limited to ONE DISK like in windows, is spans everything including RAM memory, terminals and internet sockets. HOME is the name of a variable $HOME call its contents. Again, this is called an environment variable, and is placed by the OS in your environment when you are granted access (you log in). |
3. ============ This is supposed to be a shell... Which SHELL is this?—May I ask? SHELL is an environment variable holding the name of the shell in this terminal, in this case “bash”. In UNIX you can open many terminals and each one has its own SHELL, which may or not be the same. “sh”, “bash” and “zsh” form a shell family (similar syntax) “csh” and “tcsh” are a different shell family. “bash” is popular and is an improved version of “sh”. We shall lean more about it. “zsh” is a more recent edition of bash. Most thing we will test here, can be used in zsh, is case you prefer to use that shell instead. |
4. ============ What files are in my space?—what is all this?
It list the files indicated in its arguments. In this case, we gave no arguments, so it listed all the files at the present location.
$PWD, i.e. the current DIRECTORY, is the default argument for ls, it is you want to list. In the answer you have, for each file:
|
5. ============ Now query the same place, but request different format the flag “-s” means size and “-h” means in "human friendly", without arguments, “ls” give us the current location Now only two files are listed!
Remember DIRECTORIES are files and “.” is the name of the current DIRECTORY, while “..” is the name of the DIRECTORY above us. These files are not listed unless you include the flag “-a” But I do have some files alrready—What is in hello.c? |
6. ============ cat is another command—What does cat do? Oh! HOUSTON WE HAVE A … program, and its comments say it can be compiled with "gcc-o hello hello.c". Compiling a program means translating it to machine language, so the computer can run it. To compile it you need a different program called compiler. |
7. ============ gcc is a compiler and requires only one argument, the name of the program to be compiled. The resulting program is stored in a.out, but is you want the program to have a different name, we use the -o flag. The -o flag needs an argument, the name you want to give to the program.
Why
happened?—there is no answer! UNIX answers something when it must. If nothing wrong happened it usually gives no answer. |
8. ============ How do I see if my program was created?—you should figure it out yourself by now
"-rwxr-xr-x" means you can read, write and execute the program, group members, and other users can read and execute, but they cannot write in it. This means the can use it, but they cannot change it. |
9. ============ Let us execute to see what it does… To execute a command, just call its name. BASH will look the program in the DIRECTORIES listed in the environment variable PATH How can I find what is inside PATH, but if you want to execute a program at the present location you need to add “./” to its name, because to call a command you need a PATH.
In
unix programs can only be called as command with a path "./"
means this DIRECTORY, YOU MAY TRY:
|
10. ============ Can we change it?—it is feels pretty powerful to write a computer program... But firs we make a copy, so we keep the original intact, in case of accidents. we need the cp command with two arguments “source file name” and “destination file name” these filename can take a PATH, so you can copy stuff between different DIRECTORIES. Then we need a test editor. vi (for visual) is the default editor in UNIX. Any UNIX system you connect to has vi. so it is a good idea to know the most basic suff of vi. |
11. ============ After typing the second line in the step 10, we are in the text editor vi we called a program and it is running, when it finishes we will go back to the shell, but for now, the SHELL is hidden and vi is paying attention to our commands. vi has modes, you entered to it, in the command mode. If you type something it is interpreted as an action to be performed. MOVING is an action called by the arrows or by pressing the letter keys: h→ j↑ k↓ l← If you type a number before an action it is performed that number of times, so 8j means “move eight spaces forward”. Some actions need a number, for instance “G” means go to a different line. The number means the line you need, but you can use $ to go to the last line. $ means the end of something, for instance the end of the current line. So let us move to "Hello World\n", so type exactly what you are asked here. 1G means goto line 1, 0 means at the beginning, 8j move 8 lines below, 12l mean move 12 places forward. |
12. ============ Now, let's delete the word, this is done with the command "d" and a modifier "w" for word. We need to do it twice, so we type “2dw”. Other modifiers for d are “0” (zero) from the current position to the beginning of line, “$” from here to the end of the line, “dd” will delete the entire line. The text you delete is place in a buffer, so you can place it somewhere else with “p” (from put). Simply move to a different location and press “p”. If instead of deleting you want to copy use “y” (yank). The same modifiers apply. The text “yanked” can be placed somewhere with “p”, just as before. Last, but not least, if you want to undo what you did, use “u” (undo). Next we need to introduce some text... |
13. ============ To write some text we need to enter INSERT MODE. “i” activates insert mode at the cursor. “a” insert mode ahead of cursor (append). “o” open a blank line below and go on insert mode there. “O”, open a blank line above and go on insert mode there. DO NOT TRY ALL THIS for now, just type "i" then the desired text. You may use the backspace key to delete some mistake and retype, but DO NOT PRESS ENTER, if you do so you open a line and continue typing at a different line. While this can be legible to a human will result in a compiler error. Actually, the “\n” inside the quotes is the indication to the compiler to write an END-OF-LINE marker. Now, when you are done press ESC key, to go back to command mode. Remember, if you make something wrong press ESC and try u, to undo you mistake. |
14. ============ Now we save our work. We need to use a composed command, and these start with a colon “:”. After typing : vi moves to the end of the screen and captures the rest of the command, in this case “w” for writing. Then press the |ENTER| key. NOTE: “:w name” will save our file as name without changing the original file. If you want a different name write a blank and the file name afterwards. |
15. ============ We are done, let's go back to BASH. To exit type “:q|ENTER|” or “:x|ENTER|”. The first just leaves, the second saves and exits. If you want to leave without saving use ":q!" The “!” mark tells vi to enforce a command, even if something is wrong, for instance, if you have not saved your changes. |
16. ============ Check out work with ls (you may look a the contents of “hello-bansky.c”, How is that done?) Now we are going to recompile the program, but we shall put it where it can be available even if we change our location. In UNIX, it is common to create a DIRECTORY for each user registered. These are usually created in the “/home” folder. There are also common location for executables: /bin, /usr/bin, /usr/local/bin, are some of them. We shall create a bin folder at /home and place our new executable there. We create DIRECTORIES with “mkdir”, then we shall add this location to the PATH, to force BASH to look for executables there, as well. Then we shall place our compiled program at that location. First we make sure /home exists, and it does Then we create /home/bin Then we compile “hello-bansky.c”, but place the result at “/home/bin/hello”. Notice we shall have two “hello” programs, the old one, which salutes the world, and the new one which salutes Bansky. We then make sure the new program is at the expected location. |
17. ============ Now add /home/bin to the environment variable "PATH". Remember, PATH is the name of the shell variable where BASH searches for executable files. The environment variables have to be modified and exported back to the environment, that is DONE with export. Notice how we reset the PATH contents PATH=$PATH:/home/bin These means place in PATH whatever was there, and add “:/home/bin”. In the PATH, the DIRECTORIES are listed, separated by colon “:”. “export” changes PATH by its new value. now if we call “hello” we get “/home/bin/hello”. If we want to execute the copy at /root/hello, he wee to tell BASH the PATH to the file ./hello
|
CONGRATULATIONS! YOU HAVE REACHED THE END OF THE TUTORIAL. Now try to make a comand that prints your full name, name it me and place it at home/bi, so it can be called from any location. |