Tuesday, May 7, 2024
 Popular · Latest · Hot · Upcoming
30
rated 0 times [  30] [ 0]  / answers: 1 / hits: 131056  / 3 Years ago, tue, may 4, 2021, 4:35:01

This is command: pdc status -a 2>&1 | grep 'okay' It gives the following output



[okay     ]: you are currently listening: 33
[okay ]: you are currently listening: 22
[okay ]: you are currently listening: 11


I have written this command in shell scrip file. But I want to store the output of this command into the array for some processing on each of the index value in the array.



How can I store the output of this command into the array?


More From » scripts

 Answers
6

If you just want the numbers at the end of each line:



numbers=( $(pdc ... | grep -oP 'okay.+?Kd+$') )


If you want to store each line into the array



mapfile -t lines < <(pdc ...)


To retrieve the data from the arrays:



for (( i=0; i<${#numbers[@]}; i++ )); do echo ${numbers[i]}; done
echo
printf "%s
" "${lines[@]}"




33
22
11

[okay ]: you are currently listening: 33
[okay ]: you are currently listening: 22
[okay ]: you are currently listening: 11

[#26350] Wednesday, May 5, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
iething

Total Points: 49
Total Questions: 127
Total Answers: 112

Location: Luxembourg
Member since Tue, Jan 25, 2022
2 Years ago
;