From 3a7f973d762cff3df2e48d452f2c696283f7584f Mon Sep 17 00:00:00 2001 From: gferg <> Date: Tue, 20 Jun 2000 14:41:52 +0000 Subject: [PATCH] updated --- LDP/howto/linuxdoc/Bash-Prog-Intro-HOWTO.sgml | 181 ++++++++++++++++-- 1 file changed, 168 insertions(+), 13 deletions(-) diff --git a/LDP/howto/linuxdoc/Bash-Prog-Intro-HOWTO.sgml b/LDP/howto/linuxdoc/Bash-Prog-Intro-HOWTO.sgml index 627cc6a5..32e0d225 100644 --- a/LDP/howto/linuxdoc/Bash-Prog-Intro-HOWTO.sgml +++ b/LDP/howto/linuxdoc/Bash-Prog-Intro-HOWTO.sgml @@ -9,13 +9,14 @@
BASH Programming - Introduction HOW-TO by Mike G - v0.04, 31 January 2000 + v0.05, 20 June 2000 This article intends to help you to start programming basic-intermediate shell scripts. It does not intend to be an advanced document (see the title). I am NOT an expert nor guru 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 :) @@ -24,23 +25,18 @@ Introduction + + Requisites

Familiarity with GNU/Linux command lines, and familiarity @@ -109,6 +105,156 @@ Very simple Scripts + + +All about redirection + + + Theory and quick reference +

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

Basically you can: + + redirect stdout to a file + redirect stderr to a file + redirect stdout to a stderr + redirect stderr to a stdout + redirect stderr and stdout to a file + redirect stderr and stdout to stdout + redirect stderr and stdout to 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. + + + + Sample: stdout 2 file +

This will cause the ouput of a program to be writen to a file + + ls -l > ls-l.txt + + 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. + + + + Sample: stderr 2 file +

This will cause the stderr ouput of a program to be writen to a file + + grep da * 2> grep-errors.txt + + 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. + + + + Sample: stdout 2 stderr +

This will cause the stderr ouput of a program to be writen to the same filedescriptor + than stdout. + + grep da * 1>&2 + + Here, the stdout portion of the command is sent to stderr, you may notice that in differen ways. + + + + Sample: stderr 2 stdout +

This will cause the stderr ouput of a program to be writen to the same filedescriptor + than stdout. + + grep * 2>&1 + + 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). + + + + Sample: stderr and stdout 2 file +

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. + + rm -f $(find / -name core) &> /dev/null + + 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. + + + + + + + + + + + +Pipes +

This section explains in a very simple and practical way how to use pipes, + nd why you may want it. + + + What they are and why you'll want to use them +

Pipes let you use (very simple, I insist) the output of a program as the + input of another one + + + + + Sample: simple pipe with sed +

This is very simple way to use pipes. + + ls -l | sed -e "s/[aeio]/u/g" + + 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. + + + + Sample: an alternative to ls -l *.txt +

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. + + ls -l | grep "\.txt$" + + 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$". + + + + + + + + + + + + Variables @@ -683,11 +829,20 @@ About the document Felix Hudson for writing the + + 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 +

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

Introduction to bash (under BE) -

Bourne Shell Programming http://207.213.123.70/book/