When using Commnad::new(... > test.txt) why does it error? | Rust - rust

I'm writing integration tests and when I try to use my binary like this:
Command::new("cargo").args(["r", "--", "test.mn", ">", "test_bytecode.txt"]).status().unwrap();
it errors out with this reason:
error: Found argument '>' which wasn't expected or isn't valid in this context
But when I run the command in my shell directly:
cargo run -- test.mn > test_bytecode.txt
It works no problem. Is there some rule that rust can't create files like this?

Related

How do I pipe input to a Node.js program that uses readline-sync?

I have a very simple Node.js program that uses readline-sync to accept input, then echo it to the console:
const readlineSync = require('readline-sync');
const input = readlineSync.prompt();
console.log(input);
It works fine as an interactive program; however, when I try to pipe input to it (in either Git Bash or PowerShell), I get a Node.js error:
PS> echo "1.2" | node .\index.js
Windows PowerShell[35552]: c:\ws\src\node_file.cc:1631: Assertion `(argc) == (5)' failed.
Adding a #!/usr/bin/env node shebang and running it as a script with echo "1.2" | .\script.js produces the same error.
Is there a configuration option or something that I'm missing that allows readline-sync to read input from a pipe? Is there something wrong with how I'm running it in the shell? Any advice would be appreciated.
It is most probably the package compatibility issue with the node version that you are using. You need to check all the dependencies whether they are compatible with the node version that you are using.
I think your program is taking 'input' in the form of 'argument' but not from 'stdin'.
when you use '|' , Input will be given to the program as 'stdin' not as 'argument'
so to convert 'stdin' coming out of '|' to 'input argument' we can use 'xargs' in linux.
try following on linux/bash to see if it works :
echo "1.2" | xargs .\script.js
Just to give a example , we can see the functionality of 'echo' command which can take 'input' only in the form of 'arguments' but not as 'stdin' :
# following command does print anything :
echo boo | echo
# but when xargs is used after | , It displays the output :
echo boo | xargs echo
boo

Why pipes are not working on Powershell for various NodeJS CLI tools?

I am trying around the following highly used tools:
prettyjson
prettier
For example when I run the following on Powershell:
echo '{"a": 1}' | prettyjson
The terminal will just keep waiting for inputs till CTRL+C pressed and it exits with no expected output.
The workaround is to add .cmd to the command or just use cmd instead:
echo '{"a": 1}' | prettyjson.cmd
Outputs
a: 1
This seems to be a known limitation and a pull request is available:
https://github.com/npm/cmd-shim/pull/43

Internal Variable PIPESTATUS

I am new to linux and bash scripting and i have query about this internal variable PIPESTATUS which is an array and stores the exit status of individual commands in pipe.
On command line:
$ find /home | /bin/pax -dwx ustar | /bin/gzip -c > myfile.tar.gz
$ echo ${PIPESTATUS[*]}
$ 0 0 0
working fine on command line but when I am putting this code in a bash script it is showing only one exit status. My default SHELL on command line is bash only.
Somebody please help me to understand why this behaviour is changing? And what should I do to get this work in script?
#!/bin/bash
cmdfile=/var/tmp/cmd$$
backfile=/var/tmp/backup$$
find_fun() {
find /home
}
cmd1="find_fun | /bin/pax -dwx ustar"
cmd2="/bin/gzip -c"
eval "$cmd1 | $cmd2 > $backfile.tar.gz " 2>/dev/null
echo -e " find ${PIPESTATUS[0]} \npax ${PIPESTATUS[1]} \ncompress ${PIPESTATUS[2]} > $cmdfile
The problem you are having with your script is that you aren't running the same code as you ran on the command line. You are running different code. Namely the script has the addition of eval. If you were to wrap your command line test in eval you would see that it fails in a similar manner.
The reason the eval version fails (only gives you one value in PIPESTATUS) is because you aren't executing a pipeline anymore. You are executing eval on a string that contains a pipeline. This is similar to executing /bin/bash -c 'some | pipe | line'. The thing actually being run by the current shell is a single command so it has a single exit code.
You have two choices here:
Get rid of eval (which you should do anyway as eval is generally something to avoid) and stop using a string for a command (see Bash FAQ 050 for more on why doing this is a bad idea.
Move the echo "${PIPESTATUS[#]}" into the eval and then capture (and split/parse) the resulting output. (This is clearly a worse solution in just about every way.)
Instead of ${PIPESTATUS[0]} use ${PIPESTATUS[#]}
As with any array in bash PIPESTATUS[0] contains the first command exit status. If you want to get all of them you have to use PIPESTATUS[#] which returns all the contents of the array.
I'm not sure why it worked for you when you tried it in the command line. I tested it and I didn't get the same result as you.

Calling a script with x3270 -script

I have an old script which is used to scrape information from an IBM server via x3270. However, I can't get it to work correctly. This is how I'm calling it:
/usr/X11R6/bin/x3270 -script -model 3279-2 -geom +110+160 -efont 3270-20 'Script( "/usr/X11R6/lib/X11/x3270/qmon_script.sh" )'
I get an x3270 window and the following error message: Hostname syntax error: Multiple port names
The script I'm calling handles all the connection details, but x3270 appears to be confused and is thinking 'Script( "/usr/X11R6/lib/X11/x3270/qmon_script.sh" )' is the hostname (which is obviously not correct).
I've been unable to find any good examples on how to call a script through x3270 like this. Any ideas?
According to the documentation for x3270:
-script
Causes x3270 to read commands from standard input, with the results written to standard
output. The protocol for these commands is documented in x3270-script(1).
So it doesn't allow giving the script itself on the command line. Instead you're supposed to supply the script through standard input. You probably want either:
echo 'Script( "/usr/X11R6/lib/X11/x3270/qmon_script.sh" )' | /usr/X11R6/bin/x3270 -script -model 3279-2 -geom +110+160 -efont 3270-20
Or maybe:
/usr/X11R6/bin/x3270 -script -model 3279-2 -geom +110+160 -efont 3270-20 < /usr/X11R6/lib/X11/x3270/qmon_script.sh

Why There Are Different Behaviors With Groovy Command Line Arguments?

I have a groovy file named test.groovy and have a single line of coding in it :
println args[0];
when I run this program like this groovy test ants, output is ants.
But when I run the program with the argument ants( then I'm getting error like this :
bash: syntax error near unexpected token (
1)If I escape the character ( then I'm getting the output as ants(. But why ( is needed to be escaped?
And when I run the program with the argument ant's, then clicking enter would make my terminal look like this :
>
>
>
2)And I terminate the program only using ctrl+c. What actually happens in this case? Why my terminal look like this?
3)After seeing these, what are the rules and condition that are to be followed in Groovy with accordance with Command-line arguments and the same holds for Java?
Thanks in advance.
You need to escape it as ( has a meaning in the bash shell which you are using.
The same goes for '
Try other commands:
ls (
Or
ls '
You'll get the same effect
Another option (other than escaping) is to put your arguments inside quote chars like so:
groovy test 'ants('

Resources