Linux/Unix Shell Scripting - [Bash]Saber valor del último parámetro sin bucle

 
Vista:
sin imagen de perfil

[Bash]Saber valor del último parámetro sin bucle

Publicado por j0sema (2 intervenciones) el 25/09/2011 23:48:22
Hola. Tengo un script que te dice el primer y último parámetro pasado. El caso es que si quiero saber el último parámetro lo hago con un bucle y haciendo un shift hasta finalmente mostrar $1 que tendrá el valor del último parámetro. La pregunta es como listo de forma directa el valor sin recorrerlo? He pesando en que si $# tiene el número de parámetros hacer como un $$# y mostrarlo pero no sé como. Espero su respuesta y dejo aquí la forma que tengo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
if [ $# -eq 0 ]
then
 
  echo "No has pasado ningún parámetro"
 
elif [ $# -eq 1 ]
then
  echo "Sólo hay un parámetro que es $1"
 
else
  echo "El primer argumento es $1"
  for i in `seq 2 1 $#`
  do
    shift
  done
  echo "El último argumento es $1"
fi
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

[Bash]Saber valor del último parámetro sin bucle

Publicado por Tom (361 intervenciones) el 26/09/2011 10:02:30
Prueba:

1
eval "echo \${$#}"
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

[Bash]Saber valor del último parámetro sin bucle

Publicado por j0sema (2 intervenciones) el 26/09/2011 12:21:13
Muchas gracias , funciona perfecto
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

[Bash]Saber valor del último parámetro sin bucle

Publicado por Salva (1 intervención) el 08/10/2012 16:41:21
Hoy también he necesitado eso y he encontrado esta otra forma:

1
${!#}


Lo hace mediante expansión indirecta. Lo he sacado de este párrafo del man bash:

If the first character of parameter is an exclamation point, a level of variable indirection is
introduced. Bash uses the value of the variable formed from the rest of parameter as the name of
the variable; this variable is then expanded and that value is used in the rest of the substitu-
tion, rather than the value of parameter itself. This is known as indirect expansion.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar