Shell issue

General FreeBASIC programming questions.
Post Reply
SARG
Posts: 1766
Joined: May 27, 2005 7:15
Location: FRANCE

Shell issue

Post by SARG »

When using shell the first space is used as a separator although there are quotes.

Using this piece of code

Code: Select all

Shell("""c:\temp\toto test.bat""")
Sleep
gives the following message :
'c:\temp\toto' n'est pas reconnu en tant que commande interne ou externe, un programme exécutable ou un fichier de commandes.
is not recognized as an intern command or extern, a executable program or a command file.

Dont' seem to be a normal behaviour as on a command window using "c:\temp\toto test.bat" gives
'"c:\temp\toto test.bat"' n'est pas reconnu en tant que commande interne ou externe, un programme exécutable ou un fichier de commandes. All the line is taken in account. Obviously in this example the 'toto test.bat doesn't exist.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Shell issue

Post by fxm »

Under French Windows XP SP3, I obtain this other generic error message (same message for the two test cases):
Le chemin d'accès spécifié est introuvable.

Command window (cmd.exe):
Microsoft Windows XP [version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
djsfantasi
Posts: 87
Joined: May 21, 2010 17:38
Location: Massachusetts, USA
Contact:

Re: Shell issue

Post by djsfantasi »

I am curious...

Do you mean to execute the following?
Shell("""c:\temp\toto\test.bat""")

Otherwise Windows may be looking for a folder named "toto test.bat" (Yes, you can have .'s in a folder name...)
SARG
Posts: 1766
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Shell issue

Post by SARG »

Sorry not to be clear ;-)

It seems that the quotes are not taken in account when using the SHELL command.
The error message shows only 'c:\temp\toto'.

Directly in a command window you get ' "c:\temp\toto test.bat" ' the entire string.
As you can see in the first error message there is no quote.


@fxm are you just using the two lines of code I gave? Tested also under XP SP3.

@djsfantasy it's an example the name has no signification. Just see that the name in the error message is truncated although there are quotes to keep the whole name.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Shell issue

Post by fxm »

SARG wrote:@fxm are you just using the two lines of code I gave? Tested also under XP SP3.
Yes, exactly your two lines of code!
codeFoil
Posts: 256
Joined: Dec 22, 2011 4:45
Location: United States
Contact:

Re: Shell issue

Post by codeFoil »

fb_Shell passes the string data unmodified to the C library routine system(). On Win32, I think I recall finding that this eventually results in a call to a routine in MSCRT.DLL which uses the current value of the COMSPEC environment variable to build a command line.

We can get an idea of what is happening after the flow of execution passes outside the FreeBASIC runtime by forcing shell to invoke another program.

Code: Select all

'nulcmd.bas 
#Define ALL_PARAMETERS -1
Print  Command(ALL_PARAMETERS )

Code: Select all

'test.bas
SetEnviron "COMSPEC=nulcmd.exe"
Shell("""c:\temp\toto test.bas""" )
Sleep
Last edited by codeFoil on Apr 12, 2012 21:11, edited 1 time in total.
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Shell issue

Post by dkl »

FB's Shell() uses the CRT's system() (without parsing the command string), and it seems like system(command) just launches "cmd.exe /c <command>". (If you use shell() to run an existing .bat with a "pause" in it, you can see the cmd.exe process and its command line via Process Explorer)

I'm not sure how cmd.exe works exactly (how it handles its command line), but it seems like it will first try the whole quoted command, and if that fails apparently it drops the quotes and re-parses the command, as a fallback mechanism in case the user did something like <cmd.exe /c "echo foo">. As you've seen, that doesn't seem to happen when you type the command into cmd.exe's command prompt though.
C:\>cmd.exe /c "C:\temp\toto test.bat"
Der Befehl "C:\temp\toto" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.

C:\>"C:\temp\toto test.bat"
Der Befehl ""C:\temp\toto test.bat"" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
and for comparison:
C:\>echo foo
foo

C:\>"echo foo"
Der Befehl ""echo foo"" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.

C:\>cmd.exe /c echo foo
foo

C:\>cmd.exe /c "echo foo"
foo
codeFoil
Posts: 256
Joined: Dec 22, 2011 4:45
Location: United States
Contact:

Re: Shell issue

Post by codeFoil »

The issue is two fold. First, the CRT is stripping double quotes from the string. Second, cmd.exe handles quotes in a complicated manner.

From the output of Cmd /?

Code: Select all

If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

    1.  If all of the following conditions are met, then quote characters
        on the command line are preserved:

        - no /S switch
        - exactly two quote characters
        - no special characters between the two quote characters,
          where special is one of: &<>()@^|
        - there are one or more whitespace characters between the
          the two quote characters
        - the string between the two quote characters is the name
          of an executable file.

    2.  Otherwise, old behavior is to see if the first character is
        a quote character and if so, strip the leading character and
        remove the last quote character on the command line, preserving
        any text after the last quote character.

SARG
Posts: 1766
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Shell issue

Post by SARG »

@fxm
I guess that the 'c:\temp' directory doesn't exist in your PC.

@dkl and codeFoil
Thanks for your explanations.

In fact the goal was to find the right syntax to execute this shell command :

Code: Select all

dim as string fbcexe="c:\freebasic v23\fbcexe",fname="c:\my dir\test.bas":cmdlfbc=""

Shell(""""+fbcexe+""" """+fname+""" -w 1 -g "+cmdlfbc+" >"""+ExePath+"\fbdebug_compil.log""")
The command should be : shell("c:\freebasic v23\fbcexe" "c:\my dir\test.bas" etc)

There may be spaces inside every file name which are causing some issues.
I was not able to find that syntax, always an error message with the dir name truncated after the first space :
'c:\freebasic'

Nota : the excess space characters near parameters or inside quotes have been added when posting.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Shell issue

Post by fxm »

SARG wrote:@fxm
I guess that the 'c:\temp' directory doesn't exist in your PC.
Yes.
SARG
Posts: 1766
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Shell issue

Post by SARG »

This syntax works :

Code: Select all

Shell(""""""+fbcexe+""""+" -w 1 -g "+""""+fname+""""+cmdlfbc+" >"""+ExePath+"\fbdebug_compil.log""")
Post Reply