PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > [bash] last element of the $* array
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

[bash] last element of the $* array

Réponse
 
LinkBack Outils de la discussion
Vieux 01/11/2007, 00h30   #1
Yakov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut [bash] last element of the $* array

How do I refer to the last element of args array
(the $* array) in bash ?

For example: set -- a b c; echo ${...} ... I want 'c' printed here.
I tried ${*[-1]}, bash refuses to read my mind.

Thanks
Yakov

  Réponse avec citation
Vieux 01/11/2007, 00h52   #2
Wayne
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [bash] last element of the $* array

Yakov wrote:
> How do I refer to the last element of args array
> (the $* array) in bash ?
>
> For example: set -- a b c; echo ${...} ... I want 'c' printed here.
> I tried ${*[-1]}, bash refuses to read my mind.
>
> Thanks
> Yakov
>


Try:
echo ${!#}
  Réponse avec citation
Vieux 01/11/2007, 05h44   #3
Jstein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: last element of the $* array

Yakov,

The variable $# tells you how many positional parameters exist in $1,
$2, $3, $4 ... $N

Under OLD shells, like the original bourne, you couls only reach $1 to
$9, and all othes
were un-touchable, until you used the "shift" command to get rid of
the first parameters,
and bring the other ones down below position ten. So people used to
write loops like:

while [ $# -gt 0 ]
do
#- process your parameters, using $1 and possibly some $2 or $3
to follow
#- set up various other info
#- perhaps store some of the params of interest
KEEP_PARAMS="$KEEP_PARAMS $1"

shift
#- this discards $1, and shifts $2 into the current $1
done

#- When you were done you could put parts of them back (if you really
wanted) with
set -- $KEEP_PARAMS
#- and the ones you liked would go back into $1, $2 ... $N

Now, several shells will allow you to access the params past $9
directly using notations like ${52}
or, if you lave a counter variable, like HERE, where HERE=52, you
could access that param with
the notation of ${!HERE} (the exclamation point says, use the value
of HERE as a pointer/reference)

That should get you what you need.

  Réponse avec citation
Vieux 01/11/2007, 10h01   #4
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [bash] last element of the $* array

2007-10-31, 23:30(-00), Yakov:
> How do I refer to the last element of args array
> (the $* array) in bash ?
>
> For example: set -- a b c; echo ${...} ... I want 'c' printed here.
> I tried ${*[-1]}, bash refuses to read my mind.

[...]

Bash specific:

last=${!#}

(note how the above nicely conflicts with the ${var#pattern}
which is why it doesn't work in ksh93)

Bash/ksh93 specific:

last=${@: -1}

Zsh equivalents:

last=${(P)#}
last=$@[-1]

POSIXly:

eval "last=\${$#}"

Bourne:

for last do :; done

--
Stéphane
  Réponse avec citation
Vieux 01/11/2007, 13h38   #5
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [bash] last element of the $* array

Yakov wrote:
> How do I refer to the last element of args array
> (the $* array) in bash ?


Another option...

: "$@"
last=$_


Janis

>
> For example: set -- a b c; echo ${...} ... I want 'c' printed here.
> I tried ${*[-1]}, bash refuses to read my mind.
>
> Thanks
> Yakov
>

  Réponse avec citation
Vieux 02/11/2007, 22h35   #6
bsh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: last element of the $* array

Yakov <iler...@gmail.com> wrote:
> How do I refer to the last element of an array?


A portable, robust, and efficient method that has kept
me in good stead for many years:

eval "set X \"\$@\"; shift $#"

=Brian

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h50.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12234 seconds with 14 queries