我有一个命令,它在执行时返回我想要存储在 bash 数组中的一组数字。
vihaan@trojan:~/trash$ xdotool search brain
Defaulting to search window name, class, and classname
52428804
50331651
62914564
65011896
48234499
如何将这些值存储在数组中?
在这个简单的例子中:
array=( $(xdotool search brain) )
如果输出更复杂(例如,行中可能有空格),您可以使用 bash 内置mapfile
:
mapfile -t array < <(xdotool search brain)
(help mapfile
了解更多信息)
declare -a myarr # declare an array
myarr=($(grep -v "Defaulting" $(xdotool search brain) | awk '{printf $1" "}')) # Fill the array with all the numbers from the command line
echo ${myarr[*]} # echo all the elements of the array
或者
echo ${myarr[1]} # First element of the array
You could write another command the expects input and puts said input into an array. So you would pipe the output from the first command to your toArray
command. Then do what you need to with the toArray
output.