• system.exec() and commands in double-quotes

    From Nightfox@VERT/DIGDIST to Digital Man on Tue Apr 8 20:52:04 2025
    Hi DM,

    Background: My login matrix has a feature where it can convert a .jpg etc. to a sixel using ImageMagick's conversion program. On Windows, it's very possible that the program may be installed in a directory with a space (i.e., somewhere in C:\Program Files). I was using a Windows test setup and making an update to my login matrix to try to allow running that with system.exec() by putting the fully-pathed executable in double-quotes. I did some debugging, adding " & pause" to the command so I could see the command window, and it complained about "C:\Program" not existing - Even though I put double-quotes around the fully-pathed executable, it seemed to be acting like those double-quotes were removed. I logged the command and verified the double-quotes were there.

    I'm wondering if system.exec() removes double-quotes from the command it's given?

    I'm also wondering if the same thing would happen on Linux (I haven't tested that).

    I thought of one solution, to write the command to a batch file or shell script and run that (in a directory that doesn't contain a space), but I'm wondering if I could avoid that.

    Nightfox

    ---
    þ Synchronet þ Digital Distortion: digitaldistortionbbs.com
  • From Digital Man@VERT to Nightfox on Tue Apr 8 21:54:41 2025
    Re: system.exec() and commands in double-quotes
    By: Nightfox to Digital Man on Tue Apr 08 2025 08:52 pm

    Hi DM,

    Background: My login matrix has a feature where it can convert a .jpg etc. to a sixel using ImageMagick's conversion program. On Windows, it's very possible that the program may be installed in a directory with a space (i.e., somewhere in C:\Program Files). I was using a Windows test setup and making an update to my login matrix to try to allow running that with system.exec() by putting the fully-pathed executable in double-quotes. I did some debugging, adding " & pause" to the command so I could see the command window, and it complained about "C:\Program" not existing - Even though I put double-quotes around the fully-pathed executable, it seemed to be acting like those double-quotes were removed. I logged the command and verified the double-quotes were there.

    I'm wondering if system.exec() removes double-quotes from the command it's given?

    No, the double-quotes won't be removed. system.exec() is just a very-thing wrapper around system():
    https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem?view=msvc-170

    I'm also wondering if the same thing would happen on Linux (I haven't tested that).

    Nope, shouldn't.
    https://www.man7.org/linux/man-pages/man3/system.3.html

    I thought of one solution, to write the command to a batch file or shell script and run that (in a directory that doesn't contain a space), but I'm wondering if I could avoid that.

    Did you escape the double-quotes in the JS string passed to system.exec()? I think there's something else at play here.
    --
    digital man (rob)

    Sling Blade quote #23:
    Karl: I reckon I'm gonna have to get used to looking at pretty people.
    Norco, CA WX: 63.0øF, 61.0% humidity, 0 mph N wind, 0.00 inches rain/24hrs
    ---
    þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net
  • From Nightfox@VERT/DIGDIST to Digital Man on Wed Apr 9 09:19:52 2025
    Re: system.exec() and commands in double-quotes
    By: Digital Man to Nightfox on Tue Apr 08 2025 09:54 pm

    No, the double-quotes won't be removed. system.exec() is just a very-thing wrapper around system(): https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/system-w syste m?view=msvc-170

    I'm also wondering if the same thing would happen on Linux (I haven't
    tested that).

    Nope, shouldn't.
    https://www.man7.org/linux/man-pages/man3/system.3.html

    Did you escape the double-quotes in the JS string passed to system.exec()? I think there's something else at play here.

    I did escape the double-quotes. I'll work on it more. Thanks.

    Nightfox

    ---
    þ Synchronet þ Digital Distortion: digitaldistortionbbs.com
  • From Nightfox@VERT/DIGDIST to Digital Man on Wed Apr 9 17:16:51 2025
    Re: system.exec() and commands in double-quotes
    By: Digital Man to Nightfox on Tue Apr 08 2025 09:54 pm

    No, the double-quotes won't be removed. system.exec() is just a very-thing wrapper around system():

    Did you escape the double-quotes in the JS string passed to system.exec()? I think there's something else at play here.

    I did a bit more investigation. Since you say system.exec() is a wrapper around system(), I'm wondering if there may be a bug in system().

    It seems that the issue happens when there are multiple things in double-quotes. I tried having it only run the converter program (so, the whole command was the fully-pathed executable in double-quotes), and that ran successfully.

    In the command line I was trying to use, I have multiple things in double-quotes: The fully-pathed executable is first, then possibly some command-line parameters, and a source filename and an output filename. The fully-pathed executable, source filename, and output filename are all within their own set of double-quotes (in case there are spaces in their paths), and it seems that's when this issue happens where the Windows command interpreter only seems to be seeing "C:\Program" as the executable and fails due to that - even though the executable filename is in double-quotes.

    If I remove the double-quotes around the source filename & destination filename, so that there's only one set of double-quotes in the command line (around the executable), then it succeeds.

    Nightfox

    ---
    þ Synchronet þ Digital Distortion: digitaldistortionbbs.com
  • From Digital Man@VERT to Nightfox on Wed Apr 9 18:27:39 2025
    Re: system.exec() and commands in double-quotes
    By: Nightfox to Digital Man on Wed Apr 09 2025 05:16 pm

    Re: system.exec() and commands in double-quotes
    By: Digital Man to Nightfox on Tue Apr 08 2025 09:54 pm

    No, the double-quotes won't be removed. system.exec() is just a very-thing wrapper around system():

    Did you escape the double-quotes in the JS string passed to system.exec()? I think there's something else at play here.

    I did a bit more investigation. Since you say system.exec() is a wrapper around system(), I'm wondering if there may be a bug in system().

    It seems that the issue happens when there are multiple things in double-quotes. I tried having it only run the converter program (so, the whole command was the fully-pathed executable in double-quotes), and that ran successfully.

    In the command line I was trying to use, I have multiple things in double-quotes: The fully-pathed executable is first, then possibly some command-line parameters, and a source filename and an output filename. The fully-pathed executable, source filename, and output filename are all within their own set of double-quotes (in case there are spaces in their paths), and it seems that's when this issue happens where the Windows command interpreter only seems to be seeing "C:\Program" as the executable and fails due to that - even though the executable filename is in double-quotes.

    If I remove the double-quotes around the source filename & destination filename, so that there's only one set of double-quotes in the command line (around the executable), then it succeeds.

    Maybe read up more about Windows system() and see if it has some magic quote parsing logic built into it that might be tripped up by quoted arguments.
    --
    digital man (rob)

    This Is Spinal Tap quote #31:
    Viv Savage: Quite exciting, this computer magic!
    Norco, CA WX: 77.5øF, 34.0% humidity, 10 mph W wind, 0.00 inches rain/24hrs
    ---
    þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net
  • From phigan@VERT/TACOPRON to Nightfox on Thu Apr 10 05:34:40 2025
    Re: system.exec() and commands in double-quotes
    By: Nightfox to Digital Man on Wed Apr 09 2025 05:16 pm

    If I remove the double-quotes around the source filename & destination filename, so that there's only one set of double-quotes in the command line

    What if you do something silly and put the whole thing in one set of quotes, then escape the quotes inside?

    ---
    þ Synchronet þ TIRED of waiting 2 hours for a taco? GO TO TACOPRONTO.bbs.io
  • From Nightfox@VERT/DIGDIST to phigan on Thu Apr 10 09:32:04 2025
    Re: system.exec() and commands in double-quotes
    By: phigan to Nightfox on Thu Apr 10 2025 05:34 am

    If I remove the double-quotes around the source filename & destination
    filename, so that there's only one set of double-quotes in the command line

    What if you do something silly and put the whole thing in one set of quotes, then escape the quotes inside?

    I suppose I could try that.

    Nightfox

    ---
    þ Synchronet þ Digital Distortion: digitaldistortionbbs.com
  • From Nightfox@VERT/DIGDIST to phigan on Thu Apr 10 11:52:54 2025
    Re: system.exec() and commands in double-quotes
    By: phigan to Nightfox on Thu Apr 10 2025 05:34 am

    If I remove the double-quotes around the source filename & destination
    filename, so that there's only one set of double-quotes in the command line

    What if you do something silly and put the whole thing in one set of quotes, then escape the quotes inside?

    That seems to work.. Thanks for the suggestion. And it seems to only be necessary on Windows.

    Nightfox

    ---
    þ Synchronet þ Digital Distortion: digitaldistortionbbs.com
  • From Digital Man@VERT to Nightfox on Thu Apr 10 15:47:22 2025
    Re: system.exec() and commands in double-quotes
    By: Nightfox to phigan on Thu Apr 10 2025 11:52 am

    Re: system.exec() and commands in double-quotes
    By: phigan to Nightfox on Thu Apr 10 2025 05:34 am

    If I remove the double-quotes around the source filename & destination
    filename, so that there's only one set of double-quotes in the command line

    What if you do something silly and put the whole thing in one set of quotes, then escape the quotes inside?

    That seems to work.. Thanks for the suggestion. And it seems to only be necessary on Windows.

    Maybe share the code?
    --
    digital man (rob)

    Breaking Bad quote #22:
    I got one more shot at that little skid mark. - Hank Schrader
    Norco, CA WX: 82.6øF, 30.0% humidity, 15 mph W wind, 0.00 inches rain/24hrs
    ---
    þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net
  • From Nightfox@VERT/DIGDIST to Digital Man on Thu Apr 10 16:42:34 2025
    Re: system.exec() and commands in double-quotes
    By: Digital Man to Nightfox on Thu Apr 10 2025 03:47 pm

    If I remove the double-quotes around the source filename & destination
    filename, so that there's only one set of double-quotes in the command
    line

    What if you do something silly and put the whole thing in one set of
    quotes, then escape the quotes inside?

    That seems to work.. Thanks for the suggestion. And it seems to only be
    necessary on Windows.

    Maybe share the code?

    It's failry simple, basically just adding double-quotes around the whole command if you know the command has multiple sets of double-quotes.

    I pasted the section of code on Pastebin:

    https://pastebin.com/pPXfHKeP

    Nightfox

    ---
    þ Synchronet þ Digital Distortion: digitaldistortionbbs.com
  • From Digital Man@VERT to Nightfox on Thu Apr 10 17:14:39 2025
    Re: system.exec() and commands in double-quotes
    By: Nightfox to Digital Man on Thu Apr 10 2025 04:42 pm

    Re: system.exec() and commands in double-quotes
    By: Digital Man to Nightfox on Thu Apr 10 2025 03:47 pm

    If I remove the double-quotes around the source filename & destination
    filename, so that there's only one set of double-quotes in the command
    line

    What if you do something silly and put the whole thing in one set of
    quotes, then escape the quotes inside?

    That seems to work.. Thanks for the suggestion. And it seems to only be
    necessary on Windows.

    Maybe share the code?

    It's failry simple, basically just adding double-quotes around the whole command if you know the command has multiple sets of double-quotes.

    I pasted the section of code on Pastebin:

    https://pastebin.com/pPXfHKeP

    Okay, so no "escaped quotes" are actually being passed to system(). I was confused by your reply to phigan.

    The end result (passed to system()) looks like it would be:

    ""C:\Program Files\Path\To\Program" some-arg "some-arg""

    That's pretty weird.

    One nice thing about JavaScript is you don't have to escape the double-quotes if you inclose the string in single-quotes. So could probably get away without escaping anything.
    --
    digital man (rob)

    This Is Spinal Tap quote #17:
    David St. Hubbins: It's such a fine line between stupid, and uh... and clever. Norco, CA WX: 82.3øF, 27.0% humidity, 11 mph W wind, 0.00 inches rain/24hrs
    ---
    þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net
  • From Nightfox@VERT/DIGDIST to Digital Man on Fri Apr 11 12:20:47 2025
    Re: system.exec() and commands in double-quotes
    By: Digital Man to Nightfox on Thu Apr 10 2025 05:14 pm

    Okay, so no "escaped quotes" are actually being passed to system(). I was confused by your reply to phigan.

    Maybe I'm not understanding what you mean by escaped quotes.. I escaped the double-quotes in the string with the command because I used double-quotes for the whole thing.

    The end result (passed to system()) looks like it would be:

    ""C:\Program Files\Path\To\Program" some-arg "some-arg""

    That's pretty weird.

    Yeah, it's weird but it looks like that's what's working on Windows.

    One nice thing about JavaScript is you don't have to escape the double-quotes if you inclose the string in single-quotes. So could probably get away without escaping anything.

    I sometimes forget you can use single-quotes for strings in JS.

    Nightfox

    ---
    þ Synchronet þ Digital Distortion: digitaldistortionbbs.com
  • From Digital Man@VERT to Nightfox on Fri Apr 11 12:42:53 2025
    Re: system.exec() and commands in double-quotes
    By: Nightfox to Digital Man on Fri Apr 11 2025 12:20 pm

    Re: system.exec() and commands in double-quotes
    By: Digital Man to Nightfox on Thu Apr 10 2025 05:14 pm

    Okay, so no "escaped quotes" are actually being passed to system(). I was confused by your reply to phigan.

    Maybe I'm not understanding what you mean by escaped quotes.. I escaped the double-quotes in the string with the command because I used double-quotes for the whole thing.

    Right, but you escaped them only for JavaScript, not the Windows command interpretter. The string being passed to system() did not appear to include any "escaped quotes".

    The end result (passed to system()) looks like it would be:

    ""C:\Program Files\Path\To\Program" some-arg "some-arg""

    That's pretty weird.

    Yeah, it's weird but it looks like that's what's working on Windows.

    One nice thing about JavaScript is you don't have to escape the double-quotes if you inclose the string in single-quotes. So could probably get away without escaping anything.

    I sometimes forget you can use single-quotes for strings in JS.

    They're actually preferred, for strings, in a lot of JS styles, e.g. https://google.github.io/styleguide/tsguide.html#primitive-literals

    Coming from C and C++ myself, I usually default to double-quoted strings.
    --
    digital man (rob)

    Steven Wright quote #7:
    A clear conscience is usually the sign of a bad memory.
    Norco, CA WX: 85.4øF, 30.0% humidity, 3 mph SW wind, 0.00 inches rain/24hrs
    ---
    þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net
  • From phigan@VERT/TACOPRON to Nightfox on Sun Apr 13 15:00:38 2025
    Re: system.exec() and commands in double-quotes
    By: Nightfox to phigan on Thu Apr 10 2025 11:52 am

    That seems to work.. Thanks for the suggestion. And it see
    to only be necessary on Windows.

    Nice. Yeah, I've had to do some goofy stuff writing scripts in
    Windows.

    ---
    þ Synchronet þ TIRED of waiting 2 hours for a taco? GO TO TACOPRONTO.bbs.io