我解析命令輸出並將結果放入數組。Bash,奇怪的變量範圍時,填充結果數組
工作正常,直到退出內部循環 - 輸出數組爲空。
declare -a KEYS
#-----------------------------------------------------------------------------#
get_keys()
{
# this extracts key NAMES from log in format "timestamp keycode"
$glue_dir/get_keys $ip | while read line; do
echo line: $line
set -- $line # $1 timestamp $2 keycode
echo 1: $1 2: $2
key=(`egrep "\\s$2$" "$glue_dir/keycodes"`) # tested for matching '40' against 401, 402 etc
set -- $key # $1 key name $2 keycode
KEYS+=("$1")
echo key $1
echo KEYS inside loop: "${KEYS[@]}"
done
echo KEYS outside loop: "${KEYS[@]}"
}
時運行agains兩個輸出線路輸出:
line: 1270899320451 38
1: 1270899320451 2: 38
key UP
KEYS inside loop: UP
line: 1270899320956 40
1: 1270899320956 2: 40
key DOWN
KEYS inside loop: UP DOWN
KEYS outside loop:
我花了一個小時試圖弄清楚這一點。請幫忙。 ;-)
感謝的人,你的問題救了我!以及答案! – Oz123