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 > variable expansion
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

variable expansion

Réponse
 
LinkBack Outils de la discussion
Vieux 10/11/2007, 12h15   #1
Luntain
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut variable expansion

I want to define a variable pointing to my desktop location, in bash I say

desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""

When I echo it looks ok, and yet when I input cd $desktop it complaints
that there is no /cygdrive/c/Dokumente directory. wtf? I also tried
defining desktop using '\ ', that is

desktop="/cygdrive/c/Dokumente\\ und\\ Ein...

I do it obviously in cygwin bash, and I don't know if it would work on
Linux.
  Réponse avec citation
Vieux 10/11/2007, 13h25   #2
Kenan Kalajdzic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable expansion

Luntain <news.20.luntain@spamgourmet.com> wrote:
> I want to define a variable pointing to my desktop location, in bash I say
>
> desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""
>
> When I echo it looks ok, and yet when I input cd $desktop it complaints


If the value of a variable contains whitespace, you must quote the
variable name when you reference it, if you want shell to treat it
as a single argument. Otherwise, shell interprets the spaces as
argument delimiters.

This means,

cd $desktop

is interpreted as a cd command with three arguments:

cd "/cygdrive/c/Dokumente" "und" "Einstellungen/Luntain/Desktop"

The cd command uses a single argument, so it will attempt to change
your working directory to /cygdrive/c/Dokumente, which does not exist.
If you surround the referenced variable name with double quotes,

cd "$desktop"

the shell will preserve the spaces and interpret this as a cd command
with a single argument

cd "/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop"

which is what you desire.

--
Kenan Kalajdzic
  Réponse avec citation
Vieux 10/11/2007, 14h50   #3
Luntain
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable expansion

>> I want to define a variable pointing to my desktop location, in bash I say
>>
>> desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""
>>
>> When I echo it looks ok, and yet when I input cd $desktop it complaints

>
> If the value of a variable contains whitespace, you must quote the
> variable name when you reference it, if you want shell to treat it
> as a single argument. Otherwise, shell interprets the spaces as
> argument delimiters.
>
> This means,
>
> cd $desktop
>
> is interpreted as a cd command with three arguments:
>
> cd "/cygdrive/c/Dokumente" "und" "Einstellungen/Luntain/Desktop"
>
> The cd command uses a single argument, so it will attempt to change
> your working directory to /cygdrive/c/Dokumente, which does not exist.
> If you surround the referenced variable name with double quotes,
>
> cd "$desktop"
>
> the shell will preserve the spaces and interpret this as a cd command
> with a single argument
>
> cd "/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop"
>
> which is what you desire.
>


I am not that lame not to now that spaces seperate arguments.

I would like to point out that I assign a quoted string to desktop variable:

desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""

when I echo $desktop, the path is displayes with the enclosing quotes. I
don't understand why cd doesn't seem to see the quotes. I want to have a
quick way of changing to my desktop directory, this is the hole point of
that desktop variable. There must be a way.
  Réponse avec citation
Vieux 10/11/2007, 15h02   #4
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable expansion

On 2007-11-10, Luntain <news.20.luntain@spamgourmet.com> wrote:
>
> I am not that lame not to now that spaces seperate arguments.
>
> I would like to point out that I assign a quoted string to desktop variable:
>
> desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""
>
> when I echo $desktop, the path is displayes with the enclosing quotes. I
> don't understand why cd doesn't seem to see the quotes. I want to have a
> quick way of changing to my desktop directory, this is the hole point of
> that desktop variable. There must be a way.


cd "$desktop" is one way. Another way is to use the CDPATH variable.
  Réponse avec citation
Vieux 10/11/2007, 16h17   #5
Claudio
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable expansion

On 2007-11-10, Luntain <news.20.luntain@spamgourmet.com> wrote:
>
> desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""
>
> when I echo $desktop, the path is displayes with the enclosing quotes.


This is the expected behaviour. You can have a directory with
a name like: abc"xyz
in that case, you can cd in this way:
cd abc\"xyz

try: desktop="bla bla bla with as many / as you want"

But the \ is the scape character (special meaning). If you want a \
without special meaning, use \\

> I
> don't understand why cd doesn't seem to see the quotes. I want to have a
> quick way of changing to my desktop directory, this is the hole point of
> that desktop variable. There must be a way.


try just:

cd

Best regards,
Claudio.

(I apologize my bad english)
  Réponse avec citation
Vieux 10/11/2007, 17h18   #6
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable expansion

On 2007-11-10, Luntain wrote:
>
> I want to define a variable pointing to my desktop location, in bash I say
>
> desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""
>
> When I echo it looks ok, and yet when I input cd $desktop it complaints
> that there is no /cygdrive/c/Dokumente directory. wtf? I also tried
> defining desktop using '\ ', that is
>
> desktop="/cygdrive/c/Dokumente\\ und\\ Ein...
>
> I do it obviously in cygwin bash, and I don't know if it would work on
> Linux.


The quotes are not part of the path; remove them.

desktop="/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop"

When you use it, quote the variable, e.g.:

cd "$desktop"

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 10/11/2007, 17h49   #7
Barry Margolin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable expansion

In article <AyjZi.45383$c_1.39330@text.news.blueyonder.co.uk> ,
Luntain <news.20.luntain@spamgourmet.com> wrote:

> I would like to point out that I assign a quoted string to desktop variable:
>
> desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""
>
> when I echo $desktop, the path is displayes with the enclosing quotes. I
> don't understand why cd doesn't seem to see the quotes. I want to have a
> quick way of changing to my desktop directory, this is the hole point of
> that desktop variable. There must be a way.


Quote processing is not performed on the result of variable expansion.
So it's looking for a filename that actually starts with doublequote.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
  Réponse avec citation
Vieux 10/11/2007, 20h18   #8
Luntain
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable expansion

>> I am not that lame not to now that spaces seperate arguments.
>>
>> I would like to point out that I assign a quoted string to desktop variable:
>>
>> desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""
>>
>> when I echo $desktop, the path is displayes with the enclosing quotes. I
>> don't understand why cd doesn't seem to see the quotes. I want to have a
>> quick way of changing to my desktop directory, this is the hole point of
>> that desktop variable. There must be a way.

>
> cd "$desktop" is one way. Another way is to use the CDPATH variable.


CDPATH is what I need, thx.

CDPATH=".:/cygdrive/c/Dokumente und Einstellungen/Luntain"
  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 08h07.


É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,12803 seconds with 16 queries