diff --git a/LDP/howto/linuxdoc/Bash-Prog-Intro-HOWTO.sgml b/LDP/howto/linuxdoc/Bash-Prog-Intro-HOWTO.sgml index e5fba51d..e73e2f29 100644 --- a/LDP/howto/linuxdoc/Bash-Prog-Intro-HOWTO.sgml +++ b/LDP/howto/linuxdoc/Bash-Prog-Intro-HOWTO.sgml @@ -9,7 +9,7 @@
BASH Programming - Introduction HOW-TO by Mike G - v0.06, 21 June 2000 + v0.07, 22 June 2000 This article intends to help you to start programming basic-intermediate shell scripts. It does not intend to be an @@ -26,17 +26,17 @@ Introduction - + Requisites

Familiarity with GNU/Linux command lines, and familiarity @@ -82,14 +82,14 @@ Very simple Scripts

#!/bin/bash - echo Hello World! + echo Hello World

This script has only two lines. The first indicates the system which program to use to run the file.

The second line is the only action performed by this script, - which prints 'Hello World! on the terminal. + which prints 'Hello World' on the terminal. A very simple backup script @@ -111,7 +111,7 @@ All about redirection Theory and quick reference -

There are 3 file descriptors, stdin, stdout and stderr (std=standar) +

There are 3 file descriptors, stdin, stdout and stderr (std=standard).

Basically you can: @@ -123,7 +123,7 @@ All about redirection redirect stderr and stdout to stdout redirect stderr and stdout to stderr - 1 'represents' stdout and 2 stderr + 1 'represents' stdout and 2 stderr.

A little note for seeing this things: with the less command you can view both stdout (which will remain on the buffer) and the stderr that will be printed on the screen, but erased as you try to 'browse' the buffer. @@ -131,7 +131,7 @@ All about redirection Sample: stdout 2 file -

This will cause the ouput of a program to be writen to a file +

This will cause the ouput of a program to be written to a file. ls -l > ls-l.txt @@ -141,7 +141,7 @@ All about redirection Sample: stderr 2 file -

This will cause the stderr ouput of a program to be writen to a file +

This will cause the stderr ouput of a program to be written to a file. grep da * 2> grep-errors.txt @@ -151,7 +151,7 @@ All about redirection Sample: stdout 2 stderr -

This will cause the stderr ouput of a program to be writen to the same filedescriptor +

This will cause the stderr ouput of a program to be written to the same filedescriptor than stdout. grep da * 1>&2 @@ -161,7 +161,7 @@ All about redirection Sample: stderr 2 stdout -

This will cause the stderr ouput of a program to be writen to the same filedescriptor +

This will cause the stderr ouput of a program to be written to the same filedescriptor than stdout. grep * 2>&1 @@ -266,6 +266,7 @@ Variables

You have no need to declare a variable, just assigning a value to its reference will create it. + @@ -284,6 +285,7 @@ Variables that if you don't use the '$' sign, the output of the program will be different, and probably not what you want it to be. + Sample: A very simple backup script (little bit better)

@@ -301,13 +303,32 @@ Variables command inside the parenthesis, capturing its output.

Notice that in this script, the output filename will - be different - every day, due to the format switch to the date command(+%Y%m%d). + be different every day, due to the format switch to the date command(+%Y%m%d). You can change this by specifying a different format.

Some more examples:

echo ls

echo $(ls) - + + + + Local variables + +

Local variables can be created by using the keyword + #!/bin/bash + HELLO=Hello + function hello { + local HELLO=World + echo $HELLO + } + echo $HELLO + hello + echo $HELLO + +

This example should be enought to show how to use a local variable. + + @@ -486,16 +507,16 @@ Functions Functions sample

- 1) #!/bin/bash - 2) function quit { - 3) exit - 4) } - 5) function hello { - 6) echo Hello! - 7) } - 8) hello - 9) quit - 10) echo foo + #!/bin/bash + function quit { + exit + } + function hello { + echo Hello! + } + hello + quit + echo foo

Lines 2-4 contain the 'quit' function. Lines 5-7 contain the 'hello' function If you are not absolutely sure about what this script does, please try it!. @@ -503,6 +524,29 @@ Functions

When running the script you'll notice that first: the function 'hello' is called, second the 'quit' function, and the program never reaches line 10. + + + Functions with parameters sample +

+ #!/bin/bash + function quit { + exit + } + function e { + echo $1 + } + e Hello + e World + quit + echo foo + + +

This script is almos identically to the previous one. The main difference is the + funcion 'e'. This function, prints the first argument it receives. + Arguments, within funtions, are treated in the same manner as arguments given to the script. + + + @@ -563,8 +607,23 @@ User interfaces Misc - Reading user input -

__TO-DO__ + Reading user input with read +

In many ocations you may want to prompt the user for some input, and there are several ways + to achive this. This is one of those ways: + + #!/bin/bash + echo Please, enter your name + read NAME + echo "Hi $NAME!" + +

As a variant, you can get multiple values with read, this example may clarify this. + + #!/bin/bash + echo Please, enter your firstname and lastname + read FN LN + echo "Hi! $LN, $FN !" + + @@ -611,7 +670,7 @@ Misc Getting the return value of a program

In bash, the return value of a program is stored in a special variable called $?.

This illustrates how to capture the return value of a program, I assume that the directory - /dada does not exist. (This was also suggested by mike) + #!/bin/bash cd /dada &> /dev/null @@ -666,8 +725,26 @@ Tables

(4) __TO-DO__

(5) s1 is not null (contains one or more characters)

(6) s1 is null - + + + String comparison examples +

Comparing two strings. + + #!/bin/bash + S1='string' + S2='String' + if [ $S1=$S2 ]; + then + echo "S1('$S1') is not equal to S2('$S2')" + fi + if [ $S1=$S1 ]; + then + echo "S1('$S1') is equal to S1('$S1')" + fi + + + Arithmetic operators @@ -703,7 +780,8 @@ Tables

sort

bc (more than a calculator)

cut (edit columns) - +

tput (get information from the current terminal) +

It it higly recommended to be familiarized with this programs (at least). There are tons of little programs that will let you do real magic in a @@ -885,20 +963,24 @@ About the document Thanks to

- Nathan Hurst for sending a lot of corrections. - Jon Abbott for sending comments about evaluating arithmetic expressions. - Laurent Martelli for translating this document to French (soon here the URL) - Felix Hudson for writing the Nathan Hurst for sending a lot of corrections. + Jon Abbott for sending comments about evaluating arithmetic expressions. + Laurent Martelli for translating this document to French (soon here the URL) + Felix Hudson for writing the Mike (pink) made some suggestions about locating bash and testing files + Kees van der Broek (for sending many corrections) + Mike (pink) made some suggestions about locating bash and testing files History -

v5 Added the redirection section -

v4 disapperd from its location due to my ex-boss and thid doc found it's new place - at the proper url: www.linuxdoc.org +

Samples added on string comparison. +

v0.7 More corrections and some old TO-DO sections written. +

v0.6 Minor corrections. +

v0.5 Added the redirection section. +

v0.4 disapperd from its location due to my ex-boss and thid doc found it's new place + at the proper url: www.linuxdoc.org.

prior: I don't rememeber and I didn't use rcs nor cvs :(