0

I'm building a script in bash for use on Linux, and I use the output of a executable to fill parameters:

version=$("${path_exec}" -version | awk '{if($1=="kernel" && $2=="release") print $3}')
patch=$("${path_exec}" -version | awk '{if($1=="patch" && $2=="number") print $3}')

This will run the executable defined in "path_exec" twice, which is time consuming. Is there a way to assign the version and path variable with a value using only one execution of "path_exec"?

An example of what I've tried to tackle this is shown below, but I don't think this will do what I want:

${path_hostexec} -version | awk '{if($1=="kernel" && $2=="release") {version_agent = $3;} else if($1=="patch" && $2=="number") {patch_agent = $3;}}'
Repeat64
  • 31
  • 3

3 Answers3

0

Could you please try following. Since I didn't have output of path_exec command so couldn't test it.

myarr=($("${path_exec}" -version | awk '{if($1=="kernel" && $2=="release");val=$3} {if($1=="patch" && $2=="number") print val,$3}'))
#Now get every element in the array
for i in "${myarr[@]}"
do
  echo $i
done

What I have done is:

  • Merged your both awk programs into one to make it run on a single time.
  • Now creating an array by output of awk command output(which should be val1 val2 as an example format)
  • Once an array created then we could get its all values by for loop or you could get its specific value by mentioning its index eg--> myarr[1] to print 2nd element.
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
0

Output both values on a single line, and let read separate the line into its two parts.

IFS=, read version patch < <($path_exec -version |
                             awk '/kernel release/ {v=$3}
                                  /patch number/ {p=$3}
                                  END {print v","p}
                                 ')
chepner
  • 497,756
  • 71
  • 530
  • 681
0

Thanks guys, I've managed to get it working thanks to your input:

version_patch_agent=$("${path_hostexec}" -version | awk '/kernel release/ {v=$3} /patch number/ {p=$3} END {print v" patch "p}')

This puts the version and patch number into a variable that I can just echo to get the info on the screen. Thanks again all!!

Repeat64
  • 31
  • 3