domingo, 9 de noviembre de 2014

Bash Guide for Beginners


Machtelt Garrels

Version 1.11 Last updated 20081227 Edition

Table of Contents
Introduction
1. Why this guide?
2. Who should read this book?
3. New versions, translations and availability
4. Revision History
5. Contributions
6. Feedback
7. Copyright information
8. What do you need?
9. Conventions used in this document
10. Organization of this document
1. Bash and Bash scripts
1.1. Common shell programs
1.2. Advantages of the Bourne Again SHell
1.3. Executing commands
1.4. Building blocks
1.5. Developing good scripts
1.6. Summary
1.7. Exercises
2. Writing and debugging scripts
2.1. Creating and running a script
2.2. Script basics
2.3. Debugging Bash scripts
2.4. Summary
2.5. Exercises
3. The Bash environment
3.1. Shell initialization files
3.2. Variables
3.3. Quoting characters
3.4. Shell expansion
3.5. Aliases
3.6. More Bash options
3.7. Summary
3.8. Exercises
4. Regular expressions
4.1. Regular expressions
4.2. Examples using grep
4.3. Pattern matching using Bash features
4.4. Summary
4.5. Exercises
5. The GNU sed stream editor
5.1. Introduction
5.2. Interactive editing
5.3. Non-interactive editing
5.4. Summary
5.5. Exercises
6. The GNU awk programming language
6.1. Getting started with gawk
6.2. The print program
6.3. Gawk variables
6.4. Summary
6.5. Exercises
7. Conditional statements
7.1. Introduction to if
7.2. More advanced if usage
7.3. Using case statements
7.4. Summary
7.5. Exercises
8. Writing interactive scripts
8.1. Displaying user messages
8.2. Catching user input
8.3. Summary
8.4. Exercises
9. Repetitive tasks
9.1. The for loop
9.2. The while loop
9.3. The until loop
9.4. I/O redirection and loops
9.5. Break and continue
9.6. Making menus with the select built-in
9.7. The shift built-in
9.8. Summary
9.9. Exercises
10. More on variables
10.1. Types of variables
10.2. Array variables
10.3. Operations on variables
10.4. Summary
10.5. Exercises
11. Functions
11.1. Introduction
11.2. Examples of functions in scripts
11.3. Summary
11.4. Exercises
12. Catching signals
12.1. Signals
12.2. Traps
12.3. Summary
12.4. Exercises
A. Shell Features
A.1. Common features
A.2. Differing features
Glossary
Index

Consultando una base de datos desde bashy asignando el resultado a una variable

#!/bin/bash
A=$(mysql --user=root --password='19s555' asterisk -e 'SELECT * FROM cdr'; )
echo $A

Haciendo una llamada telefonica desde bash

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
echo " prueba de llamadas $(date)"
$1
$2
B=`asterisk -x "originate SIP/$1 extension $2@internal" `

Script de selection en Bash read

#!/bin/bash
function press_enter
{
echo ""
echo -n "Press Enter to continue"
read
clear
}

selection=
until [ "$selection" = "0" ]; do
echo ""
echo "PROGRAM MENU"
echo "1 - display free disk space"
echo "2 - display free memory"
echo ""
echo "0 - exit program"
echo ""
echo -n "Enter selection: "
read selection
echo ""
case $selection in
1 ) df ; press_enter ;;
2 ) free ; press_enter ;;
0 ) exit ;;
* ) echo "Please enter 1, 2, or 0"; press_enter
esac
done

Bash IF

#!/bin/bash
T1="foo"
T2="bar"
if [ "$T1" = "$T2" ]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi

exportando la ruta de nuestro script

Exportando la ruta de nuestro script
export PATH="$PATH:~/scripts"

http://www.cyberciti.biz/faq/unix-linux-adding-path/

Exportando variables Bash



root@asterisk-dominicana:~# export var1=4566
root@asterisk-dominicana:~# echo $var1
4566
root@asterisk-dominicana:~#

BASH Programming - Introduction HOW-TO


by Mike G mikkey at dynamo.com.ar

Thu Jul 27 09:36:18 ART 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. Any feedback will be apreciated, specially in the patch form :)

1. Introduction

2. Very simple Scripts

3. All about redirection

4. Pipes

5. Variables

6. Conditionals

7. Loops for, while and until

8. Functions

9. User interfaces

10. Misc

11. Tables

12. More Scripts

13. When something goes wrong (debugging)

14. About the document


Next Previous Contents