Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Running java script from piped output

Programming


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-16-2016
SkySmart SkySmart is offline
Registered User
 
Join Date: Dec 2006
Last Activity: 22 January 2017, 2:09 AM EST
Posts: 836
Thanks: 656
Thanked 3 Times in 3 Posts
Running java script from piped output

to run most other scripts through a pipe, something similar to the following is usually enough:


Code:
cat script.sh | sh
cat perl.pl | perl -- "<arguments"


However, for javascript command line scripts, i cant seem to get this to work. Any ideas?

cat hull.js

Code:
#!/usr/bin/js

console.log("Hello, world!");

attempts to run it by piping:


Code:
root@gbrown-VirtualBox:~# cat hull.js | js

[stdin]:1
#!/usr/bin/js
^
SyntaxError: Unexpected token ILLEGAL
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:456:26)
    at evalScript (node.js:532:25)
    at Socket.<anonymous> (node.js:154:11)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# cat hull.js | node

[stdin]:1
#!/usr/bin/js
^
SyntaxError: Unexpected token ILLEGAL
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:456:26)
    at evalScript (node.js:532:25)
    at Socket.<anonymous> (node.js:154:11)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# cat hull.js | nodejs

[stdin]:1
#!/usr/bin/js
^
SyntaxError: Unexpected token ILLEGAL
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:456:26)
    at evalScript (node.js:532:25)
    at Socket.<anonymous> (node.js:154:11)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# cat hull.js | nodejs --

[stdin]:1
#!/usr/bin/js
^
SyntaxError: Unexpected token ILLEGAL
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:456:26)
    at evalScript (node.js:532:25)
    at Socket.<anonymous> (node.js:154:11)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# cat hull.js | node --

[stdin]:1
#!/usr/bin/js
^
SyntaxError: Unexpected token ILLEGAL
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:456:26)
    at evalScript (node.js:532:25)
    at Socket.<anonymous> (node.js:154:11)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# cat hull.js | js -
Error: unrecognized flag -
Try --help for options

[stdin]:1
#!/usr/bin/js
^
SyntaxError: Unexpected token ILLEGAL
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:456:26)
    at evalScript (node.js:532:25)
    at Socket.<anonymous> (node.js:154:11)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)
root@gbrown-VirtualBox:~#

Sponsored Links
    #2  
Old Unix and Linux 10-17-2016
hergp hergp is offline Forum Advisor  
Problem Eliminator
 
Join Date: Jan 2010
Last Activity: 22 January 2017, 3:36 PM EST
Location: Vienna, Austria
Posts: 844
Thanks: 27
Thanked 191 Times in 170 Posts
The shebang line does only work with languages that interpret the hash-sign as a comment. Javascript does not, so it thinks, that #!/usr/bin/js is a valid statement. By the way, if you feed your script via pipe into the interpreter, the shebang is useless anyway.

Remove the shebang line and it should work:


Code:
$ cat hull.js

console.log("Hello, world!");
$ cat hull.js | node
Hello, world!

The Following User Says Thank You to hergp For This Useful Post:
SkySmart (10-17-2016)
Sponsored Links
    #3  
Old Unix and Linux 10-17-2016
SkySmart SkySmart is offline
Registered User
 
Join Date: Dec 2006
Last Activity: 22 January 2017, 2:09 AM EST
Posts: 836
Thanks: 656
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by hergp View Post
The shebang line does only work with languages that interpret the hash-sign as a comment. Javascript does not, so it thinks, that #!/usr/bin/js is a valid statement. By the way, if you feed your script via pipe into the interpreter, the shebang is useless anyway.

Remove the shebang line and it should work:


Code:
$ cat hull.js

console.log("Hello, world!");
$ cat hull.js | node
Hello, world!

is there anyway to pass arguments to the node command in this situation?


Code:
cat hull.js | node -- one two three four five

    #4  
Old Unix and Linux 10-17-2016
hergp hergp is offline Forum Advisor  
Problem Eliminator
 
Join Date: Jan 2010
Last Activity: 22 January 2017, 3:36 PM EST
Location: Vienna, Austria
Posts: 844
Thanks: 27
Thanked 191 Times in 170 Posts
I don't think that this is possible. But maybe someone with better NodeJS knowledge has an idea?
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script output should be piped to a file samsungsamsung Shell Programming and Scripting 3 07-12-2011 08:12 AM
Directing only part of a script's output to piped application trigg Shell Programming and Scripting 1 11-12-2010 07:51 PM
Running shell script from java samshaw Shell Programming and Scripting 2 04-26-2009 12:34 AM
Running Shell Script from Java Asty Shell Programming and Scripting 3 08-14-2006 10:08 AM
perl... how to tell if a piped command is still running? boytheo Shell Programming and Scripting 0 08-17-2005 12:01 PM



All times are GMT -4. The time now is 01:04 AM.