3

Is there a way to write an executable shell script file using fs.writeFileSync

Fundamentally i'm trying to output a .sh file via a CLI and i want that .sh file to be executable so i do not need to manually run a chmod +x "filename" once the file gets outputted from CLI.

Mat
  • 202,337
  • 40
  • 393
  • 406
Sai
  • 1,790
  • 5
  • 29
  • 51
  • https://nodejs.org/api/fs.html#fs_fs_chmodsync_path_mode & https://stackoverflow.com/questions/8756639/how-do-i-use-chmod-with-node-js – Will Jul 06 '17 at 18:48
  • the nodeJS filesystem module is your way to go – taha Jul 06 '17 at 18:52
  • you can even create shell scripts that will get interpreted by node instead of bash – taha Jul 06 '17 at 18:55

1 Answers1

6

You can create the script file and set the permissions within Node using the filesystem (fs) module.

var fs = require('fs');

var script_name = "myscript.sh";
var script_content = "echo Hello world!";

fs.writeFileSync(script_name, script_content);
fs.chmodSync(script_name, "755");

Consider looking here for more information on understanding rights

daviscodesbugs
  • 657
  • 7
  • 17