0

I’m trying to run a piped Linux command in ruby by enclosing it in backticks`` and by using Mixlib::ShellOut. But in both the cases, I'm getting an empty string as the output even though the actual output looks like this i | kernel-default-devel | package | 3.0.101-108.21.1 | x86_64 | SLES11-SP4-Updates

Actually, the zypper search command will display the output in a table format.

The command I’m using is zypper search -s kernel | grep -P '(^|\s)\Kkernel-default-devel(?=\s|)' | grep (rpm -qa | grep kernel-default-[0-9] | sort -Vr | head -1 | cut -c 16- | awk ‘{print substr($0, 1, length($0)-7)}’)

The code for Mixlib::ShellOut is given below.

current_package_cmd = "zypper search -s kernel | grep -P '(^|\s)\Kkernel-default-devel(?=\s|$)' | grep $(rpm -qa | grep kernel-default-[0-9] | sort -Vr | head -1 | cut -c 16- | awk '{print substr($0, 1, length($0)-7)}')"
cmd = Mixlib::ShellOut.new(current_package_cmd )
cmd.run_command 
current_package = cmd.stdout
status = cmd.exitstatus
log "current package is #{current_package} and status is #{status}"

The value of output variable current_package is empty string and the exitstatus is 1.

Kindly advise how to resolve the issue.

  • Since the exit status is 1, your command seems to fail when executed via shellout. Have you checked the stderr output? – kaikuchn Feb 15 '18 at 10:01
  • @kaikuchn : The stderr is also empty. This is the code I have added to get the output, exitstatus and error. current_package = cmd.stdout current_package_err = cmd.stderr status = cmd.exitstatus log "current package is #{current_package} and status is #{status} and error is #{current_package_err} package version is #{package_version}" And this is the output while chef run log[current package is and status is 1 and error is package version is 3.0.101-108.21.1] action write – Rajalakshmirm Feb 15 '18 at 11:39
  • Here's one more thing to try: prefix your command with `set -x;` to make the shell echo commands with expanded variables to stdout.. Then you could check if anything is amiss. – kaikuchn Feb 15 '18 at 21:53

0 Answers0