Diferencia entre revisiones de «Usuario:ManuelRomero/Shell»

De WikiEducator
Saltar a: navegación, buscar
(Leer ficheros)
(Comando if)
Línea 58: Línea 58:
 
</source>
 
</source>
 
===Comando if===
 
===Comando if===
<source lang=shell>
+
<source lang=bash>
 
if TEST-COMMANDS; then
 
if TEST-COMMANDS; then
  

Revisión de 08:56 16 abr 2013

Iniciando el programa

  • Primera línea hay que indicar que tipo de shell es
#!/bin/sh
  • Visualizar un mensaje por pantalla comando echo
#!/bin/sh
echo Esto es un shell je je

Variables

  • Definir variables, directamente variable=valor
#!/bin/sh
edad = 5
nombre = pedro
echo hola $nombre tienes %edad años
  • Variables disponibles
$0
contiene el nombre nombre de nuestro script
$#
el número de parámetros con los que se ha invocado al shell
$n
los parámetros, con n de 1 a 9 (a $#)
$$
el PID de nuestro proceso
$*
todos los parámetros menos $0
Determinar el valor de una variable de caracteres

El valor de la variable ($var) está vacío (null):

if [ "$var" == "" ] then
echo variable is null
fi

El valor de la variable ($var) vale manolo

if [ "$var" == "manolo" ] then
echo variable vale manolo
fi

Leer variables

read
Con este comando leemos una variable del teclado

Para debugar

#!/bin/bash -xv

Leer ficheros

while read linea
do
   comando
done < archivo

Comando if

if TEST-COMMANDS; then
 
CONSEQUENT-COMMANDS;
 
elif MORE-TEST-COMMANDS; then
 
MORE-CONSEQUENT-COMMANDS;
 
else ALTERNATE-CONSEQUENT-COMMANDS;
 
fi