Search code examples
cmdfirefox-addonxulxulrunner

What environment variable is used to execute commands and .sh file in terminal


I successfully executed commands and .bat files in command prompt using XUL by this code:

Components.utils.import("resource://gre/modules/FileUtils.jsm");

var env = Components.classes["@mozilla.org/process/environment;1"]
                    .getService(Components.interfaces.nsIEnvironment);
var shell = new FileUtils.File(env.get("COMSPEC"));

var args = ["/c", "cd C:/ffmpeg/bin & record.bat"];

var process = Components.classes["@mozilla.org/process/util;1"]
                        .createInstance(Components.interfaces.nsIProcess);
process.init(shell);
process.runAsync(args, args.length);

But now I changed .bat files to .sh files to run for Ubuntu and need to run commands in terminal by using same code, so I need to change environment variable "COMSPEC" to something which works for terminal, I found that it is "TERM" but didn't work.

Is there any other environment variable or way to do this task?


Solution

  • The command to run shell files on Linux is fixed - it is always /bin/sh. So you would do:

    var shell = new FileUtils.File("/bin/sh");
    var args = ["-c", "record.sh"];
    

    This isn't quite the correct approach - if you wanted to do it absolutely correctly you would look inside the script file and parse the first line (which is usually something like #!/bin/sh). And in the general case you wouldn't check the file extension but rather whether the file is executable (on Unix-like systems this is completely independent of the file extension). But I guess that you aren't very interested in the general case.