Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts

    using JS in shell and batch files

    i recently discovered that node.js can accept javascript at/from the command-line.

    this works on windows, mac, and linux.

    I'll use the windows version since it's still the most common desktop, but they are all identical expect for the look of the console prompt (as far as using node.js is concerned) in terms of the couple usages shown below.


    Once you install node, which is actually really easy these days (next,next,next...), you can use "node" at the command line.

    usually this just jumps into an interactive node session, but 2 flags can stop that: -p and -e.

    -p prints an evaluated program
    -e evaluates a program that follows, so it must be the last flag


    very very simple examples:

    first, as a pipe destination, same as MORE:
    Code:
    C:\>echo 123*456 | node -p
    56088
    note that if you're piping into node, you don't need the -e flag, it's implied.

    you can interpolate shell expansions into node code:
    Code:
    C:\>echo  '%TIME%'.bold()  | node -p
    <b> 2:36:30.61</b>


    second, you can send it a program to the right of the command using -e:

    Code:
    C:\>node -p -e  Math.random()
    0.6126509115565568
    if you want to interpolate in this mode, you need to use a quote around the whole program:
    Code:
    C:\>node -p -e  " '%TIME%'  + Date() "
     2:39:24.44Fri Jan 31 2014 02:39:24 GMT-0600 (Central Standard Time)

    =========================================
    BONUS, windows batch file.

    this little guy lets you easily execute JS code in a batch file or right in the console.
    if nothing else, it's a handy way to get a calculator on the console.

    once node is installed, NPM adds itself to the path (reboot may be needed)
    so, by adding a simple batch file to that folder, we can create a new shell command that runs in any folder.

    the file, js.bat:
    Code:
    @node -p -e %1
    setup and demo (type what's in red, [ENTER] after each line):
    note: "^Z" means [CTRL]+[Z] in the screen shot below:
    Code:
    C:\Users\dan\AppData\Roaming\npm>copy con js.bat
    @node -p -e %1 ^Z
            1 file(s) copied.
    
    C:\Users\dan\AppData\Roaming\npm>cd\
    
    C:\>js 123*456
    56088


    so, the next time you need a special format date stamp, a random number, or to convert data primitives on the console, node.js is your man!
    Last edited by rnd me; 01-31-2014 at 08:59 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  2. Users who have thanked rnd me for this post:

    Dormilich (02-01-2014)


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •