mirror of https://github.com/tLDP/LDP
updated
This commit is contained in:
parent
0ab1bbbb56
commit
6435be0bc4
|
@ -9,7 +9,7 @@
|
|||
<article>
|
||||
<title>BASH Programming - Introduction HOW-TO</title>
|
||||
<author>by Mike G <tt/mikkey@dynamo.com.ar/</author>
|
||||
<date>v0.06, 21 June 2000</date>
|
||||
<date>v0.07, 22 June 2000</date>
|
||||
<abstract>
|
||||
This article intends to help you to start programming
|
||||
basic-intermediate shell scripts. It does not intend to be an
|
||||
|
@ -26,17 +26,17 @@
|
|||
<sect>
|
||||
Introduction
|
||||
|
||||
<!--
|
||||
|
||||
<sect1>
|
||||
Getting the latest version
|
||||
|
||||
<P> View it online
|
||||
<htmlurl url=""
|
||||
name="">
|
||||
<p>
|
||||
<htmlurl url="http://www.linuxdoc.org/HOWTO/Bash-Prog-Intro-HOWTO.html"
|
||||
name="http://www.linuxdoc.org/HOWTO/Bash-Prog-Intro-HOWTO.html">
|
||||
|
||||
|
||||
</sect1>
|
||||
-->
|
||||
|
||||
<sect1>
|
||||
Requisites
|
||||
<P> Familiarity with GNU/Linux command lines, and familiarity
|
||||
|
@ -82,14 +82,14 @@ Very simple Scripts
|
|||
<P>
|
||||
<tscreen><verb>
|
||||
#!/bin/bash
|
||||
echo Hello World!
|
||||
echo Hello World
|
||||
</verb></tscreen>
|
||||
<P>
|
||||
<P> This script has only two lines.
|
||||
The first indicates the system which program
|
||||
to use to run the file.
|
||||
<P> 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.
|
||||
</sect1>
|
||||
<sect1>
|
||||
A very simple backup script
|
||||
|
@ -111,7 +111,7 @@ All about redirection
|
|||
|
||||
<sect1>
|
||||
Theory and quick reference
|
||||
<p> There are 3 file descriptors, stdin, stdout and stderr (std=standar)
|
||||
<p> There are 3 file descriptors, stdin, stdout and stderr (std=standard).
|
||||
|
||||
<p>Basically you can:
|
||||
<enum>
|
||||
|
@ -123,7 +123,7 @@ All about redirection
|
|||
<item> redirect stderr and stdout to stdout
|
||||
<item> redirect stderr and stdout to stderr
|
||||
</enum>
|
||||
1 'represents' stdout and 2 stderr
|
||||
1 'represents' stdout and 2 stderr.
|
||||
<p> 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
|
|||
|
||||
<sect1>
|
||||
Sample: stdout 2 file
|
||||
<p> This will cause the ouput of a program to be writen to a file
|
||||
<p> This will cause the ouput of a program to be written to a file.
|
||||
<tscreen><verb>
|
||||
ls -l > ls-l.txt
|
||||
</verb></tscreen>
|
||||
|
@ -141,7 +141,7 @@ All about redirection
|
|||
|
||||
<sect1>
|
||||
Sample: stderr 2 file
|
||||
<p> This will cause the stderr ouput of a program to be writen to a file
|
||||
<p> This will cause the stderr ouput of a program to be written to a file.
|
||||
<tscreen><verb>
|
||||
grep da * 2> grep-errors.txt
|
||||
</verb></tscreen>
|
||||
|
@ -151,7 +151,7 @@ All about redirection
|
|||
|
||||
<sect1>
|
||||
Sample: stdout 2 stderr
|
||||
<p> This will cause the stderr ouput of a program to be writen to the same filedescriptor
|
||||
<p> This will cause the stderr ouput of a program to be written to the same filedescriptor
|
||||
than stdout.
|
||||
<tscreen><verb>
|
||||
grep da * 1>&2
|
||||
|
@ -161,7 +161,7 @@ All about redirection
|
|||
|
||||
<sect1>
|
||||
Sample: stderr 2 stdout
|
||||
<p> This will cause the stderr ouput of a program to be writen to the same filedescriptor
|
||||
<p> This will cause the stderr ouput of a program to be written to the same filedescriptor
|
||||
than stdout.
|
||||
<tscreen><verb>
|
||||
grep * 2>&1
|
||||
|
@ -266,6 +266,7 @@ Variables
|
|||
<P> You have no need to declare a variable, just
|
||||
assigning a value to its reference will create it.
|
||||
|
||||
|
||||
|
||||
<!-- Samples -->
|
||||
<sect1>
|
||||
|
@ -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.
|
||||
</sect1>
|
||||
|
||||
<sect1>
|
||||
Sample: A very simple backup script (little bit better)
|
||||
<P>
|
||||
|
@ -301,13 +303,32 @@ Variables
|
|||
command inside the parenthesis, capturing its output.
|
||||
|
||||
<P> 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.
|
||||
<P> Some more examples:
|
||||
<P> echo ls
|
||||
<P> echo $(ls)
|
||||
</sect1>
|
||||
</sect1>
|
||||
|
||||
<sect1>
|
||||
Local variables
|
||||
|
||||
<p> Local variables can be created by using the keyword <it/local/.
|
||||
|
||||
<tscreen><verb>
|
||||
#!/bin/bash
|
||||
HELLO=Hello
|
||||
function hello {
|
||||
local HELLO=World
|
||||
echo $HELLO
|
||||
}
|
||||
echo $HELLO
|
||||
hello
|
||||
echo $HELLO
|
||||
</verb></tscreen>
|
||||
<p> This example should be enought to show how to use a local variable.
|
||||
</sect1>
|
||||
|
||||
</sect>
|
||||
|
||||
<!-- Conditionals -->
|
||||
|
@ -486,16 +507,16 @@ Functions
|
|||
<sect1>
|
||||
Functions sample
|
||||
<P> <tscreen><verb>
|
||||
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
|
||||
</verb></tscreen>
|
||||
<P> 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
|
|||
<P> 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.
|
||||
</sect1>
|
||||
|
||||
<sect1>
|
||||
Functions with parameters sample
|
||||
<P> <tscreen><verb>
|
||||
#!/bin/bash
|
||||
function quit {
|
||||
exit
|
||||
}
|
||||
function e {
|
||||
echo $1
|
||||
}
|
||||
e Hello
|
||||
e World
|
||||
quit
|
||||
echo foo
|
||||
|
||||
</verb></tscreen>
|
||||
<P> 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.
|
||||
</sect1>
|
||||
|
||||
|
||||
</sect>
|
||||
|
||||
|
||||
|
@ -563,8 +607,23 @@ User interfaces
|
|||
Misc
|
||||
<!-- Reading user input -->
|
||||
<sect1>
|
||||
Reading user input
|
||||
<P> __TO-DO__
|
||||
Reading user input with read
|
||||
<P> 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:
|
||||
<tscreen><verb>
|
||||
#!/bin/bash
|
||||
echo Please, enter your name
|
||||
read NAME
|
||||
echo "Hi $NAME!"
|
||||
</verb></tscreen>
|
||||
<P> As a variant, you can get multiple values with read, this example may clarify this.
|
||||
<tscreen><verb>
|
||||
#!/bin/bash
|
||||
echo Please, enter your firstname and lastname
|
||||
read FN LN
|
||||
echo "Hi! $LN, $FN !"
|
||||
</verb></tscreen>
|
||||
|
||||
</sect1>
|
||||
|
||||
<!-- Arithmetic evaluation -->
|
||||
|
@ -611,7 +670,7 @@ Misc
|
|||
Getting the return value of a program
|
||||
<P> In bash, the return value of a program is stored in a special variable called $?.
|
||||
<P> 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)
|
||||
<it/dada/ does not exist. (This was also suggested by mike)
|
||||
<tscreen><verb>
|
||||
#!/bin/bash
|
||||
cd /dada &> /dev/null
|
||||
|
@ -666,8 +725,26 @@ Tables
|
|||
<P> (4) __TO-DO__
|
||||
<P> (5) s1 is not null (contains one or more characters)
|
||||
<P> (6) s1 is null
|
||||
|
||||
</sect1>
|
||||
|
||||
<sect1>
|
||||
String comparison examples
|
||||
<p> Comparing two strings.
|
||||
<tscreen><verb>
|
||||
#!/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
|
||||
</verb></tscreen>
|
||||
</sect1>
|
||||
|
||||
<!-- Arithmetic operators -->
|
||||
<sect1>
|
||||
Arithmetic operators
|
||||
|
@ -703,7 +780,8 @@ Tables
|
|||
<P> sort
|
||||
<P> bc (more than a calculator)
|
||||
<P> cut (edit columns)
|
||||
|
||||
<P> tput (get information from the current terminal)
|
||||
|
||||
<P> 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
|
||||
<P>
|
||||
<itemize>
|
||||
<item> Nathan Hurst for sending a lot of corrections.
|
||||
<item> Jon Abbott for sending comments about evaluating arithmetic expressions.
|
||||
<item> Laurent Martelli for translating this document to French (soon here the URL)
|
||||
<item> Felix Hudson for writing the <it/renna/ script
|
||||
<item> Nathan Hurst for sending a lot of corrections.
|
||||
<item> Jon Abbott for sending comments about evaluating arithmetic expressions.
|
||||
<item> Laurent Martelli for translating this document to French (soon here the URL)
|
||||
<item> Felix Hudson for writing the <it/renna/ script
|
||||
|
||||
<item> Mike (pink) made some suggestions about locating bash and testing files
|
||||
<item> Kees van der Broek (for sending many corrections)
|
||||
<item> Mike (pink) made some suggestions about locating bash and testing files
|
||||
|
||||
</itemize>
|
||||
</sect1>
|
||||
<sect1>
|
||||
History
|
||||
<p> v5 Added the redirection section
|
||||
<p> 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
|
||||
<p> Samples added on string comparison.
|
||||
<p> v0.7 More corrections and some old TO-DO sections written.
|
||||
<p> v0.6 Minor corrections.
|
||||
<p> v0.5 Added the redirection section.
|
||||
<p> 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.
|
||||
<p> prior: I don't rememeber and I didn't use rcs nor cvs :(
|
||||
</sect1>
|
||||
|
||||
|
|
Loading…
Reference in New Issue