Tired of writing and maintaining multiple Command Line scripts (shell / batch) for various platforms (Ubuntu/Windows/Mac) ?

Here’s a one stop solution implemented in Node.js for easing your life - Shelljs.

You will not only ensure that a single copy of script runs on all platform but also don’t have to bother about gaining expertise in multiple scripting languages.

Prerequisites -

Knowledge -

Basic JavaScript knowledge.

Shell Scripting knowledge.

Tools -

Node.js

Npm (Comes bundled along with Node.js)

Setup -

Make a directory for the scripts project

Navigate to the scripts project directory

Initialize npm in the given project using npm init. Accept all the default values by pressing enter at each step

Install shelljs as an npm dependency

mkdir shell-scripts

cd shell-scripts/

npm install --save shelljs

npm init

A package.json file will be created in the directory

{ "name": "shell-scripts", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "shelljs": "^0.8.3" } }

Directory Structure will look like -

Scripting -

Basic Commands (pwd, cd, echo, mkdir) - Here’s a program showing commonly used commands

Output -

shell-scripts harshitsinha$ node test_script.js Current Directory /Users/harshitsinha/Desktop/random_tests/shell-scripts Navigating to temp folder inside current directory New Current Directory /Users/harshitsinha/Desktop/random_tests/shell-scripts/temp Navigating back to project root Current Directory /Users/harshitsinha/Desktop/random_tests/shell-scripts Files/Folders in Directory /Users/harshitsinha/Desktop/random_tests/shell-scripts - node_modules - package.json - temp - test_script.js Making a directory name tempDir

More commands (grep, cp, cat, sed)

Output -

shell-scripts$ node test_script2.js Grepped lines with word total

Line - total is used for testing purposes

Line - We will again drop a total here

Line - Copying file ./temp/file1.txt to ./temp/file1-copy.txt Replacing in place all occurences of 'if' with 'but' in file Displaying text of file using cat

Hi this is a sample text file

total is used for testing purposes

We will again drop a total here

if we can keep on continuing this difficult process we will have a good test example.

Grep lines with word 'total' from ./temp/file1.txt, then replace 'total' with 'totalReplaced' then output final file to ./temp/out.txt

For more examples refer