Run Shell Script from Ruby File and Capture the Output [duplicate] - ruby

This question already has answers here:
Getting output of system() calls in Ruby
(18 answers)
Closed 8 years ago.
I would like to run a shell script from my ruby file. Then capture its output and analyze it.
This is the scenario:
- Within my ruby script I need to execute the my_script.sh
- The shell script produce the followinf output in the terminal
> my_script.sh
xxxxx 1111
yyyyy 2222
zzzzz 3333
I need to capture and analyse this ouput inside the ruby script in order to find if a keyword is displayed (e.g., yyyy).
I'm using the following comand:
my_script = "/home/script.sh"
system("sh #{my_script}")
I'm not able to capture the output produced in the terminal for parsing

Don't use system, it does not capture STDOUT. Use backticks (or %x()):
output = %x( #{my_script} )

Related

How can I simply write all console output of a command to a file, in the terminal? [duplicate]

This question already has answers here:
print the output of strace command in a text file
(1 answer)
How to redirect and append both standard output and standard error to a file with Bash
(8 answers)
Closed 1 year ago.
I am trying to send all printed output of a strace command to a file. straces have a lot of console output since they print all the system calls of a command.
I have tried the most popular ways that I can find or think of, such as command > file.txt which does print output but then the output is not written to a file, same with command | tee file.txt Output is always printed but never written to a file. Is it because there is too much output or something? The first command definitely suceeds.
But my real question is what is the best way to do this and how can I get it done?
Many many thanks,
Milan

What does this "python -" do? [duplicate]

This question already has answers here:
What's the magic of "-" (a dash) in command-line parameters?
(5 answers)
Closed 1 year ago.
I see this command:
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python -
What does this to? What is the "-" called in bash?
This has nothing to do with bash. - has a very specific meaning when passed to the python binary:
-
Read commands from standard input (sys.stdin). [...]
Since, in your example,
curl outputs the downloaded file to stdout and
the shell pipe | passes curl's output to python's stdin,
python will execute the commands contained in the file downloaded by curl.
Note that this is a convention commonly found in various command-line utilities: Providing a single hyphen in place of a file name causes the command to read input from stdin instead of a file.

How to print bash command output and save to file? [duplicate]

This question already has answers here:
How to store the output of a command in a variable at the same time as printing the output?
(4 answers)
Capture stdout to a variable but still display it in the console
(5 answers)
Closed 3 years ago.
I am writing bash script and inside I am executing a command. I want to save the output of the command to variable but also want to print the output of the command to the standard output. I dont want to print the variable once the command is completed. How can I achieve this ?
Try the following, it will print the output of your command and assign to variable.
VAR="$(your_command| tee /dev/tty)"

reading a line from file using shell script [duplicate]

This question already has answers here:
How to read a file into a variable in shell?
(9 answers)
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
I have store ip address with port in a file and I want to read it using shell script. Thus file serverIP has data 192.168.1.17:3000. I am using following bash script to read it
IPAddressFile=/home/geo/serverIP
SERVER_IP_PORT=$(<$IPAddressFile)
echo $SERVER_IP_PORT
But this script echo empty string. Where I am making mistake?
If you're going to use bash-only syntax like $(<...), your script must be run with bash, not sh.
Thus, either run bash yourscript or add a #!/bin/bash (or similar) shebang, flag the file executable, and invoke it as a command, for example ./yourscript
As an alternative that's both efficient and compatible with POSIX sh:
IFS= read -r SERVER_IP_PORT <"$IPAddressFile"

Bash string concatenation producing weird results [duplicate]

This question already has answers here:
Concatenating strings in bash overwrites them
(4 answers)
Closed 5 years ago.
My bash code file.sh :
username=$1
pkgpath="/home/${username}_tmp.txt"
echo $username
echo $pkgpath
Now running the script with the command bash file.sh abc should produce the result :
abc
/home/abc_tmp.txt
But the output I'm getting is :
abc
_tmp.txtc
Can someone explain why is this behavior occurring and how to obtain the desired result ?
EDIT
I'd like to mention that using pkgpath="/home/${username}" gives me /home/abc (desired) but running pkgpath="${username}_tmp.txt"gives me _tmp.txt(weird).
Looks like you are somehow inserting a carriage return character after abc when you run the command bash file abc. The culprit is probably either your terminal, or you are copy pasting the command and are including ^M without realizing.
So what bash is outputting on the second line is really /home/abc^M_tmp.txt, which gets rendered as _tmp.txtc. You can easily verify this by piping the output of your command to less -R.

Resources