Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[bash] assegnamenti e comandi [risolto]
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) Forum di discussione italiano
View previous topic :: View next topic  
Author Message
cloc3
Advocate
Advocate


Joined: 13 Jan 2004
Posts: 4815
Location: http://www.gentoo-users.org/user/cloc3/

PostPosted: Tue Aug 22, 2006 7:23 am    Post subject: [bash] assegnamenti e comandi [risolto] Reply with quote

Code:

cloc3@s939 ~ $ cat /etc/make.conf|grep PORTDIR=
PORTDIR="/mnt/tmp/portage"
cloc3@s939 ~ $ `cat /etc/make.conf|grep PORTDIR=`
bash: PORTDIR="/mnt/tmp/portage": No such file or directory

cilecca. la stringa `PORTDIR="/mnt/tmp/portage"` non è riconosciuta come un comando.

ho trovato un workaround, ma mi sembra goffo:
Code:

cloc3@s939 ~ $ PORTDIR=`cat /etc/make.conf|grep PORTDIR=`
cloc3@s939 ~ $ echo $PORTDIR
PORTDIR="/mnt/tmp/portage"
cloc3@s939 ~ $ PORTDIR=${PORTDIR/PORTDIR=}
cloc3@s939 ~ $ echo $PORTDIR
"/mnt/tmp/portage"

davvero sono necessari due passaggi?

ps: sbagliato forum: sorry
_________________
vu vu vu
gentù
mi piaci tu


Last edited by cloc3 on Tue Aug 22, 2006 12:42 pm; edited 1 time in total
Back to top
View user's profile Send private message
gutter
Bodhisattva
Bodhisattva


Joined: 13 Mar 2004
Posts: 7162
Location: Aarau, Aargau, Switzerland

PostPosted: Tue Aug 22, 2006 8:18 am    Post subject: Reply with quote

Moved from Forum italiano (Italian) to Forum di discussione italiano.

P.S.: Se non ho capito male (ma ho dubbi), quello che vorresti e':

Code:
$ export `cat /etc/make.conf|grep PORTDIR=`

_________________
Registered as User #281564 and Machines #163761
Back to top
View user's profile Send private message
Kernel78
Moderator
Moderator


Joined: 24 Jun 2005
Posts: 3654

PostPosted: Tue Aug 22, 2006 8:36 am    Post subject: Reply with quote

Premetto che nemmeno io sono sicuro di aver capito cosa vuoi ottenere ma faccio due appunti:
- cat è superfluo, basta usare grep PORTDIR= /etc/make.conf
- l'istruzione suggerita da gutter non penso sia adatta visto che assegnerebbe alla variabile il valore "/mnt/tmp/portage" (virgolette incluse)

Se quello che vuoi ottenere è la variabile valorizzata a /mnt/tmp/portage un trucco "sporco" è
Code:
export `grep PORTDIR= /etc/make.conf | tr -d "\""`
ma non è che mi piaccia molto, mi auguro che esista un metodo migliore ...
_________________
Le tre grandi virtù di un programmatore: pigrizia, impazienza e arroganza. (Larry Wall).
Prima di postare un file togli i commenti con
Code:
grep -vE '(^[[:space:]]*($|(#|!|;|//)))'
Back to top
View user's profile Send private message
cloc3
Advocate
Advocate


Joined: 13 Jan 2004
Posts: 4815
Location: http://www.gentoo-users.org/user/cloc3/

PostPosted: Tue Aug 22, 2006 10:44 am    Post subject: Reply with quote

grazie a entrambi.

mi dispiace un po' il fatto che, se da linea di comando digito semplicemente:
Code:

# PORTDIR="/mnt/pippo"

definisco correttamente la mia variabile.

Tra l'altro, mi chiedo se con export la variabile non risulti assegnata ad un ambiente più ampio.
_________________
vu vu vu
gentù
mi piaci tu
Back to top
View user's profile Send private message
randomaze
Bodhisattva
Bodhisattva


Joined: 21 Oct 2003
Posts: 9985

PostPosted: Tue Aug 22, 2006 11:41 am    Post subject: Re: [bash] assegnamenti e comandi Reply with quote

