• Print

Author Topic: Shell command  (Read 126 times)

wiggins

  • Jr. Member
  • **
  • Posts: 50
Shell command
« on: March 20, 2013, 03:18:12 PM »
The documentation for SHELL states "Some commands may not work without adding CMD /C to the start of the command line. "

My questions are: 

Is there any downside to adding "cmd /c" in front of all shelled commands in my program? 
Example: Shell "CMD /C NET USE k: \\SERVERNAME"
                Shell "CMD /C IF Exist C:\temp\file.txt delete c:\temp\file.txt"

Is there any way to know which programs are likely to require CMD /C?

DSMan195276

  • Hero Member
  • *****
  • Posts: 1978
  • Yes
    • Email
Re: Shell command
« Reply #1 on: March 20, 2013, 04:55:19 PM »
'cmd /c' spawns a 'cmd' process which executes your command (that's what the /c flag is for) where as a straight SHELL just sends your text over to the 'system' command in C, which I'm assuming doesn't need to spawn a new cmd to work (I think 'system()' can only executable programs, not run commands). Making another cmd process is no big deal really, except that if you use the _SHELL() function and try to get the return code from a process you may end-up getting the code from the cmd process instead of your actual command, or IE not the code you were intending to get. That's not really an issue here, as you're not using _SHELL().

The problem comes in when you have things that are maybe not 'commands' but instead statements that work in cmd. On a Unix system, the shell has no real 'built-in commands' (Mostly anyway), every command is simply an executable file. Because executable files on Unix do not have extensions like 'EXE', they all look like normal commands. Windows inherited some of this into DOS, but blurred the line between programs and built-in commands. Windows executables have EXE on the end, but cmd doesn't require you to include their extension to run them (Probably because Unix doesn't have extensions). But the 'cmd' also supports many commands built right into it that don't have any programs representing them (Like 'copy' and 'cd'). Because of these distinctions, the built-in commands and programs are somewhat blurred if you always run them without including their extension. However, since SHELL does not by default run your command through 'cmd' it doesn't do all of the parsing that CMD will do, such as running an executable without the 'EXE' on the end, or running an 'IF' statement. To do all of that you need to run your statement through CMD, via 'cmd /c'. 'net' may work if you instead use 'net.exe'.

Personally I wouldn't worry to much about the distinction, mostly because it's not amazingly easy to tell when to use 'cmd /c' and it doesn't really have any noticeable draw-backs unless you use _SHELL() to get a return code. I would just put in 'cmd /c' and recognize what you're trying to do requires the use of 'cmd' and QB64 doesn't run it by default. If you run a program directly and give it's extension then you probably shouldn't need to use 'cmd /c'

Matt
"Cast your cares on the Lord and he will sustain you; he will never let the righteous be shaken" -- Psalm 55:22
QB64 Linux Installer

wiggins

  • Jr. Member
  • **
  • Posts: 50
Re: Shell command
« Reply #2 on: March 22, 2013, 11:12:38 AM »
Thanks Matt.  That explanation is very helpful.  I appreciate you taking the time to send back a very detailed response.

  • Print