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.

0

There are 0 best solutions below