cloc3 wrote:
Code:

cloc3@s939 ~ $ PORTDIR=`cat /etc/make.conf|grep PORTDIR=`
cloc3@s939 ~ $ echo $PORTDIR
PORTDIR="/mnt/tmp/portage"
cloc3@s939 ~ $ PORTDIR=${PORTDIR/PORTDIR=}
cloc3@s939 ~ $ echo $PORTDIR
"/mnt/tmp/portage"

davvero sono necessari due passaggi?


No:

Code:
eval `cat /etc/make.conf |grep PORTDIR=`

_________________
Ciao da me!
Back to top
View user's profile Send private message
Kernel78
Moderator
Moderator


Joined: 24 Jun 2005
Posts: 3654

PostPosted: Tue Aug 22, 2006 11:57 am    Post subject: Re: [bash] assegnamenti e comandi Reply with quote

randomaze wrote:

Code:
eval `cat /etc/make.conf |grep PORTDIR=`

Questa è una soluzione elegante anche se torno a ribadire che l'uso di cat sia superfluo
Code:
eval `grep PORTDIR= /etc/make.conf`
funziona perfettamente
_________________
Le tre grandi virtù di un programmatore: pigrizia, impazienza e arroganza. (Larry Wall).
Prima di postare un file togli i commenti con
Code:
grep -vE '(^[[:space:]]*($|(#|!|;|//)))'
Back to top
View user's profile Send private message
randomaze
Bodhisattva
Bodhisattva


Joined: 21 Oct 2003
Posts: 9985

PostPosted: Tue Aug 22, 2006 12:33 pm    Post subject: Re: [bash] assegnamenti e comandi Reply with quote

Kernel78 wrote:
Questa è una soluzione elegante anche se torno a ribadire che l'uso di cat sia superfluo
Code:
eval `grep PORTDIR= /etc/make.conf`
funziona perfettamente


hai ragione, mi era sfuggito ;-)
_________________
Ciao da me!
Back to top
View user's profile Send private message
Luca89
Advocate
Advocate


Joined: 27 Apr 2005
Posts: 2107
Location: Agrigento (Italy)

PostPosted: Wed Aug 23, 2006 7:33 am    Post subject: Reply with quote

anche così sarebbe buono, credo:

Code:
# export PORTDIR=`portageq portdir`

_________________
Running Fast!
Back to top
View user's profile Send private message
gutter
Bodhisattva
Bodhisattva


Joined: 13 Mar 2004
Posts: 7162
Location: Aarau, Aargau, Switzerland

PostPosted: Wed Aug 23, 2006 10:20 am    Post subject: Reply with quote

Luca89 wrote:

Code:
# export PORTDIR=`portageq portdir`


Meno performante ma piu' elegante :D.
_________________
Registered as User #281564 and Machines #163761
Back to top
View user's profile Send private message
Kernel78
Moderator
Moderator


Joined: 24 Jun 2005
Posts: 3654

PostPosted: Wed Aug 23, 2006 11:08 am    Post subject: Reply with quote

Luca89 wrote:

Code:
# export PORTDIR=`portageq portdir`

Io pensavo che PORTDIR fosse solo un esempio ... ovviamente la tua soluzione funziona in questo caso ma non in quello generico ...
_________________
Le tre grandi virtù di un programmatore: pigrizia, impazienza e arroganza. (Larry Wall).
Prima di postare un file togli i commenti con
Code:
grep -vE '(^[[:space:]]*($|(#|!|;|//)))'
Back to top
View user's profile Send private message
Luca89
Advocate
Advocate


Joined: 27 Apr 2005
Posts: 2107
Location: Agrigento (Italy)

PostPosted: Wed Aug 23, 2006 11:43 am    Post subject: Reply with quote

Kernel78 wrote:
Io pensavo che PORTDIR fosse solo un esempio ... ovviamente la tua soluzione funziona in questo caso ma non in quello generico ...


Se deve prendere variabili da portage, portageq è la soluzione adatta, è fatto apposta per questo scopo. Se poi si tratta genericamente di prendere variabili bash da un file, accettando un po di spazzatura (variabili che sono definite nel file ma che non servono) si può semplicemente fare anche:

