+ All Categories
Home > Documents > Ejercicios_4 Sebastian Rubio Gonzalez

Ejercicios_4 Sebastian Rubio Gonzalez

Date post: 20-Nov-2015
Category:
Upload: sebastian-rubio-gonzalez
View: 9 times
Download: 6 times
Share this document with a friend
Description:
Ejercicios shell
5
Ejercicios_4 Programación Shell. 1 Crea un script que pida tres números y que los ordene de mayor a menor. #!/bin/bash read –p “Introduce tres números separados por un espacio: “ a b c if test $c –gt $a –a $c –gt $b then if test $a –gt $b then num1=$c num2=$a num3=$b else num1=$c num2=$b num3=$a fi elif test $b -gt $a –a $b –gt $c then if test $a –gt $c then num1=$b num2=$a num3=$c else num1=$b num2=$c num3=$a fi elif test $a –gt $b –a $a –gt $c then if test $b –gt $c then num1=$a num2=$b num3=$c else num1=$a num2=$c num3=$b fi fi echo $num1, $num2, $num3
Transcript

Ejercicios_3 Programacin Shell

Ejercicios_4 Programacin Shell.

1 Crea un script que pida tres nmeros y que los ordene de mayor a menor.

#!/bin/bash

read p Introduce tres nmeros separados por un espacio: a b cif test $c gt $a a $c gt $b

then

if test $a gt $b

then

num1=$c

num2=$a

num3=$b else

num1=$c num2=$b

num3=$a fi

elif test $b -gt $a a $b gt $c

then

if test $a gt $c

then

num1=$b

num2=$a

num3=$c else

num1=$b

num2=$c

num3=$a fi

elif test $a gt $b a $a gt $c then

if test $b gt $c then

num1=$a

num2=$b

num3=$c else

num1=$a

num2=$c

num3=$b fi

fi

echo $num1, $num2, $num3Y el resultado final con su comprobacin ser

2 Sobre el ejercicio anterior realizar las modificaciones para que los tres nmeros sean parmetros de invocacin al script. Y lo primero que haga el script es comprobar si se han introducido realmente 3 parmetros. En caso contrario sacar mensaje Lo siento. Debe introducir 3 nmeros.

#!/bin/bash

if test $# -eq 3

then

if test $3 gt $1 a $3 gt $2

then

if test $1 gt $2

then

echo $3, $1, $2 else

echo $3, $2, $1

fi

fi

if test $2 gt $1 a $2 gt $3

then

if test $1 gt $3

then

echo $2, $1, $3

else

echo $2, $3, $1 fi

fi

if test $1 gt $2 a $1 gt $3

then

if test $2 gt $3

then

echo $1, $2, $3

else

echo $1, $3, $2

fi

fi

else echo Lo siento. Debe introducir los tres nmeros

fi

Y el resultado final con sus comprobaciones ser

3 Realizar un script que compare dos archivos (que se reciben por parmetro) y muestre por pantalla cul de ellos tiene mayor nmero de lneas.

#!/bin/bashif test $# -eq 2 #si el numero total de parmetros es equivalente a 2

then

lineas1=`wc l $1 | cut d f1` #cuenta las lneas del fichero 1 lineas2=`wc l $2 | cut d f1` #cuenta las lneas del fichero 2 if test $lineas1 gt $lineas2

then

echo $1 tiene mas lneas que $2

else

echo $2 tiene mas lneas que $1

else Lo siento. Tienes que introducir dos ficheros

fi

4 Crear un script que reciba como parmetro un directorio, y a continuacin una lista de archivos. Una vez validado si todo es correcto copiar los archivos en el directorio.#!/bin/bashif [ ! d $1 ] #si no esta creado el directorio ASO

then

dir=$1

mkdir $dir #crea el directorio

shift

while test $# -ne #cuando $# no sea 0

do

cp v $1 $dir #copia y visualiza el archivo que se corresponde con $1 y con $dir shift

done

fi


Recommended