{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}{\f1\fmodern\fprq1\fcharset0 Courier New;}} {\colortbl ;\red255\green0\blue0;} \viewkind4\uc1\pard\lang1033\f0\fs20 =========================== Aliens Bash Tutorial ==============================\par -----------------Written by Alien <-> alien@ktv.koping.se----------------------\par ===============================================================================\par \par First you probably need to read a UNIX command bible to really understand this\par tutorial, but I will try to make it as clear as possible, there is about\par 100-150 UNIX commands explained later in this tutorial.\par You are to have some UNIX experance before starting on this tutorial, so\par if you feel that you have UNIX/Linux experance feel free to start to learn\par here.\par What I included here is general shell scripting, most common other things\par and some UNIX commands.\par Here's the most common shell types:\par \par bash = Bourne again shell\par sh = shell\par csh = C shell\par tcsh = Tenex C shell (not tab-completion-extended C shell)\par tclsh = Tcl shell\par ksh = korn shell\par ash = a shell\par bsh = bourne shell ? (in most Linux distributions it's a link to /bin/ash) \par zsh = the Z shell (it's what it's manual page tells about it .. :/ )\par \par ===============================================================================\par 1 - What you already know. (should know)\par ===============================================================================\par \par Here we go, bash scripting is nothing more then combining lots of UNIX commands\par to do things for you, you can even make simple games in bash \par (just UNIX commands) or as in normal cases, batch files to control things\par in your computer.\par \par There is a variety of easyer and harder examples in the beginning of this\par tutorial, I've done it this way to make it easier for people to get the \par general picture, so they will get more of the "aha!" experiences in the later \par chapters of this tutorial.\par \par What bash or any scripting language does is to call for premade programs\par that lives in your computer.\par So when you write a command in a script you are calling a command that is\par a part of the system.\par That is why this tutorial will be just as much a Linux and UNIX tutorial\par as a shell scripting tutorial.\par \par I will however not take up much about The X Windows System in this\par tutorial, for the simple reason that a Window Manager does nothing else\par then display programs.\par This means that a Window Manager is like a graphical shell for the system.\par You can do all in this tutorial from any terminal emulator in a Linux\par Graphical Enviorment (The X Windows System).\par A terminal emulator would be such as: Eterm, xterm, axvt, rxvt, kterm etc.\par A terminal emulator let's you get up a terminal with a command prompt\par in a graphical window.\par \par --------------------------------------------------------------------------------\par \par Shell command separator/control characters:\par \par | = pipe will take the first commands stdout as the second commands stdin.\par || = OR if first command is false, it will take the second.\par |= = OR IS (mostly used in if statements)\par && = AND if first command is true, it will execute the second one.\par ! = NOT (mostly used in if and test statements), but as a shell-command\par it opens a shell to run the command (ex. `! echo foo`)\par != = NOT IS (mostly used in if statements)\par !$ = last commands last argument\par !! = repeat last command\par = = IS (mostly used in if statements)\par ; = will separate 2 commands as if they were written on separate command lines\par ;; = end of a case function in a case statement. (see `case` further down)\par $ = prefix to a variable like "$myvar"\par $$ = PID of current process (PID == Process ID)\par $0 = Shows program that owns the current process.\par $1 = First agument supplied after the program/function on execution.\par $2 = Second agument supplied after the program/function on execution. ($3 etc.)\par $# = Shows the number of arguments.\par $? = Any argument (good to use in `if` statements)\par $- = current option flags (I never ever had to use this one)\par $_ = Last argument/Command\par $* = All arguments\par $@ = All arguments\par # = remmed line, anything on a line after "#" will be overlooked by the script\par \{ = start braces (starts a function)\par \} = end braces (ends a function)\par [ = start bracket (multiple-argument specifiers)\par ] = end bracket (multiple-argument specifiers)\par @ = $@ is equivalent to "$1" "$2" etc. (all arguments)\par * = wild card (* can substitute any number of characters)\par ? = wild card (? can substitute any single character)\par " = quote\par ' = precise quote. (Will even include "'s in the quote)\par ` = command quote. (variable=`ls -la` doing $variable will show the dir llist)\par . = dot will read and execute commands from a file, ( . .bashrc )\par & = and. as suffix to executed file makes it go to the background(./program &)\par 0> = stdin stream director\par 1> = stdout stream director (standard output)\par 2> = stderr stream director (standard error output)\par % = job character, %1 = fg job 1, %2 = fg job 2, etc.\par >> = stream director append to a file\par << = stdin stream director. (cat > file << EOF ; anything ; EOF)\par > = stream director that will start at the top of the file (in if statements < and > may be used as greater-then and lesser-then, as: if [ "$1" >= "2" ])\par \\ = back-slash, takes away any special meaning with a character, \par \\$var will not be treated as a variable. \par (and a new line will not be treated as a new line)\par >& = stream director to stream director, ie. echo "a" 1>/dev/null 2>&1\par this directs 2> to the same place as 1>\par \par --------------------------------------------------------------------------------\par \par Here is the basic UNIX or rather Linux directory structur:\par \par \tab\tab\tab\tab / (system root)\par _______________________________|____________________________________\par | |\tab |\tab | | |\tab |\tab |\tab |\tab | | |\par bin/ boot/ dev/ etc/ home/ lib/ lost+found/ proc/ root/ sbin/ usr/ var/\par | | | | | | | | | | | |\par | | | | | | | | | | | |-> various\par | | | | | | | | | | |\par | | | | | | | | | | |-> Read later..\par | | | | | | | | | | \par | | | | | | | | | |-> Superuser Binarys\par | | | | | | | | |\par | | | | | | | | |-> User roots home dir\par | | | | | | | | \par | | | | | | | |-> system info etc.\par | | | | | | | \par | | | | | | |-> Orphan files (look here after crashes) \par | | | | | | \par | | | | | |-> System / programming librarys\par | | | | | \par | | | | |-> Contains the user accounts home directorys \par | | | |\par | | | |-> System configuration files.\par | | |\par | | |-> Devices\par | |\par | |-> The kernel and kernel maps.\par |\par |-> Executeble files. (binarys)\par \par \par --------------------------------------------------------------------------------\par \par Here's an example of the following files locations:\par \par /dev/null\par /dev/fd0\par /etc/passwd\par /home/alien/.profile\par /usr/local/bin/BitchX\par \par / \par _______________________________|____________________________________\par | | | | | | | | | | | |\par bin/ boot/ dev/ etc/ home/ lib/ lost+found/ proc/ root/ sbin/ usr/ var/\par | | | | \par | | alien/ local/\par | | | |\par | | |-> .profile | \par | |-> passwd bin/ \par | |\par |-> null |-> BitchX\par |-> fd0\par \par --------------------------------------------------------------------------------\par \par And a final example that's a bit more stripped, just incase you are really\par new to computers.\par Here following files locations:\par \par /dev/null\par /dev/audio\par /dev/hda1\par /etc/passwd\par /etc/shadow\par /home/alien/.profile\par /home/alien/tmp/somefile\par /home/user/.bash_profile\par /usr/local/bin/BitchX\par /sbin/shutdown\par \par /\par _____________________________|_____________________________\par | | | | | \par dev/ etc/ home/----- usr/ sbin/ \par | | | | | |\par |-> null | | | local/ |-> shutdown\par |-> audio | | | | \par |-> hda1 | | | bin/ \par |-> passwd alien/ | |\par |-> shadow | user/ |-> BitchX\par | |\par | |-> .bash_profile\par |\par |-> .profile\par |\par tmp/\par |\par |-> somefile\par \par --------------------------------------------------------------------------------\par \par This is the same structure as on any computer that uses directorys.\par Though some of the directory names are UNIX specific.\par No further explanation should be nessesary.\par \par After reading this tutorial, brows around the system and try to learn\par what all the files does, just dont remove any files you didnt put there\par until your ABSOLUTLEY sure of what you are doing.\par \par --------------------------------------------------------------------------------\par \par Here's a few UNIX commands just for illustration:\par \par echo \par ======\par \tab echo will *echo* anything you add to it like this:\par \par \tab alien:~$ echo "blah"\par \tab blah\par \tab alien:~$\par \tab\par \tab To get it to echo without a new line add the suffix -n\par \tab like this:\par \par \tab alien:~$ echo -n "blah "\par \tab blah alien:~$\par \par \tab I'll get back to why you want to do "-n" sometimes in a while.\par \par read\par ======\par \tab read will read from the keyboard (stdin) and save it as a variable\par \tab the variable name goes after the read command, like this:\par \tab\par \tab alien:~$ read myvar\par \tab \par \tab alien:~$ echo $myvar\par \tab blah\par \tab alien:~$\par \par \tab To combine these two commands (echo and read) in a small script\par \tab line, it can look like this:\par \tab\par \tab alien:~$ echo -n "password: " ; read pass ; echo "Your pass is $pass"\par \tab password: \par \tab Your pass is mypass\par \tab alien:~$\par \par \tab Get the basic idea ?\par \par --------------------------------------------------------------------------------\par \par Anyway, here are some commands that you should know before moving on with this\par tutorial - \par [*] after == important to know.\par [X] after == very basics will do.\par \par ls\tab [*]\par echo\tab [*]\par cat\tab [*]\par less\tab [X]\par head\tab [X]\par tail\tab [X]\par grep\tab [*]\par chmod\tab [*]\par chown\tab [X]\par \par Some applications you need to know how to operate (basics will do) :\par \par any text editor (preferably emacs or vi)\par telnet\par lynx\par ftp (ncftp)\par \par These are all explained later in this tutorial.\par \par --------------------------------------------------------------------------------\par \par Now don't sit there and ask yourself how's going to teach you the commands\par or applications I just listed here above. use the manual pages.\par like this:\par \par man echo\par \par that will get you the full manual on the command echo :)\par man works the same way with applications that you have the manual pages for.\par \par To get out of the manual page just press the letter "q".\par "q" quits it and bet's you back to the command line prompt.\par man uses all the normal "less" commands.\par \par Or read further down in this tutorial in the basic Linux/UNIX commands and \par operations section (8).\par \par --------------------------------------------------------------------------------\par \par The key to shell scripting just as with any programming language/Operating \par System is to REALLY understand what you are doing, so do read this file\par more then once, and don't read it to fast.\par Take your time and let it sink in, so you know what it's all about,\par and do take time to read manual pages and do some playing with the commands\par so you learn them.\par \par Now that should be enough of what you *should* know before starting to learn\par UNIX shell scripting.\par \par So here we go.......\par \par ===============================================================================\par 2 - Where to start\par ===============================================================================\par \par You should always start with very simple scripts that you really don't have\par any practical use for but still *could* be of practical use =)\par \par As for first let's make what we already know to a *real* executable script.\par Open a text editor (name the file myscript.sh) and type this:\par \par \par \par #!/bin/bash\par \par echo -n "password: " \par read pass \par echo "Your pass is $pass"\par \par \par \par save & exit --- then do this command: chmod u+x myscript.sh\par Then we can execute it:\par \par \tab alien:~$ ./myscript.sh\par \tab password: \par \tab Your pass is \par \tab alien:~$\par \par The "#!/bin/bash" at the start of the file is to let the script know what\par shell type it should use.\par \par The "chmod u+x myscript.sh" is to make it executable. \par (change-mode user+execute-right-on myscript.sh) ..... read the manual pages\par on chmod for more info on it =)\par \par --------------------------------------------------------------------------------\par \par Take alot of time to play around in your system, open files, figure out\par what they do (but don't edit or remove them).\par \par Take time also to learn some good text editor, that's important.\par Learn, emacs or vi, those are by most people considerd the absolutly best,\par but jed, joe, elvis, pico or any simple editor like that will do \par just fine for now.\par \par emacs and vi are explained later in this tutorial.\par \par --------------------------------------------------------------------------------\par \par Another thing before moving on is that you can export a variable from a\par script.\par \par Say that you have the variable "$var" in a script and want to export\par it to the system for use with some other script or something, you can\par do: export var\par \par Like this little script:\par \par --------------------------------------------------------------------------------\par \par #!/bin/bash\par \par VAR="10"\par export VAR\par \par --------------------------------------------------------------------------------\par \par But more to how to make scripts in a second, I just thought that this would\par be a good time to enlighten you about this.\par \par So here we go ....\par \par ===============================================================================\par 3 - Beginning techniques.\par ===============================================================================\par \par First off I'm going to show how to count in bash or how to use your command\par line as a calculator, which is really easy and useful.\par \par alien:~$ echo $[ 4 * 2 ]\par 8\par alien:~$\par \par or another example:\par \par alien:~$ echo $[ 10 + 5 ]\par 15\par alien:~$\par \par Easy ? .... I think it is =)\par \par The internal calculator can also be used like this: \par \par alien:~$ echo $(( 10 + 5 ))\par 15\par alien:~$\par \par The second way of using the internal shell calculator is here just\par so you dont get confused if you see it used that way sometime.\par \par --------------------------------------------------------------------------------\par \par Now I'd like to show the *string comparing* with "if" statements,\par which can be a little hard at first contact, but as soon as you get\par familiar with it, it wont be any problems.\par So here's an example.\par \par --------------------------------------------------------------------------------\par \par #!/bin/bash\par \par echo -n "enter a name: "\par read var1\par echo -n "enter another name: "\par read var2\par \par if [ "$var1" = "$var2" ]; then\par echo "The names are the same"\par else\par echo "The names were not the same"\par fi\par \par exit 0\par \par \par --------------------------------------------------------------------------------\par \par Note: "fi" is the ending of if, just like a "\}" is the ending of a "\{",\par exit 0 terminates the script correctly and returns you to the prompt.\par \par Another note is that instead of '=' you can use '-eq' to test if\par 2 expressions are equal, or '-eg' to check if 2 integers are equal, etc.\par \par It should also be said that a variable say '$var' can be written \par as this: $\{var\}, just so you know if you see it done that way\par in some scripts, but here we will use the $var way.\par \par --------------------------------------------------------------------------------\par \par This example if executed looks like this:\par \