Code:
source /file

_________________
Running Fast!
Back to top
View user's profile Send private message
Kernel78
Moderator
Moderator


Joined: 24 Jun 2005
Posts: 3654

PostPosted: Wed Aug 23, 2006 11:46 am    Post subject: Reply with quote

Luca89 wrote:
Kernel78 wrote:
Io pensavo che PORTDIR fosse solo un esempio ... ovviamente la tua soluzione funziona in questo caso ma non in quello generico ...


Se deve prendere variabili da portage, portageq è la soluzione adatta, è fatto apposta per questo scopo. Se poi si tratta genericamente di prendere variabili bash da un file, accettando un po di spazzatura (variabili che sono definite nel file ma che non servono) si può semplicemente fare anche:

Code:
source /file

Questo lo so, semplicemente io pensavo che la richiesta fosse di estrarre da un file la valorizzazione di 1 sola variabile ...
_________________
Le tre grandi virtù di un programmatore: pigrizia, impazienza e arroganza. (Larry Wall).
Prima di postare un file togli i commenti con
Code:
grep -vE '(^[[:space:]]*($|(#|!|;|//)))'
Back to top
View user's profile Send private message
Luca89
Advocate
Advocate


Joined: 27 Apr 2005
Posts: 2107
Location: Agrigento (Italy)

PostPosted: Wed Aug 23, 2006 11:51 am    Post subject: Reply with quote

Kernel78 wrote:
Questo lo so, semplicemente io pensavo che la richiesta fosse di estrarre da un file la valorizzazione di 1 sola variabile ...


Il vero problema è allora che cloc3 doveva spiegare meglio cosa voleva fare. :)
_________________
Running Fast!
Back to top
View user's profile Send private message
Kernel78
Moderator
Moderator


Joined: 24 Jun 2005
Posts: 3654

PostPosted: Wed Aug 23, 2006 11:54 am    Post subject: Reply with quote

Luca89 wrote:
Kernel78 wrote:
Questo lo so, semplicemente io pensavo che la richiesta fosse di estrarre da un file la valorizzazione di 1 sola variabile ...


Il vero problema è allora che cloc3 doveva spiegare meglio cosa voleva fare. :)

Non dico mica che io ho capito correttamente quale risultato voleva ottenere, per le esigenze espresse tutte le soluzioni che abbiamo postato vanno cmq bene :wink:
_________________
Le tre grandi virtù di un programmatore: pigrizia, impazienza e arroganza. (Larry Wall).
Prima di postare un file togli i commenti con
Code:
grep -vE '(^[[:space:]]*($|(#|!|;|//)))'
Back to top
View user's profile Send private message
cloc3
Advocate
Advocate


Joined: 13 Jan 2004
Posts: 4815
Location: http://www.gentoo-users.org/user/cloc3/

PostPosted: Wed Aug 23, 2006 3:42 pm    Post subject: Reply with quote

Luca89 wrote:

Il vero problema è allora che cloc3 doveva spiegare meglio cosa voleva fare. :)

Per la verità, pensavo che i veri problemi, nella vita, fossero prevalentemente di donne 8) .

Per quanto riguarda il mio (piccolo) problema, mi infastidiva la differenza di trattamento tra assegnazione e comando.
Quindi le soluzioni alla kernel78 sono per lo più quelle che cercavo: evidentemente, per qualche motivo bash obbliga ad usare export oppure eval per raggiungere lo scopo.

Relativamente a portageq, ho il sospetto che, in questo caso, sia il classico ariete che schiaccia la formica.
Tuttavia è interessante ricavare le variabili di portage direttamente con l'uso delle routine incluse.
Così ho dato un occhiata a quel comando ... e mi è venuta voglia di aprire un altro post.

Andiamo a litigare di là?

:lol:
_________________
vu vu vu
gentù
mi piaci tu
Back to top
View user's profile Send private message
randomaze
Bodhisattva
Bodhisattva


Joined: 21 Oct 2003
Posts: 9985

