Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Bash - poslednji parametar?

[es] :: Skript jezici :: Bash - poslednji parametar?

[ Pregleda: 3116 | Odgovora: 5 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

niceness
Novi Sad

Član broj: 93992
Poruke: 993



+22 Profil

icon Bash - poslednji parametar?05.06.2006. u 14:20 - pre 172 meseci
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.
 
Odgovor na temu

Jbyn4e

Član broj: 422
Poruke: 6049
195.252.126.*



+256 Profil

icon Re: Bash - poslednji parametar?05.06.2006. u 14:58 - pre 172 meseci
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 :))
Kad sve ostalo zakaže, pročitaj uputstvo...
 
Odgovor na temu

niceness
Novi Sad

Član broj: 93992
Poruke: 993



+22 Profil

icon Re: Bash - poslednji parametar?05.06.2006. u 15:20 - pre 172 meseci
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 "[email protected]" |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]
 
Odgovor na temu

TiXo
Tihomir Pantović
Čačak

Član broj: 7796
Poruke: 537
*.smin.sezampro.yu.

Jabber: tixo@jabber.ru
ICQ: 74381511


Profil

icon Re: Bash - poslednji parametar?05.06.2006. u 15:32 - pre 172 meseci
Code:
#!/bin/bash
eval poslednji=\$$#
echo $#
echo $poslednji

GnuPG public key:
tixo.asc


Try and be nice to people, avoid eating fat, read a good book every now and
then, get some walking in, and try and live together in peace and harmony with
people of all creeds and nations.
 
Odgovor na temu

niceness
Novi Sad

Član broj: 93992
Poruke: 993



+22 Profil

icon Re: Bash - poslednji parametar?05.06.2006. u 15:49 - pre 172 meseci
Zanimljivo, hvala na odgovoru.
 
Odgovor na temu

random
Vladimir Vrzić
Beograd

Član broj: 85
Poruke: 3866
*.f.bg.ac.yu.

Sajt: www.last.fm/user/vrza


+4 Profil

icon Re: Bash - poslednji parametar?05.06.2006. u 17:55 - pre 172 meseci
Citat:
TiXo:
Code:
eval poslednji=\$$#


Iskusno, bravo! Posebno što i klasičan Bourne shell ima eval.
int rand(void);

Those who do not understand Unix are condemned to reinvent it, poorly.

Upali lampicu — koristi Jabber!
 
Odgovor na temu

[es] :: Skript jezici :: Bash - poslednji parametar?

[ Pregleda: 3116 | Odgovora: 5 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.