| Asunto: | [glisc] Introducción a BASH shell scripting | | Fecha: | Miercoles, 2 de Abril, 2003 16:32:13 (-0400) | | Autor: | Alfonso Fernandez <afernandez @........bo>
|
Introduction to Shell Scripting
http://www.linuxgazette.com/issue53/okopnik.html
"FOR;DO;DONE"
Often, scripts are written to automate some repetitive task; as a random
example, if you have to repeatedly edit a series of files in a specific
directory, you might have a script that looks like this:
---------------------------------------------------------------------------
-
#!/bin/bash
for n in ~/weekly/*.txt
do
ae $n
done
echo "Done."
---------------------------------------------------------------------------
-
#!/bin/bash
for n in ~/weekly/*.txt; do ae $n; done; echo "Done."
"WHILE;DO;DONE"
Often, we need a control mechanism that acts based on a specified condition
rather than iterating through a list. The 'while' loop fills this
requirement:
---------------------------------------------------------------------------
-----
#!/bin/bash
pppd call provider &
while [ -n "$(ping -c 1 192.168.0.1|grep 100%)" ]
do
echo "Connecting..."
done
echo "Connection established."
"UNTIL;DO;DONE"
The 'until' loop is the reverse of the 'while' - it continues to loop as
long as the test is false, and fails when it becomes true. I've never had
the occasion to use it; the 'while' loop and the flexibility of the
available tests have sufficed for everything I've needed so far.
"IF;THEN;[ELSE];FI"
There are many times when we just need to check for the existence of a
condition and branch the execution based on the result. For those times, we
have the 'if' statement:
---------------------------------------------------------------------------
----
...
if [ $BOSS="jerk" ]
then
echo 'Take this job and shove it!'
else
echo 'Stick around; the money is good.'
fi
...
"CASE;IN;;ESAC"
The remaining tool that we can use for conditional branching is basically a
multiple 'if' statement, based on the evaluation of a test. If, for
example, we know that the only possible outputs from an imaginary program
called 'intel_cpu_test' are 4, 8, 16, 32, or 64, then we can write the
following:
---------------------------------------------------------------------------
-----
#!/bin/bash
case $(intel_cpu_test) in
4) echo "You're running Linux on a calculator??";;
8) echo "That 8088 is past retirement age...";;
16) echo "A 286 kinda guy, are you?";;
32) echo "One of them new-fangled gadgets!";;
64) echo "Oooh... serious CPU envy!";;
*) echo "What the heck are you running, anyway?";;
esac
BREAK and CONTINUE
These statements interrupt the program flow in specific ways. The "break",
once executed, immediately exits the enclosing loop; the "continue"
statement skips the current loop iteration. This is useful in a number of
situations, particularly in long loops where the existence of a given
condition makes all further tests unnecessary.
DATE EXAMPLE
---------------------------------------------------------------------------
-----
#!/bin/bash
# "bkup" - copies specified files to the user's ~/Backup
# directory after checking for name conflicts.
a=$(date +%T-%d_%m_%Y)
cp -i $1 ~/Backup/$1.$a
_______________________________________________________________________
~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~
!!Tu correo gratuito para siempre!!
10 megabytes de espacio, agenda de direcciones, filtro anti-spam, etc.
garantizado de funcionar siempre con eListas.net
y por supuesto con el resto de Internet...
Y si lo deseas, obten igualmente revistas gratis en tu casa,
acceso libre a webs exclusivas, descargas, libros electrónicos...
y sorteos trimestrales de 1.000 euros.
!!En GeoMundos!!
Haz clic aquí ¡YA! -> http://elistas.net/ml/119/
~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~
|