1 of 26

Scripting: Bash

Sistemas Operativos

http://faqoperativos.com.ar/bash

2 of 26

Bash: Agenda

  • Shells
  • Acerca de bash
  • Introducción al Lenguaje

3 of 26

Bash: Shells

¿ ?

4 of 26

Bash: Shells

Shell: Interfaz entre el Sistema Operativo y el Usuario

GUI (Graphical User Interface)

CLI (Command Line Interface)

5 of 26

Bash: Shells

Command Line Interface

Comando

Resultado

Prompt

6 of 26

Bash: Shells

¿Que puedo hacer?

  • ls folder => Listar archivos del directorio "folder"

  • vi file.txt => Editar un archivo de texto

  • ./mi-TP.exe => Ejecutar mi TP

  • Ejecutar programas, o más genericamente:
    • "Comandos"

7 of 26

Bash: Shells

¿Entonces, quién hace posible esto?

" Un programa, comúnmente llamado Shell, que realiza la función de interpretar los comandos recibidos por el usuario. "

Sin embargo, también hace algunas cosas más....

8 of 26

Bash: Shells

El protocolo de comunicación es, en realidad, un lenguaje de programación

SHELL

ORDEN

ORDEN respeta la Sintaxis y Semántica del lenguaje aceptado por el Shell

9 of 26

Bash: Shells

WINDOWS

  • cmd.exe
  • command.com
  • PowerShell

Linux

  • Bourne Shell (sh)
  • Bourne-Again Shell (bash)
  • Korn Shell (ksh)
  • C Shell (csh)

Algunos Ejemplos:

10 of 26

Bash: Acerca de...

Bash: Bourne-Again Shell

11 of 26

Bash: Acerca de...

Bash es...

  • Un lenguaje de Shell Scripting

  • Un lenguaje Interpretado

12 of 26

Bash: Intro Lenguaje

  • Un programa pequeño
  • Pensado, inicialmente, para automatizar tareas de administración
  • Una forma "quick-and-dirty" de prototipar una aplicación compleja
  • Adhiere a la filosofía clásica de UNIX de partir un problema grande en muchos chiquitos, encadenando ("chaining together") componentes y utilitarios

Shell Script

13 of 26

Bash: Intro Lenguaje

Lenguaje Interpretado (ej: Bash)

Intérprete

SO / HW

if...

then..

else..

Lenguaje Compilado (ej: C)

Compilador

if...

then..

else..

SO / HW

1010..

1110..

0010..

Traduce y Ejecuta

Traducción y Linkeo

Ejecución

14 of 26

Bash: Intro Lenguaje

Como todo lenguaje procedural imperativo, tiene:

  • Variables (enteras, strings, arrays)
  • Tests (if/then)
  • Loops (for/while)
  • Funciones

15 of 26

Bash: Intro Lenguaje

Como muchos lenguajes de shell scripting, no sirve para:

  • Tareas intensivas
  • Operaciones matemáticas complejas
  • Aplicaciones complejas

16 of 26

Bash: Intro Lenguaje

Hola Mundo

#!/bin/bash

# Comentario

echo hola mundo;

exit 0;

Intérprete a utilizar

Programa

1° parámetro

2° parámetro

17 of 26

Bash: Intro Lenguaje

Ejecutando el script:

bash holamundo.sh

sh holamundo.sh

chmod u+x holamundo.sh

./holamundo.sh

Danger!

18 of 26

Bash: Intro Lenguaje

Sobre la primer línea...

¿Puede ir cualquier cosa?

#!/bin/rm

echo ¿Que onda?

19 of 26

Bash: Intro Lenguaje

Variables y parámetros:

  • Se declaran en cualquier lugar

  • Normalmente, se usan sin tipar

  • Existen algunas variables predefinidas:

$$, $!, $?, $@ $1, $2, $#, etc

20 of 26

Bash: Intro Lenguaje

En Bash, los espacios importan:

A=1 # OK

A =1 # Error, intenta ejecutar A

A= 1 # Error, intenta ejecutar A=

Bash: "Si no lo entiendo, es un programa"

21 of 26

Bash: Intro Lenguaje

Quoting (entrecomillado) & Escaping:

echo hola $A # hola mundo

echo hola ($A) # Error

echo "hola ($A)" # hola (mundo)

echo 'hola ($A)' # hola ($A)

echo hola \($A\) # hola (mundo)

Light Quoting

Strong Quoting

Escaping

22 of 26

Bash: Intro Lenguaje

¿Como se ejecuta el script a nivel SO?

utnso@utnso:~$ ./listar-procesos.sh

PID TTY STAT COMMAND

6719 pts/0 Ss -bash

15036 pts/0 S+ \_ /bin/bash ./listar..sh

15037 pts/0 R+ \_ ps f

  • El script se ejecuta en un proceso separado
  • El interprete original, efectivamente es un shell de Bash (PID 6719)

23 of 26

Bash: Intro Lenguaje

Entonces:

¿Pueden un script ejecutor y el script ejecutado compartir variables?

24 of 26

Bash: Intro Lenguaje

No todo es un programa...

Builtins (o Órdenes Internas)

  • A builtin is a command contained within the Bash tool set, literally built in
  • Motivaciones
    • Performance
    • Necesidad de acceso a cuestiones internas del shell

Ejemplos: cd, export, unset, source, ...

25 of 26

Bash: Intro Lenguaje

Muchas cosas más por saber:

  • El uso de test para consultar sobre archivos
  • Arrays
  • Command substitution
  • I/O Redirection
  • Pipelines (tuberías)
  • Expansión arimética
  • etc....

26 of 26

Bash: Intro Lenguaje