PostPosted: Wed Aug 23, 2006 8:11 pm    Post subject: Reply with quote

cloc3 wrote:
evidentemente, per qualche motivo bash obbliga ad usare export oppure eval per raggiungere lo scopo.


sono due comandi totalmente diversi:

eval esegue il comando indicato

export esporta la variabile anche ai processi figli

credo che sia un limite del backtick piú che di bash.
_________________
Ciao da me!
Back to top
View user's profile Send private message
cloc3
Advocate
Advocate


Joined: 13 Jan 2004
Posts: 4815
Location: http://www.gentoo-users.org/user/cloc3/

PostPosted: Thu Aug 24, 2006 5:11 am    Post subject: Reply with quote

randomaze wrote:

credo che sia un limite del backtick piú che di bash.

backtik :?:
e che è?
_________________
vu vu vu
gentù
mi piaci tu
Back to top
View user's profile Send private message
gutter
Bodhisattva
Bodhisattva


Joined: 13 Mar 2004
Posts: 7162
Location: Aarau, Aargau, Switzerland

PostPosted: Thu Aug 24, 2006 7:34 am    Post subject: Reply with quote

cloc3 wrote:

backtik :?:
e che è?


Backtick = "Apice singolo inverso"
_________________
Registered as User #281564 and Machines #163761
Back to top
View user's profile Send private message
Kernel78
Moderator
Moderator


Joined: 24 Jun 2005
Posts: 3654

PostPosted: Thu Aug 24, 2006 7:38 am    Post subject: Reply with quote

cloc3 wrote:
randomaze wrote:

credo che sia un limite del backtick piú che di bash.

backtik :?:
e che è?

è la versione scomoda di $() :lol:
_________________
Le tre grandi virtù di un programmatore: pigrizia, impazienza e arroganza. (Larry Wall).
Prima di postare un file togli i commenti con
Code:
grep -vE '(^[[:space:]]*($|(#|!|;|//)))'
Back to top
View user's profile Send private message
cloc3
Advocate
Advocate


Joined: 13 Jan 2004
Posts: 4815
Location: http://www.gentoo-users.org/user/cloc3/

PostPosted: Thu Aug 24, 2006 9:03 am    Post subject: Reply with quote

Kernel78 wrote:
cloc3 wrote:
randomaze wrote:

credo che sia un limite del backtick piú che di bash.

backtik :?:
e che è?

è la versione scomoda di $() :lol:

Allora non è soltanto una limitazione del backtiK:
Code:

s939 ~ # unset PORTDIR
s939 ~ # $(cat /etc/make.conf|grep PORTDIR=)
-su: PORTDIR="/mnt/tmp/portage": No such file or directory
s939 ~ # eval $(cat /etc/make.conf|grep PORTDIR=)
s939 ~ # echo $PORTDIR
/mnt/tmp/portage
s939 ~ # unset PORTDIR
s939 ~ # `cat /etc/make.conf|grep PORTDIR=`
-su: PORTDIR="/mnt/tmp/portage": No such file or directory
s939 ~ # eval `cat /etc/make.conf|grep PORTDIR=`
s939 ~ # echo $PORTDIR
/mnt/tmp/portage
s939 ~ # unset PORTDIR
s939 ~ # PORTDIR=/etc/tmp/portage
s939 ~ # echo $PORTDIR
/etc/tmp/portage

_________________
vu vu vu
gentù
mi piaci tu
Back to top
View user's profile Send private message
Kernel78
Moderator
Moderator


Joined: 24 Jun 2005
Posts: 3654

PostPosted: Thu Aug 24, 2006 9:12 am    Post subject: Reply with quote

cloc3 wrote:
Allora non è soltanto una limitazione del backtiK

No però il backtik è scomodo se devi metterne uno dentro l'altro mentre $($()) funziona senza problemi :wink:
_________________
Le tre grandi virtù di un programmatore: pigrizia, impazienza e arroganza. (Larry Wall).
Prima di postare un file togli i commenti con
Code:
grep -vE '(^[[:space:]]*($|(#|!|;|//)))'
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) Forum di discussione italiano All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum