mirror of https://github.com/tLDP/LDP
updated
This commit is contained in:
parent
1f204da411
commit
3a7f973d76
|
@ -9,13 +9,14 @@
|
||||||
<article>
|
<article>
|
||||||
<title>BASH Programming - Introduction HOW-TO</title>
|
<title>BASH Programming - Introduction HOW-TO</title>
|
||||||
<author>by Mike G <tt/mikkey@dynamo.com.ar/</author>
|
<author>by Mike G <tt/mikkey@dynamo.com.ar/</author>
|
||||||
<date>v0.04, 31 January 2000</date>
|
<date>v0.05, 20 June 2000</date>
|
||||||
<abstract>
|
<abstract>
|
||||||
This article intends to help you to start programming
|
This article intends to help you to start programming
|
||||||
basic-intermediate shell scripts. It does not intend to be an
|
basic-intermediate shell scripts. It does not intend to be an
|
||||||
advanced document (see the title). I am NOT an expert nor guru
|
advanced document (see the title). I am NOT an expert nor guru
|
||||||
shell programmer. I decided to write this because I'll learn a
|
shell programmer. I decided to write this because I'll learn a
|
||||||
lot and it might be useful to other people.
|
lot and it might be useful to other people. Any feedback will be apreciated,
|
||||||
|
specially in the patch form :)
|
||||||
</abstract>
|
</abstract>
|
||||||
|
|
||||||
<!-- Table of contents -->
|
<!-- Table of contents -->
|
||||||
|
@ -24,23 +25,18 @@
|
||||||
<!-- Requisites -->
|
<!-- Requisites -->
|
||||||
<sect>
|
<sect>
|
||||||
Introduction
|
Introduction
|
||||||
|
|
||||||
|
<!--
|
||||||
<sect1>
|
<sect1>
|
||||||
Getting the latest version
|
Getting the latest version
|
||||||
|
|
||||||
<P> Download latest version
|
|
||||||
<htmlurl url="http://www.digimedia.com.ar/~mike/contrib/BASH-INTRO-PROG-HOW-TO/BASH-PROG-INTRO-HOWTO.tgz"
|
|
||||||
name="http://www.digimedia.com.ar/~mike/contrib/BASH-INTRO-PROG-HOW-TO/BASH-PROG-INTRO-HOWTO.tgz">
|
|
||||||
|
|
||||||
<P> Get sgml source
|
|
||||||
<htmlurl url="http://www.digimedia.com.ar/~mike/contrib/BASH-INTRO-PROG-HOW-TO/BASH-PROG-INTRO-HOWTO.sgml"
|
|
||||||
name="http://www.digimedia.com.ar/~mike/contrib/BASH-INTRO-PROG-HOW-TO/BASH-PROG-INTRO-HOWTO.sgml">
|
|
||||||
|
|
||||||
<P> View it online
|
<P> View it online
|
||||||
<htmlurl url="http://www.digimedia.com.ar/~mike/contrib/BASH-INTRO-PROG-HOW-TO/html/BASH-PROG-INTRO-HOWTO.html"
|
<htmlurl url=""
|
||||||
name="http://www.digimedia.com.ar/~mike/contrib/BASH-INTRO-PROG-HOW-TO/html/BASH-PROG-INTRO-HOWTO.html">
|
name="">
|
||||||
|
|
||||||
|
|
||||||
</sect1>
|
</sect1>
|
||||||
|
-->
|
||||||
<sect1>
|
<sect1>
|
||||||
Requisites
|
Requisites
|
||||||
<P> Familiarity with GNU/Linux command lines, and familiarity
|
<P> Familiarity with GNU/Linux command lines, and familiarity
|
||||||
|
@ -109,6 +105,156 @@ Very simple Scripts
|
||||||
</sect1>
|
</sect1>
|
||||||
</sect>
|
</sect>
|
||||||
|
|
||||||
|
<!-- Redirection -->
|
||||||
|
<sect>
|
||||||
|
All about redirection
|
||||||
|
|
||||||
|
<sect1>
|
||||||
|
Theory and quick reference
|
||||||
|
<p> There are 3 file descriptors, stdin, stdout and stderr (std=standar)
|
||||||
|
|
||||||
|
<p>Basically you can:
|
||||||
|
<enum>
|
||||||
|
<item> redirect stdout to a file
|
||||||
|
<item> redirect stderr to a file
|
||||||
|
<item> redirect stdout to a stderr
|
||||||
|
<item> redirect stderr to a stdout
|
||||||
|
<item> redirect stderr and stdout to a file
|
||||||
|
<item> redirect stderr and stdout to stdout
|
||||||
|
<item> redirect stderr and stdout to stderr
|
||||||
|
</enum>
|
||||||
|
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.
|
||||||
|
</sect1>
|
||||||
|
|
||||||
|
<sect1>
|
||||||
|
Sample: stdout 2 file
|
||||||
|
<p> This will cause the ouput of a program to be writen to a file
|
||||||
|
<tscreen><verb>
|
||||||
|
ls -l > ls-l.txt
|
||||||
|
</verb></tscreen>
|
||||||
|
Here, a file called 'ls-l.txt' will be created and it will contain what you would see on the
|
||||||
|
screen if you type the command 'ls -l' and execute it.
|
||||||
|
</sect1>
|
||||||
|
|
||||||
|
<sect1>
|
||||||
|
Sample: stderr 2 file
|
||||||
|
<p> This will cause the stderr ouput of a program to be writen to a file
|
||||||
|
<tscreen><verb>
|
||||||
|
grep da * 2> grep-errors.txt
|
||||||
|
</verb></tscreen>
|
||||||
|
Here, a file called 'grep-errors.txt' will be created and it will contain what you would see
|
||||||
|
the stderr portion of the output of the 'grep da *' command.
|
||||||
|
</sect1>
|
||||||
|
|
||||||
|
<sect1>
|
||||||
|
Sample: stdout 2 stderr
|
||||||
|
<p> This will cause the stderr ouput of a program to be writen to the same filedescriptor
|
||||||
|
than stdout.
|
||||||
|
<tscreen><verb>
|
||||||
|
grep da * 1>&2
|
||||||
|
</verb></tscreen>
|
||||||
|
Here, the stdout portion of the command is sent to stderr, you may notice that in differen ways.
|
||||||
|
</sect1>
|
||||||
|
|
||||||
|
<sect1>
|
||||||
|
Sample: stderr 2 stdout
|
||||||
|
<p> This will cause the stderr ouput of a program to be writen to the same filedescriptor
|
||||||
|
than stdout.
|
||||||
|
<tscreen><verb>
|
||||||
|
grep * 2>&1
|
||||||
|
</verb></tscreen>
|
||||||
|
Here, the stderr portion of the command is sent to stdout, if you pipe to less, you'll see that
|
||||||
|
lines that normally 'dissapear' (as they are written to stderr) are being kept now (because
|
||||||
|
they're on stdout).
|
||||||
|
</sect1>
|
||||||
|
|
||||||
|
<sect1>
|
||||||
|
Sample: stderr and stdout 2 file
|
||||||
|
<p> This will place every output of a program to a file. This is suitable sometimes
|
||||||
|
for cron entries, if you want a command to pass in absolute silence.
|
||||||
|
<tscreen><verb>
|
||||||
|
rm -f $(find / -name core) &> /dev/null
|
||||||
|
</verb></tscreen>
|
||||||
|
This (thinking on the cron entry) will delete every file called 'core' in any directory. Notice
|
||||||
|
that you should be pretty sure of what a command is doing if you are going to wipe it's output.
|
||||||
|
</sect1>
|
||||||
|
<!--
|
||||||
|
redirect stderr and stdout to stdout
|
||||||
|
redirect stderr and stdout to stderr
|
||||||
|
-->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<!--
|
||||||
|
<sect1>
|
||||||
|
Sample:
|
||||||
|
<p> This will
|
||||||
|
<tscreen><verb>
|
||||||
|
command
|
||||||
|
</verb></tscreen>
|
||||||
|
Explanation
|
||||||
|
</sect1>
|
||||||
|
-->
|
||||||
|
|
||||||
|
</sect>
|
||||||
|
|
||||||
|
<!-- pipes -->
|
||||||
|
<sect>
|
||||||
|
Pipes
|
||||||
|
<p> This section explains in a very simple and practical way how to use pipes,
|
||||||
|
nd why you may want it.
|
||||||
|
|
||||||
|
<sect1>
|
||||||
|
What they are and why you'll want to use them
|
||||||
|
<p> Pipes let you use (very simple, I insist) the output of a program as the
|
||||||
|
input of another one
|
||||||
|
</sect1>
|
||||||
|
|
||||||
|
|
||||||
|
<sect1>
|
||||||
|
Sample: simple pipe with sed
|
||||||
|
<p> This is very simple way to use pipes.
|
||||||
|
<tscreen><verb>
|
||||||
|
ls -l | sed -e "s/[aeio]/u/g"
|
||||||
|
</verb></tscreen>
|
||||||
|
Here, the following happens: first the command ls -l is executed, and it's output,
|
||||||
|
instead of being printed, is sent (piped) to the sed program, which in turn, prints
|
||||||
|
what it has to.
|
||||||
|
</sect1>
|
||||||
|
|
||||||
|
<sect1>
|
||||||
|
Sample: an alternative to ls -l *.txt
|
||||||
|
<p> Probably, this is a more difficult way to do ls -l *.txt, but it is here for illustrating pipes,
|
||||||
|
not for solving such listing dilema.
|
||||||
|
<tscreen><verb>
|
||||||
|
ls -l | grep "\.txt$"
|
||||||
|
</verb></tscreen>
|
||||||
|
Here, the output of the program ls -l is sent to the grep program, which, in turn, will print
|
||||||
|
lines which match the regex "\.txt$".
|
||||||
|
</sect1>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<!--
|
||||||
|
<sect1>
|
||||||
|
Sample:
|
||||||
|
<p> This will
|
||||||
|
<tscreen><verb>
|
||||||
|
command
|
||||||
|
</verb></tscreen>
|
||||||
|
Explanation
|
||||||
|
</sect1>
|
||||||
|
-->
|
||||||
|
|
||||||
|
|
||||||
|
</sect>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Variables -->
|
<!-- Variables -->
|
||||||
<sect>
|
<sect>
|
||||||
Variables
|
Variables
|
||||||
|
@ -683,11 +829,20 @@ About the document
|
||||||
<item> Felix Hudson for writing the <it/renna/ script
|
<item> Felix Hudson for writing the <it/renna/ script
|
||||||
</itemize>
|
</itemize>
|
||||||
</sect1>
|
</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> prior: I don't rememeber and I didn't use rcs nor cvs :(
|
||||||
|
</sect1>
|
||||||
|
|
||||||
<sect1>
|
<sect1>
|
||||||
More resources
|
More resources
|
||||||
<P>
|
<P>
|
||||||
<P> Introduction to bash (under BE)
|
<P> Introduction to bash (under BE)
|
||||||
<htmlurl http://org.laol.net/lamug/beforever/bashtut.htm
|
<htmlurl url="http://org.laol.net/lamug/beforever/bashtut.htm"
|
||||||
|
name="http://org.laol.net/lamug/beforever/bashtut.htm">
|
||||||
<P> Bourne Shell Programming
|
<P> Bourne Shell Programming
|
||||||
http://207.213.123.70/book/
|
http://207.213.123.70/book/
|
||||||
</sect1>
|
</sect1>
|
||||||
|
|
Loading…
Reference in New Issue