Saving the output of a command to an array

I have a problem with the bash command output. I want to save the result of a command for an array and cut off the first part at [0] and seccond at [1]

result=$(findId PATTERN | tr -s " " | cut -d " " -f 1) 

The output of findID:

 id errorcode nameOfProgramRunned 

Example:

 12345 "compilation problem" fullScan.java 12346 "other problem" basicTest.java 

I want to save id in array[0] and nameofProgramRunned until array[1]

The main problem is that I want to avoid executing the command twice beacause, the result may be different.

Current code:

 #!/bin/sh echo 'Enter error code:' read errorLine result=$(findID PATTERN | awk '{print $1}') while read -r line do dir="/proj/jobs/"$line"/screenlog.*" if [ -e $dir ] then if grep -qi "$errorLine" $dir then echo -e "\e[101m$line:\e[49m" grep $dir -i -e "$errorLine" fi fi done <<<"$result" 

I want to add nameOfProgramRunned to final grep

And I use bash

+5
source share
1 answer

Edited for open-length arrays.

 echo -e '12345\t"compilation problem"\tfullScan.java' | { IFS=$'\t' read -r -a FOO echo number: ${FOO[0]} echo string: ${FOO[1]} echo file: ${FOO[2]} } 

Note that IFS is set to a tab delimiter

Also look

 help read # bash built in help 
0
source

Source: https://habr.com/ru/post/1275993/


All Articles