最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Write an executable .sh file via fs.writeFileSync - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Jul 6, 2017 at 18:45 Mat 207k41 gold badges402 silver badges418 bronze badges asked Jul 6, 2017 at 18:42 SaiSai 2,0426 gold badges34 silver badges56 bronze badges 3
  • nodejs/api/fs.html#fs_fs_chmodsync_path_mode & stackoverflow./questions/8756639/… – Will Commented Jul 6, 2017 at 18:48
  • the nodeJS filesystem module is your way to go – taha Commented Jul 6, 2017 at 18:52
  • you can even create shell scripts that will get interpreted by node instead of bash – taha Commented Jul 6, 2017 at 18:55
Add a ment  | 

1 Answer 1

Reset to default 8

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

发布评论

评论列表(0)

  1. 暂无评论