BASH Programming − Introduction HOW−TO BASH Programming − Introduction HOW−TO
Total Page:16
File Type:pdf, Size:1020Kb
BASH Programming − Introduction HOW−TO BASH Programming − Introduction HOW−TO Table of Contents BASH Programming − Introduction HOW−TO.............................................................................................1 by Mike G mikkey at dynamo.com.ar.....................................................................................................1 1.Introduction...........................................................................................................................................1 2.Very simple Scripts...............................................................................................................................1 3.All about redirection.............................................................................................................................1 4.Pipes......................................................................................................................................................1 5.Variables...............................................................................................................................................2 6.Conditionals..........................................................................................................................................2 7.Loops for, while and until.....................................................................................................................2 8.Functions...............................................................................................................................................2 9.User interfaces......................................................................................................................................2 10.Misc....................................................................................................................................................2 11.Tables..................................................................................................................................................3 12.More Scripts........................................................................................................................................3 13.When something goes wrong (debugging).........................................................................................3 14.About the document............................................................................................................................3 1.Introduction...........................................................................................................................................3 1.1 Getting the latest version...................................................................................................................3 1.2 Requisites...........................................................................................................................................4 1.3 Uses of this document........................................................................................................................4 2.Very simple Scripts...............................................................................................................................4 2.1 Traditional hello world script.............................................................................................................4 2.2 A very simple backup script..............................................................................................................4 3.All about redirection.............................................................................................................................5 3.1 Theory and quick reference...............................................................................................................5 3.2 Sample: stdout 2 file .........................................................................................................................5 3.3 Sample: stderr 2 file ..........................................................................................................................5 3.4 Sample: stdout 2 stderr......................................................................................................................6 3.5 Sample: stderr 2 stdout .....................................................................................................................6 3.6 Sample: stderr and stdout 2 file ........................................................................................................6 4.Pipes......................................................................................................................................................6 4.1 What they are and why you'll want to use them................................................................................7 4.2 Sample: simple pipe with sed ...........................................................................................................7 4.3 Sample: an alternative to ls −l *.txt ..................................................................................................7 5.Variables...............................................................................................................................................7 5.1 Sample: Hello World! using variables...............................................................................................7 5.2 Sample: A very simple backup script (little bit better)......................................................................8 5.3 Local variables...................................................................................................................................8 6.Conditionals..........................................................................................................................................9 6.1 Dry Theory.........................................................................................................................................9 6.2 Sample: Basic conditional example if .. then.....................................................................................9 6.3 Sample: Basic conditional example if .. then ... else.......................................................................10 6.4 Sample: Conditionals with variables...............................................................................................10 7.Loops for, while and until...................................................................................................................10 7.1 For sample........................................................................................................................................10 7.2 C−like for.........................................................................................................................................11 7.3 While sample...................................................................................................................................11 i BASH Programming − Introduction HOW−TO Table of Contents 7.4 Until sample.....................................................................................................................................11 8.Functions.............................................................................................................................................12 8.1 Functions sample.............................................................................................................................12 8.2 Functions with parameters sample...................................................................................................12 9.User interfaces....................................................................................................................................13 9.1 Using select to make simple menus.................................................................................................13 9.2 Using the command line .................................................................................................................13 10.Misc..................................................................................................................................................14 10.1 Reading user input with read.........................................................................................................14 10.2 Arithmetic evaluation.....................................................................................................................14 10.3 Finding bash ..................................................................................................................................15 10.4 Getting the return value of a program............................................................................................16 10.5 Capturing a commands output ......................................................................................................16 10.6 Multiple source files......................................................................................................................16 11.Tables................................................................................................................................................16 11.1 String comparison operators..........................................................................................................16 11.2 String comparison examples..........................................................................................................17 11.3 Arithmetic operators......................................................................................................................17