[ niceness @ 05.06.2006. 14:20 ] @
Kako odrediti poslednji parametar koji je prosledjen bash skripti.
Na komandnoj liniji se to moze uraditi sa Meta + . (to je kod mene Alt ili Esc + tacka) ili sa !$
Jedino sto sam do sada nasao je:
Code:
for zad_arg do :; done; echo $zad_arg


ali sigurno mora postojati neko bolje resenje.
[ Jbyn4e @ 05.06.2006. 14:58 ] @
google kaze:
sa http://tille.xalasys.com/training/bash/ch03s02.html
Citat:

The positional parameters are the words following the name of a shell script. They are put into the variables $1, $2, $3 and so on. As long as needed, variables are added to an internal array. $# holds the total number of parameters, as is demonstrated with this simple script:

#!/bin/bash

# positional.sh
# This script reads 3 positional parameters and prints them out.

POSPAR1="$1"
POSPAR2="$2"
POSPAR3="$3"

echo "$1 is the first positional parameter, \$1."
echo "$2 is the second positional parameter, \$2."
echo "$3 is the third positional parameter, \$3."
echo
echo "The total number of positional parameters is $#."

Upon execution one could give any numbers of arguments:

franky ~> positional.sh one two three four five
one is the first positional parameter, $1.
two is the second positional parameter, $2.
three is the third positional parameter, $3.

The total number of positional parameters is 5.


kao i sa
http://linuxhelp.blogspot.com/...conds-guide-to-bash-shell.html

Citat:

A few special symbols and their meanings w.r.t shell scripts

$* - This denotes all the parameters passed to the script
at the time of its execution. Which includes $1, $2
and so on.
$0 - Name of the shell script being executed.
$# - Number of arguments specified in the command line.
$? - Exit status of the last command.

The above symbols are known as positional parameters. Let me explain the positional parameters with the aid of an example. Suppose I have a shell script called my_script.sh . Now I execute this script in the command line as follows :

$ ./my_script.sh linux is a robust OS

... as you can see above, I have passed 5 parameters to the script. In this scenario, the values of the positional parameters are as follows:
$* - will contain the values 'linux','is','a','robust','OS'.
$0 - will contain the value my_script.sh - the name of the script being
executed.
$# - contains the value 5 - the total number of parameters.
$$ - contains the process ID of the current shell. You can use this parameter while giving unique names to any temporary files that you create at the time of execution of the shell.

$1 - contains the value 'linux'
$2 - contains the value 'is'
... and so on.


P.S. trazio sam na reci "bash number of parameters", ovi su ti drugi i treci u listi.... stvarno ne znam kako ih nisi nasao :))
[ niceness @ 05.06.2006. 15:20 ] @
Izgleda da nisam dobro postavio pitanje.
Znaci, kako odrediti koji je poslednji parametar kada nije nije poznat broj datih parametara?
Nadam se da sam sada bio jasniji.

EDIT:
Moze se i sa awk:
Code:
echo "$@" |awk '{print $'$#'}'

Ima li neki jednostavniji nacin sa koriscenjem bash-ovih builtin komandi?

[Ovu poruku je menjao niceness dana 05.06.2006. u 16:32 GMT+1]
[ TiXo @ 05.06.2006. 15:32 ] @
Code:
#!/bin/bash
eval poslednji=\$$#
echo $#
echo $poslednji
[ niceness @ 05.06.2006. 15:49 ] @
Zanimljivo, hvala na odgovoru.
[ random @ 05.06.2006. 17:55 ] @
Citat:
TiXo:
Code:
eval poslednji=\$$#


Iskusno, bravo! Posebno što i klasičan Bourne shell ima eval.