Obtain output from a bash command in Ruby - ruby

I'm trying to obtain the output of a bash command. More precisely, I need to store the number of lines that contains a string in a file:
variable_name = AAAAAAA
PATH_TO_SEARCH = .
COMMAND = "grep -R #{variable_name} #{PATH_TO_SEARCH} | wc -l"
To execute the command I tried both methods:
num_lines = %x[ #{COMMAND} ]
num_lines = `#{COMMAND}`
but the problem is: In "num_lines" I have 1) the number of lines that contain the string (OK!) and 2) output from grep like "grep: /home/file_example.txt: No such file or directory" (NO!).
I would like to store just the first output.

Looks like you may just need to suppress the error messages.
"You can use the -s or --no-messages flag to suppress errors." found from How can I have grep not print out 'No such file or directory' errors?

Related

Inspect null character from Bash's read command

I am on a system that does not have hexdump. I know there's a null character on STDIN, but I want to show/prove it. I've got Ruby on the system. I've found that I can directly print it like this:
$ printf 'before\000after' | (ruby -e "stdin_contents = STDIN.read_nonblock(10000) rescue nil; puts 'stdin contents: ' + stdin_contents.inspect")
stdin contents: "before\x00after"
However, I need to run this inside of a bash script i.e. STDIN is not being directly piped to my script. I have to get it via running read in bash.
When I try to use read to get the stdin characters, it seems to be truncating them and it doesn't work:
$ printf 'before\000after' | (read -r -t 1 -n 1000000; printf "$REPLY" | ruby -e "stdin_contents = STDIN.read_nonblock(10000) rescue nil; puts 'stdin contents: ' + stdin_contents.inspect")
stdin contents: "before"
My question is this: How can I get the full/raw output including the null character from read

Read first token from output

I have this command:
num_lines="$(wc -l "$HOME/my_bash_history")"
Which yields:
17 /Users/alex/my_bash_history
So I tried to get the first token using:
local read num_lines < <(wc -l "$HOME/my_bash_history")
But all I get is empty result:
num lines:
Anybody know why?
wc reports the file name unless it is reading from stdin. So, keep it simple, just use:
$ num_lines="$(wc -l <"$HOME/my_bash_history")"
$ echo "$num_lines"
17
If you really want to use read with process substitution, then use two arguments to read like this:
$ read num_lines fname < <(wc -l "$HOME/my_bash_history")
$ echo "$num_lines"
17
or, use a here-string like this:
$ read num_lines fname <<<"$(wc -l "$HOME/my_bash_history")"
$ echo "$num_lines"
17
When read reads a line, the shell first splits the lines into words. The words are assigned to each argument in turn with the last argument receiving whatever remains. In our case, this means the the number is assigned to num_lines and whatever words follow the number are assigned to fname.
try this:
num_lines="$(wc -l $HOME/my_bash_history)"
echo "${num_lines%% *}"
explanation
${num_lines%% *} # delete all after first blank

Re-direct output of a shell to a txt file

