[英]How to store command return values in bash array
我有一個命令,在執行時返回我想要存儲在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
您可以使用期望輸入編寫另一個命令並將所述輸入放入數組中。 因此,您可以將第一個命令的輸出傳遞給toArray
命令。 然后使用toArray
輸出執行您需要的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.