Running java script from piped output


 
Thread Tools Search this Thread
Top Forums Programming Running java script from piped output
# 1  
Old 10-16-2016
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:~#

# 2  
Old 10-17-2016
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!

This User Gave Thanks to hergp For This Post:
# 3  
Old 10-17-2016
Quote:
Originally Posted by hergp
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 10-17-2016
I don't think that this is possible. But maybe someone with better NodeJS knowledge has an idea?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cat piped output

Hello, How can I efficiently cat piped output with another file? > (awk command) | cat file1 (piped output) Thanks! (11 Replies)
Discussion started by: palex
11 Replies

2. Shell Programming and Scripting

script output should be piped to a file

hi i have a script named mount.sh under the location /data/scripts/ in my aix box i want this script this to be run everyday morning at 04:45 AM and the output of the script should be piped to a file how to do this ? (3 Replies)
Discussion started by: samsungsamsung
3 Replies

3. Shell Programming and Scripting

Directing only part of a script's output to piped application

Is there a way to keep the output of a script displayed on the terminal when it's run by itself, but suspend part of that output and only have a specific part delivered when it's piped to another script or program? I'm thinking something like the following pseudocode: #!/bin/bash ... (1 Reply)
Discussion started by: trigg
1 Replies

4. Shell Programming and Scripting

find output seems to change when piped

Currently, i am trying to create a simple robust script that is intended to move the contents of a given source directory to a target directory. Optionally, the script should allow to either move the whole source dir content, or dotfiles only, or visible files only. I am aware the target directory... (0 Replies)
Discussion started by: shells_bells
0 Replies

5. Shell Programming and Scripting

Concatenate piped output and some var?

Hello, There is pipe chain and I want concacenate piped data with some variable: balh blah| ... $var1 What command I should use instead ... to concatenate piped output with $var1. I think I coud solve this using temp var - but could it be done in one line like sample above ? thanks... (4 Replies)
Discussion started by: vilius
4 Replies

6. Shell Programming and Scripting

Running shell script from java

Hello All, Hope all is well. I was trying to scratch my head here with simple problem of running Shell script in Java. I tried to google and look through forums but was unable to understand how to solve it. Here is my simple Java class, which resides in different directory then my shell... (2 Replies)
Discussion started by: samshaw
2 Replies

7. Shell Programming and Scripting

Exclude Certain Entries from Piped or Redirected Output?

I want to execute a command something like: find / -name "jni.h" and I want to direct the output of that command to some type of filter that will leave out all the lines reporting inaccessible directories (permission unavailable). Is this a pipe or a redirect? For example, output like... (1 Reply)
Discussion started by: downplay
1 Replies

8. Shell Programming and Scripting

Running a Java Programm with a (korn)shell-script

hey everyone, For my studies i had to write a javaprogram which reads 2 integers from the keyboard and then using the basic operations(addition, division etc) with them. so far no problem. but now i gotta make a shell-script which: runs the program(compiled with javac) #!bin/ksh java... (1 Reply)
Discussion started by: simlmf
1 Replies

9. Shell Programming and Scripting

Running Shell Script from Java

Hi How can I call a .sh (shell script) from a java procedure? Is this possible at all? Please tell me. Thanks. Asty (3 Replies)
Discussion started by: Asty
3 Replies

10. Shell Programming and Scripting

perl... how to tell if a piped command is still running?

I'm using the fabulous perl. I need a way to tell when a piped call to "open" has completed. Can I do this with a command like <ShellPipe> ?? Reason behind this: I'm trying to write a backup script in perl! This script will download a certain file from my web server, to my computer. Now,... (0 Replies)
Discussion started by: boytheo
0 Replies
Login or Register to Ask a Question