I have a script written and I want to include a function in the script, that silently logs the console output to a .txt file. The printf used in my shell scripts have colors for certain characters.
A sample:
# Color block
G="\033[32m"
N="\033[0m"
R="\033[31m"
Y="\033[33m"
# MCS Check
mcs=$(cat /home/admin/service-health.txt | grep -i mcs | cut -d ' ' -f 5 | tr . " ")
if [ "$mcs" == "up " ]
then
printf "${Y}MCS${N} Service Status is\t\t |${G}UP${N}\n"
else
printf "${Y}MCS${N} Service Status is\t\t |${R}DOWN${N}\n"
fi
Console output for this will display the color.
This is not mandatory in the .txt logging.
I will then be emailing this .txt to an address using:
sendmail $vdp $eaddr < /home/admin/health-check.txt
I used this block as I want to redirect the output within the script itself:
sudo touch /home/admin/health-check.txt
exec > >(tee -i /home/admin/health-check.txt)
exec 2>&1
But since this is a colored output, I keep getting this in my email:
[33mGSAN[0m Service Status is |[32mUP[0m
[33mMCS[0m Service Status is |[32mUP[0m
[33mTomcat[0m Service Status is |[32mUP[0m
[33mScheduler[0m Service Status is |[32mUP[0m
[33mMaintenance[0m Service Status is |[32mUP[0m
VDP [33mAccess State[0m is |[32mFULL[0m
Any thoughts about stripping colors during redirect? I do not want to use sed to find and replace as this looks tedious.
Thanks.
You can direct the output using the > character. printf "mytext" > out.txt will print "mytext" to the file "out.txt"

How to concatenate stdin and a string?

How to I concatenate stdin to a string, like this?
echo "input" | COMMAND "string"
and get
inputstring
A bit hacky, but this might be the shortest way to do what you asked in the question (use a pipe to accept stdout from echo "input" as stdin to another process / command:
echo "input" | awk '{print $1"string"}'
Output:
inputstring
What task are you exactly trying to accomplish? More context can get you more direction on a better solution.
Update - responding to comment:
#NoamRoss
The more idiomatic way of doing what you want is then:
echo 'http://dx.doi.org/'"$(pbpaste)"
The $(...) syntax is called command substitution. In short, it executes the commands enclosed in a new subshell, and substitutes the its stdout output to where the $(...) was invoked in the parent shell. So you would get, in effect:
echo 'http://dx.doi.org/'"rsif.2012.0125"
use cat - to read from stdin, and put it in $() to throw away the trailing newline
echo input | COMMAND "$(cat -)string"
However why don't you drop the pipe and grab the output of the left side in a command substitution:
COMMAND "$(echo input)string"
I'm often using pipes, so this tends to be an easy way to prefix and suffix stdin:
echo -n "my standard in" | cat <(echo -n "prefix... ") - <(echo " ...suffix")
prefix... my standard in ...suffix
There are some ways of accomplish this, i personally think the best is:
echo input | while read line; do echo $line string; done
Another can be by substituting "$" (end of line character) with "string" in a sed command:
echo input | sed "s/$/ string/g"
Why i prefer the former? Because it concatenates a string to stdin instantly, for example with the following command:
(echo input_one ;sleep 5; echo input_two ) | while read line; do echo $line string; done
you get immediatly the first output:
input_one string
and then after 5 seconds you get the other echo:
input_two string
On the other hand using "sed" first it performs all the content of the parenthesis and then it gives it to "sed", so the command
(echo input_one ;sleep 5; echo input_two ) | sed "s/$/ string/g"
will output both the lines
input_one string
input_two string
after 5 seconds.
This can be very useful in cases you are performing calls to functions which takes a long time to complete and want to be continuously updated about the output of the function.
You can do it with sed:
seq 5 | sed '$a\6'
seq 5 | sed '$ s/.*/\0 6/'
In your example:
echo input | sed 's/.*/\0string/'
I know this is a few years late, but you can accomplish this with the xargs -J option:
echo "input" | xargs -J "%" echo "%" "string"
And since it is xargs, you can do this on multiple lines of a file at once. If the file 'names' has three lines, like:
Adam
Bob
Charlie
You could do:
cat names | xargs -n 1 -J "%" echo "I like" "%" "because he is nice"
Also works:
seq -w 0 100 | xargs -I {} echo "string "{}
Will generate strings like:
string 000
string 001
string 002
string 003
string 004
...
The command you posted would take the string "input" use it as COMMAND's stdin stream, which would not produce the results you are looking for unless COMMAND first printed out the contents of its stdin and then printed out its command line arguments.
It seems like what you want to do is more close to command substitution.
http://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html#Command-Substitution
With command substitution you can have a commandline like this:
echo input `COMMAND "string"`
This will first evaluate COMMAND with "string" as input, and then expand the results of that commands execution onto a line, replacing what's between the ‘`’ characters.
cat will be my choice: ls | cat - <(echo new line)
With perl
echo "input" | perl -ne 'print "prefix $_"'
Output:
prefix input
A solution using sd (basically a modern sed; much easier to use IMO):
# replace '$' (end of string marker) with 'Ipsum'
# the `e` flag disables multi-line matching (treats all lines as one)
$ echo "Lorem" | sd --flags e '$' 'Ipsum'
Lorem
Ipsum#no new line here
You might observe that Ipsum appears on a new line, and the output is missing a \n. The reason is echo's output ends in a \n, and you didn't tell sd to add a new \n. sd is technically correct because it's doing exactly what you are asking it to do and nothing else.
However this may not be what you want, so instead you can do this:
# replace '\n$' (new line, immediately followed by end of string) by 'Ipsum\n'
# don't forget to re-add the `\n` that you removed (if you want it)
$ echo "Lorem" | sd --flags e '\n$' 'Ipsum\n'
LoremIpsum
If you have a multi-line string, but you want to append to the end of each individual line:
$ ls
foo bar baz
$ ls | sd '\n' '/file\n'
bar/file
baz/file
foo/file
I want to prepend my sql script with "set" statement before running it.
So I echo the "set" instruction, then pipe it to cat. Command cat takes two parameters : STDIN marked as "-" and my sql file, cat joins both of them to one output. Next I pass the result to mysql command to run it as a script.
echo "set #ZERO_PRODUCTS_DISPLAY='$ZERO_PRODUCTS_DISPLAY';" | cat - sql/test_parameter.sql | mysql
p.s. mysql login and password stored in .my.cnf file

Bash add to end of file (>>) if not duplicate line

Normally I use something like this for processes I run on my servers
./runEvilProcess.sh >> ./evilProcess.log
However I'm currently using Doxygen and it produces lots of duplicate output
Example output:
QGDict::hashAsciiKey: Invalid null key
QGDict::hashAsciiKey: Invalid null key
QGDict::hashAsciiKey: Invalid null key
So you end up with a very messy log
Is there a way I can only add the line to the log file if the line wasn't the last one added.
A poor example (but not sure how to do in bash)
$previousLine = ""
$outputLine = getNextLine()
if($previousLine != $outputLine) {
$outputLine >> logfile.log
$previousLine = $outputLine
}
If the process returns duplicate lines in a row, pipe the output of your process through uniq:
$ ./t.sh
one
one
two
two
two
one
one
$ ./t.sh | uniq
one
two
one
If the logs are sent to the standard error stream, you'll need to redirect that too:
$ ./yourprog 2>&1 | uniq >> logfile
(This won't help if the duplicates come from multiple runs of the program - but then you can pipe your log file through uniq when reviewing it.)
Create a filter script (filter.sh):
while read line; do
if [ "$last" != "$line" ]; then
echo $line
last=$line
fi
done
and use it:
./runEvilProcess.sh | sh filter.sh >> evillog

Resources