How do get the full command line in ruby? - ruby

How do I get the full command line in ruby?
$ rails c
> $0
=> "script/rails"
> ARGV
[]
> `ps -eo "%p|$|%a" | grep '^\\s*#{Process.pid}'`.strip.split("|$|")[1]
=> "/home/sam/.rvm/rubies/ruby-1.9.3-p194-perf/bin/ruby script/rails console"
Is there anything cleaner than ninja ps I can do to get the same results?
To clarify, in case there is confusion, I want the exact same output as:
`ps -eo "%p|$|%a" | grep '^\\s*#{Process.pid}'`.strip.split("|$|")[1]
ARGV is coming back blank.
$0 is missing the full path.

I'd use:
#!/usr/bin/env ruby
puts "Process ID: #{ $$ }"
puts `ps axw`.split("\n").select{ |ps| ps[ /\A#{ $$ }/ ] }
Running that inside a script outputs:
18222 s000 S+ 0:00.25 /Users/foo/.rbenv/versions/1.9.3-p385/bin/ruby /Users/foo/.rbenv/versions/1.9.3-p385/bin/rdebug /Users/foo/Desktop/test.rb

Related

Bash script sshd group check

hello I have script written in python. This script is saving results output of bash script modules. I have to written module that check that are user in
cat /etc/group |grep sshd and write in excel
in group 203. When i execute command command in host i recveive output
staff:!:,sshd,sosnix,xo29321,siwja8211,293912,29314
sshd:!:203:sshd
In this module "compilant" and "actual_value" this is row's from excel
My code of module
module_id="XX.TEST"
echo " === $module_id module === "
command_output=`cat /etc/group |grep sshd`
if [ "$command_output" = "cat /etc/group |grep sshd" ]; then
compliant="Yes"
actual_value="GUID 203"
else
compliant="No"
actual_value="N/A"
fi
# SCRIPT RESULT
echo :::$module_id:::$compliant:::$actual_value:::
echo " === End of $module_id module === "
and this script write in my excel result No In compilant and N/A in actual value
This is correct behavior.
You compare two strings: first string with result of cat /etc/group |grep sshd execution and second string the command itself - "cat /etc/group |grep sshd".
These strings are not equivalent. So 'If' goes by 'else' branch and you got the mentioned output.
Please refer for https://www.gnu.org/software/bash/manual/bash.html "6.4 Bash Conditional Expressions" for more information.

Using groovy, how do you pipe multiple shell commands?

Using Groovy and it's java.lang.Process support, how do I pipe multiple shell commands together?
Consider this bash command (and assume your username is foo):
ps aux | grep ' foo' | awk '{print $1}'
This will print out usernames - one line for some processes related to your user account.
Using Groovy, the ProcessGroovyMethods documentation and code says I should be able to do this to achieve the same result:
def p = "ps aux".execute() | "grep ' foo'".execute() | "awk '{print $1}'".execute()
p.waitFor()
println p.text
However, I can't get any text output for anything other than this:
def p = "ps aux".execute()
p.waitFor()
println p.text
As soon as I start piping, the println does not print out any anything.
Thoughts?
This works for me :
def p = 'ps aux'.execute() | 'grep foo'.execute() | ['awk', '{ print $1 }'].execute()
p.waitFor()
println p.text
for an unknown reason, the parameters of awk can't be send with only one string (i don't know why! maybe bash is quoting something differently). If you dump with your command the error stream, you'll see error relative to the compilation of the awk script.
Edit : In fact,
"-string-".execute() delegate to Runtime.getRuntime().exec(-string-)
It's bash job to handle arguments containing spaces with ' or ". Runtime.exec or the OS are not aware of the quotes
Executing "grep ' foo'".execute() execute the command grep, with ' as the first parameters, and foo' as the second one : it's not valid. the same for awk
You can do this to just let the shell sort it out:
// slash string at the end so we don't need to escape ' or $
def p = ['/bin/bash', '-c', /ps aux | grep ' foo' | awk '{print $1}'/].execute()
p.waitFor()
println p.text
This has worked for me
def command = '''
ps aux | grep bash | awk '{print $1}'
'''
def proc = ['bash', '-c', command].execute()
proc.waitFor()
println proc.text
If you want to run multiple commands, you can add it in the command.
def command = '''
ls -ltr
cat secret
'''
def proc = ['bash', '-c', command].execute()
proc.waitFor()
println proc.text
If you want it async I recommend
proc.consumeProcessOutputStream(new LineOrientedOutputStream() {
#Override
protected void processLine(String line) throws IOException {
println line
}
}
);

How can I provide the $INPUT_RECORD_SEPARATOR to ruby -n -e?

I'd like to use Ruby's $INPUT_RECORD_SEPARATOR aka $/ to operate on a tab-separated file.
The input file looks like this (grossly simplified):
a b c
(the values are separated by tabs).
I want to get the following output:
a---
b---
c---
I can easily achieve this by using ruby -e and setting the $INPUT_RECORD_SEPARATOR alias $/:
cat bla.txt | ruby -e '$/ = "\t"; ARGF.each {|line| puts line.chop + "---" }'
This works, but what I'd really like is this:
cat bla.txt | ruby -n -e '$/ = "\t"; puts $_.chop + "---" '
However, this prints:
a b c---
Apparently, it doesn't use the provided separator - presumably because it has already read the first line before the separator was set. I tried to provide it as an environment variable:
cat bla.txt | $/="\n" ruby -n -e 'puts $_.chop + "---" '
but this confuses the shell - it tries to interpret $/ as a command (I also tried escaping the $ with one, two, three or four backslashes, all to no avail).
So how can I combine $/ with ruby -n -e ?
Use the -0 option :
cat bla.txt | ruby -011 -n -e 'puts $_.chop + "---" '
a---
b---
c---
-0[ octal] Sets default record separator ($/) as an octal. Defaults to \0 if octal not specified.
tabs have an ascii code of 9, which in octal is 11. Hence the -011
Use a BEGIN block, which is processed before Ruby begins looping over the lines:
$ echo "foo\tbar\tbaz" | \
> ruby -n -e 'BEGIN { $/ = "\t" }; puts $_.chop + "---"'
foo---
bar---
baz---
Or, more readably:
#!/usr/bin/env ruby -n
BEGIN {
$/ = "\t"
}
puts $_.chop + "---"
Then:
$ chmod u+x script.rb
$ echo "foo\tbar\tbaz" | ./script.rb
foo---
bar---
baz---
If this is more than a one-off script (i.e. other people might use it), it may be worthwhile to make it configurable with an argument or an environment variable, e.g. $/ = ENV['IFS'] || "\t".

chop `%` from an output produced by `dh -H` command

I am new to scripting, I am trying to write a simple script for sensu check to create an alert for Disk Space.
The below command produces an output:
#!/usr/bin/ruby
a = `df -h / | grep -v "Filesystem" | awk '{print $5}'`
puts a
Assume that the output is 35%
now I want to strip down the %, when I try using a.chop! still it is not removing the %
Could some one please help me stripping off the % from the output.
The value returned from your command has a newline at its end:
a
# => "35%\n"
To remove the % using chop you need to strip! it first:
a.strip!
a.chop!
# => "35"
Since you're going to use this in a numeric comparison just convert it to an integer in one step:
a = `df -h / | grep -v "Filesystem" | awk '{print $5}'`
puts a.to_i
# => 66

Ruby: execute bash command, capture output AND dump to screen at the same time

So my problem is that I need to have the output of running the command dumped to the screen and also capture it in a variable in a ruby script. I know that I can do the second part like this:
some_variable = `./some_kickbutt`
But my problem is that I need it to still print to the console as Hudson captures that output and records it for posterity's sake.
thanks in advance for any ideas...
Just tee the stdout stream to stderr like so:
ruby -e 'var = `ls | tee /dev/stderr`; puts "\nFROM RUBY\n\n"; puts var' | nl
ruby -e 'var = `ls | tee /dev/stderr`; puts "\nFROM RUBY\n\n"; puts var' 2>&1 | nl

Resources