Variables de shell

From Luniwiki
(Redirected from Variables)
Jump to: navigation, search

Substitución de Variables

  • var=valor
asigna valor a la variable var
  • ${var}
utiliza el valor de la varible var
los corchetes son opcionales pero pueden ser utiles
  • ${var:-valor}
utiliza el valor de var si esta asignada y sino
utiliza el valor dado por valor
  • ${var:=valor}
utiliza el valor de var si esta asignada y sino
utiliza el valor daor por valor y se lo asigna a var
  • ${var:?valor}
utiliza el valor de var si esta asignada y sino
imprime valor. Si no se da valor imprime
"parameter null or not set."
  • ${var:+valor}
utiliza valor si var esta asignada

Asignación de variables numericas

Esto funciona en bash por lo menos

#!/bin/bash
numeroA=2
numeroB=5
frase="es correcto"
suma=$[$numeroa+$numerob]
multiplica=$[$numeroa*$numerob]
divide=$[$numeroa/$numerob]
resta=$[$numeroa-$numerob]
echo "2 + 5 = $suma $frase"
echo "2 * 5 = $multiplica $frase"
echo "2 / 5 = $divide $frase"
echo "2 - 5 = $resta $frase"

Referencia indirecta de variables

En el caso que queramos que una variable sea la evaluación de otra variable. tenemos que realizarlo en 2 pasos. Por ejeplo si tenemos una vaiable que es EXT y $EXT es JPG y el valor de la variable $JPG es imagen.jpg, si queremos que una variable FICHERO coja directamente el valor imagen.jpg unicamente desde la variable EXT (por que estemos en un bucle por ejemplo) tenemos que usar el comando eval

  • Ejemplo
a=letra
letra=z
eval a=\$$a
echo $a #Devuelve z

Variables embebidas de la shell

$#    Numeros de argumentos dados en la línea de comandos
$-    Options currently in effect (No se a que se refiere el autor)
$?    Valor de salida del ultimo comando ejecutado
$$    PID del proceso actual
$!    PID del ultimo proceso lanzado en background
$0    Primera palabra del la línea de comandos (es decir el comando)
$n    Argumento n de la línea de comando
$*    Todos los argumentos de la línea de comandos ("$1 $2 ...")
"$@"  Todos los argumentos de la línea de comandos,
      separados individualmente ("$1" "$2" ...)

Expresiones para IF

Primary Meaning

[ -a FILE ]	True if FILE exists.
[ -b FILE ]	True if FILE exists and is a block-special file.
[ -c FILE ]	True if FILE exists and is a character-special file.
[ -d FILE ]	True if FILE exists and is a directory.
[ -e FILE ]	True if FILE exists.
[ -f FILE ]	True if FILE exists and is a regular file.
[ -g FILE ]	True if FILE exists and its SGID bit is set.
[ -h FILE ]	True if FILE exists and is a symbolic link.
[ -k FILE ]	True if FILE exists and its sticky bit is set.
[ -p FILE ]	True if FILE exists and is a named pipe (FIFO).
[ -r FILE ]	True if FILE exists and is readable.
[ -s FILE ]	True if FILE exists and has a size greater than zero.
[ -t FD ]	True if file descriptor FD is open and refers to a terminal.
[ -u FILE ]	True if FILE exists and its SUID (set user ID) bit is set.
[ -w FILE ]	True if FILE exists and is writable.
[ -x FILE ]	True if FILE exists and is executable.
[ -O FILE ]	True if FILE exists and is owned by the effective user ID.
[ -G FILE ]	True if FILE exists and is owned by the effective group ID.
[ -L FILE ]	True if FILE exists and is a symbolic link.
[ -N FILE ]	True if FILE exists and has been modified since it was last read.
[ -S FILE ]	True if FILE exists and is a socket.
[ FILE1 -nt FILE2 ]	True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not.
[ FILE1 -ot FILE2 ]	True if FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not.
[ FILE1 -ef FILE2 ]	True if FILE1 and FILE2 refer to the same device and inode numbers.
[ -o OPTIONNAME ]	True if shell option "OPTIONNAME" is enabled.
[ -z STRING ]	True of the length if "STRING" is zero.
[ -n STRING ] or [ STRING ]	True if the length of "STRING" is non-zero.
[ STRING1 == STRING2 ]	True if the strings are equal. "=" may be used instead of "==" for strict POSIX compliance.
[ STRING1 != STRING2 ]	True if the strings are not equal.
[ STRING1 < STRING2 ]	True if "STRING1" sorts before "STRING2" lexicographically in the current locale.
[ STRING1 > STRING2 ]	True if "STRING1" sorts after "STRING2" lexicographically in the current locale.
[ ARG1 OP ARG2 ]	"OP" is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if "ARG1" is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to "ARG2", respectively. "ARG1" and "ARG2" are integers.

Referencias

--Daniel Simao 18:19, 5 Dec 2005 (CET)