Unix/Linux Go Back    


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

Running java script from piped output

Programming


Closed    
 
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: 8 May 2017, 11:06 AM EDT
Posts: 873
Thanks: 719
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 online now Forum Advisor  
Problem Eliminator
 
Join Date: Jan 2010
Last Activity: 22 June 2017, 4:21 PM EDT
Location: Vienna, Austria
Posts: 846
Thanks: 32
Thanked 193 Times in 172 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: 8 May 2017, 11:06 AM EDT
Posts: 873
Thanks: 719
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 online now Forum Advisor  
Problem Eliminator
 
Join Date: Jan 2010
Last Activity: 22 June 2017, 4:21 PM EDT
Location: Vienna, Austria
Posts: 846
Thanks: 32
Thanked 193 Times in 172 Posts
I don't think that this is possible. But maybe someone with better NodeJS knowledge has an idea?
Sponsored Links
Closed

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 07:12 AM
Directing only part of a script's output to piped application trigg Shell Programming and Scripting 1 11-12-2010 06:51 PM
Running shell script from java samshaw Shell Programming and Scripting 2 04-25-2009 11:34 PM
Running Shell Script from Java Asty Shell Programming and Scripting 3 08-14-2006 09:08 AM
perl... how to tell if a piped command is still running? boytheo Shell Programming and Scripting 0 08-17-2005 11:01 AM



All times are GMT -4. The time now is 04:30 PM.