View previous topic :: View next topic |
Author |
Message |
cloc3 Advocate


Joined: 13 Jan 2004 Posts: 4815 Location: http://www.gentoo-users.org/user/cloc3/
|
Posted: Tue Aug 22, 2006 7:23 am Post subject: [bash] assegnamenti e comandi [risolto] |
|
|
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 |
|
 |
gutter Bodhisattva


Joined: 13 Mar 2004 Posts: 7162 Location: Aarau, Aargau, Switzerland
|
Posted: Tue Aug 22, 2006 8:18 am Post subject: |
|
|
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 |
|
 |
Kernel78 Moderator

Joined: 24 Jun 2005 Posts: 3654
|
Posted: Tue Aug 22, 2006 8:36 am Post subject: |
|
|
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 |
|
 |
cloc3 Advocate


Joined: 13 Jan 2004 Posts: 4815 Location: http://www.gentoo-users.org/user/cloc3/
|
Posted: Tue Aug 22, 2006 10:44 am Post subject: |
|
|
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 |
|
 |
randomaze Bodhisattva


Joined: 21 Oct 2003 Posts: 9985
|
Posted: Tue Aug 22, 2006 11:41 am Post subject: Re: [bash] assegnamenti e comandi |
|
|
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 |
|
 |
Kernel78 Moderator

Joined: 24 Jun 2005 Posts: 3654
|
Posted: Tue Aug 22, 2006 11:57 am Post subject: Re: [bash] assegnamenti e comandi |
|
|
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 |
|
 |
randomaze Bodhisattva


Joined: 21 Oct 2003 Posts: 9985
|
Posted: Tue Aug 22, 2006 12:33 pm Post subject: Re: [bash] assegnamenti e comandi |
|
|
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 |
|
 |
Luca89 Advocate


Joined: 27 Apr 2005 Posts: 2107 Location: Agrigento (Italy)
|
Posted: Wed Aug 23, 2006 7:33 am Post subject: |
|
|
anche così sarebbe buono, credo:
Code: | # export PORTDIR=`portageq portdir` |
_________________ Running Fast! |
|
Back to top |
|
 |
gutter Bodhisattva


Joined: 13 Mar 2004 Posts: 7162 Location: Aarau, Aargau, Switzerland
|
Posted: Wed Aug 23, 2006 10:20 am Post subject: |
|
|
Luca89 wrote: |
Code: | # export PORTDIR=`portageq portdir` |
|
Meno performante ma piu' elegante . _________________ Registered as User #281564 and Machines #163761 |
|
Back to top |
|
 |
Kernel78 Moderator

Joined: 24 Jun 2005 Posts: 3654
|
Posted: Wed Aug 23, 2006 11:08 am Post subject: |
|
|
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 |
|
 |
Luca89 Advocate


Joined: 27 Apr 2005 Posts: 2107 Location: Agrigento (Italy)
|
Posted: Wed Aug 23, 2006 11:43 am Post subject: |
|
|
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:
_________________ Running Fast! |
|
Back to top |
|
 |
Kernel78 Moderator

Joined: 24 Jun 2005 Posts: 3654
|
Posted: Wed Aug 23, 2006 11:46 am Post subject: |
|
|
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:
|
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 |
|
 |
Luca89 Advocate


Joined: 27 Apr 2005 Posts: 2107 Location: Agrigento (Italy)
|
Posted: Wed Aug 23, 2006 11:51 am Post subject: |
|
|
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 |
|
 |
Kernel78 Moderator

Joined: 24 Jun 2005 Posts: 3654
|
Posted: Wed Aug 23, 2006 11:54 am Post subject: |
|
|
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  _________________ 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 |
|
 |
cloc3 Advocate


Joined: 13 Jan 2004 Posts: 4815 Location: http://www.gentoo-users.org/user/cloc3/
|
Posted: Wed Aug 23, 2006 3:42 pm Post subject: |
|
|
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 .
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à?
 _________________ vu vu vu
gentù
mi piaci tu |
|
Back to top |
|
 |
randomaze Bodhisattva


Joined: 21 Oct 2003 Posts: 9985
|
Posted: Wed Aug 23, 2006 8:11 pm Post subject: |
|
|
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 |
|
 |
cloc3 Advocate


Joined: 13 Jan 2004 Posts: 4815 Location: http://www.gentoo-users.org/user/cloc3/
|
Posted: Thu Aug 24, 2006 5:11 am Post subject: |
|
|
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 |
|
 |
gutter Bodhisattva


Joined: 13 Mar 2004 Posts: 7162 Location: Aarau, Aargau, Switzerland
|
Posted: Thu Aug 24, 2006 7:34 am Post subject: |
|
|
Backtick = "Apice singolo inverso" _________________ Registered as User #281564 and Machines #163761 |
|
Back to top |
|
 |
Kernel78 Moderator

Joined: 24 Jun 2005 Posts: 3654
|
Posted: Thu Aug 24, 2006 7:38 am Post subject: |
|
|
cloc3 wrote: | randomaze wrote: |
credo che sia un limite del backtick piú che di bash. |
backtik
e che è? |
è la versione scomoda di $()  _________________ 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 |
|
 |
cloc3 Advocate


Joined: 13 Jan 2004 Posts: 4815 Location: http://www.gentoo-users.org/user/cloc3/
|
Posted: Thu Aug 24, 2006 9:03 am Post subject: |
|
|
Kernel78 wrote: | cloc3 wrote: | randomaze wrote: |
credo che sia un limite del backtick piú che di bash. |
backtik
e che è? |
è la versione scomoda di $()  |
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 |
|
 |
Kernel78 Moderator

Joined: 24 Jun 2005 Posts: 3654
|
Posted: Thu Aug 24, 2006 9:12 am Post subject: |
|
|
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  _________________ 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 |
|
 |
|