====== Scripting: ejemplos básicos ====== //Nota//: The magical incantation '2>&1' has more meaning when it is considered that, when running in a UNIX or UNIX-like environment, each file opened (for reading or writing) is assigned a file descriptor number. By default, all commands have three file descriptors open as they run: STDIN (standard input), STDOUT (standard output), and STDERR (standard error). These are assigned the numbers 0, 1, and 2 respectively. So in the given syntax, '2' means standard error, '>' means '2' is being redirected, '&' means '1' is a file descriptor (and not a file named '1'), and '1' means standard output. The reason '2>&1' is specified after STDOUT is redirected to a file is that the command is read by the shell interpreter from left to right and descriptors are resolved immediately. If '2>&1' came first, STDERR would go to the original STDOUT (and not the file), which is not what I want here. ===== Script 1 ===== #!/bin/bash echo "Hola, mundo" ===== Script 2 ===== #!/bin/bash if w | grep -i root > /dev/null then echo "El usuario root está logueado" else echo "El usuario root NO está logueado" fi ===== Script 3 ===== #!/bin/bash if [ -f /utils/scripts/verxtech ] then echo "El archivo existe" else echo "El archivo NO existe" fi if [ -x /utils/scripts/verxtech ] then echo "El archivo tiene permisos de ejecución" else echo "El archivo NO tiene permisos de ejecución" fi ===== Script 4 ===== #!/bin/bash for i in joselo jose sergio alberto melisa felix do if [ $i = 'felix' ] then echo "$i es profe" else echo "$i es alumno" fi done ===== Script 5 ===== #!/bin/bash for i in $(ls /) do file /$i done ===== Script 6 ===== #!/bin/bash a=3344 for (( i=1; i<=$a; i=i+1 )) do echo "Número de ciclo: $i" done ===== Script 7 ===== #!/bin/bash # M="Febrero es el mes en que estamos" # El mes está indicado en el script # M=$1 # El mes es pasado como parámetro al script M="$(date +"%B de %Y")" # El mes es tomado con el comando date case $M in ene*|Ene*) echo "Estamos en enero" ;; feb*|Feb*) echo "Lo encontré!!!!!!!" echo "Estamos en febrero" ;; *) echo "Estamos en algún otro mes" ;; esac ===== Script 8 ===== #!/bin/bash # Recibe como parámetros dos números # y los compara. Además muestra info sobre # sí mismo VAR1=$1 VAR2=$2 if [ $VAR1 -lt $VAR2 ] then echo "\$VAR1 es menor" elif [ $VAR1 -eq $VAR2 ] then echo "\$VAR1 y \$VAR2 son iguales" else echo "\$VAR1 es mayor" fi echo "Numero de argumentos: $#" echo "Todos los argumentos: $*" echo "Opciones suministradas al shell: $-" echo "Valor de error del ultimo comando ejecutado: $?" echo "Identificacion del PID: $$" echo "Comando usado para ejecutar el script: $0" ===== Dialog test ===== #!/bin/bash # # Ejemplo de comando dialog # TMPFILE="/tmp/dialogtest.$$" dialog --inputbox "Ingrese su nombre" 10 30 2> $TMPFILE V=`cat $TMPFILE` # Borro el tmpfile rm -f $TMPFILE echo "El nombre que usted ingreso es $V" exit 0 ===== Agregar varios usuarios ===== #!/bin/bash # Crea usuarios a partir de una lista dada de # pares uid:passwd LISTA="usu1:plokij usu2:plokij usu3:plokij usu4:plokij" GRUPOPRIMARIO="users" GRUPOSSECUNDARIOS="audio,cdrom,dialout,disk,floppy" for i in $LISTA do USER=$(echo $i | cut -d: -f1) PASS=$(echo $i | cut -d: -f2) useradd -d /home/$USER -m -g $GRUPOPRIMARIO -G $GRUPOSSECUNDARIOS -s /bin/bash $USER echo "$USER:$PASS" | chpasswd --md5 done ===== Revisar si un demonio se esta ejecutando ===== #!/bin/bash # Revisa si un demonio se esta ejecutando # y en caso negativo lo inicia if ps ax | grep -i /usr/sbin/sshd | grep -v grep then echo "El SSH esta corriendo" else echo "El SSH _no_ esta corriendo" echo "Entonces, lo inicio" /etc/init.d/ssh start fi