{"id":4602,"date":"2021-02-08T14:51:02","date_gmt":"2021-02-08T13:51:02","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=4602"},"modified":"2021-02-08T14:51:03","modified_gmt":"2021-02-08T13:51:03","slug":"cross-platform-portable-shell-scripts-using-node-js","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/cross-platform-portable-shell-scripts-using-node-js\/","title":{"rendered":"Cross Platform Portable Shell Scripts Using Node.js"},"content":{"rendered":"\n<p>Source: <a href=\"https:\/\/medium.com\/@harshitsinha1102\/cross-platform-portable-shell-scripts-using-node-69c63e7b578\">Cross Platform Portable Shell Scripts Using Node.js<\/a><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\"use strict\";\n\nconst shell = require(\"shelljs\");\n\n\/\/ Show preseint working directory using pwd\nconsole.log(\"Current Directory\", shell.pwd().toString());\n\n\/\/ Navigate to a directory using cd\nconsole.log(\"Navigating to temp folder inside current directory\");\nshell.cd('.\/temp');\nconsole.log(\"New Current Directory\", shell.pwd().toString());\n\nconsole.log(\"Navigating back to project root\");\nshell.cd(\"..\");\nconsole.log(\"Current Directory\", shell.pwd().toString());\n\n\/\/ List all files\/folders in a directory using ls\nconsole.log(\"Files\/Folders in Directory\", shell.pwd().toString());\nshell.ls(\".\/\").forEach(function(file) {\n    console.log(\"-\", file.toString());\n});\n\n\/\/ Output to terminal using echo\nshell.echo(\"Making a directory name tempDir\");\nshell.mkdir(\"tempDir\");\n\n\/\/ Gets all lines from given file where the word total exists\nconst greppedLines = shell.grep(\"total\", \".\/temp\/file1.txt\").toString().split('\\n');\n\nconsole.log(\"\\nGrepped lines with word total\");\ngreppedLines.forEach(function(line) {\n    console.log(\"Line - \", line);\n});\n\nconsole.log(\"\\nCopying file .\/temp\/file1.txt to .\/temp\/file1-copy.txt\");\nshell.cp(\".\/temp\/file1.txt\", \".\/temp\/file1-copy.txt\");\n\nconsole.log(\"\\nReplacing in place all occurences of 'if' with 'but' in file\");\nshell.sed(\"-i\", RegExp(\/if\/, \"g\"), \"but\", \".\/temp\/file1-copy.txt\");\n\nconsole.log(\"\\nDisplaying text of file using cat\");\nconsole.log(shell.cat(\".\/temp\/file1.txt\").toString());\n\nconsole.log(\"Grep lines with word 'total' from .\/temp\/file1.txt, then replace 'total' with 'totalReplaced' then output final file to .\/temp\/out.txt\");\nshell.grep(\"total\", \".\/temp\/file1.txt\")\n    .sed(RegExp(\/total\/, \"g\"), \"totalReplaced\")\n    .to(\".\/temp\/out.txt\");<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Source: Cross Platform Portable Shell Scripts Using Node.js<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4602","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=4602"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4602\/revisions"}],"predecessor-version":[{"id":4603,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4602\/revisions\/4603"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=4602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=4602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=4602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}