2016-07-25 49 views
0

我想從我的屏幕上輸入讀取條目,並將其推到一個數組讀取屏幕值到一個數組

echo "enter the day, example 01 or 02 etc..enter CTRL+D to break"; 

while read line; 
do 
    my_array=("${my_array[@]}" $line) 
done 

但是當我試着執行它時,我收到以下錯誤

第5行的語法錯誤:`my_array ='不是預期的。

任何輸入什麼是外殼檢測

PS的語法錯誤:我已經運行在殼以及bash的上述snipet,錯誤仍然存​​在 OS:AIX 7.1

+0

[linux的bash腳本得到一個陣列中的用戶輸入,並存儲(可能的重複http://stackoverflow.com/questions/17199736/linux-bash-script-get-user-input-and-store -in-a-array) – Inian

+0

很可能你從* bash shell運行腳本*,而不是*通過* bash shell。 –

回答

1

可以運行這在bash環境下如下所示;

[email protected]:/tmp:>cat testksh.sh 
#!/bin/ksh 
echo "enter the day, example 01 or 02 etc..enter CTRL+D to break"; 
while read line; 
do 
    my_array=("${my_array[@]}" $line) 
done 

[email protected]:/tmp:>./testksh.sh 
    enter the day, example 01 or 02 etc..enter CTRL+D to break 
    ./testksh.sh[3]: 0403-057 Syntax error at line 5 : `(' is not expected. 

[email protected]:/tmp:>cat testbash.sh 
#!/bin/bash 
echo "enter the day, example 01 or 02 etc..enter CTRL+D to break"; 
while read line; 
do 
    my_array=("${my_array[@]}" $line) 
done 


[email protected]:/tmp:>./testbash.sh 
enter the day, example 01 or 02 etc..enter CTRL+D to break 
01 
相關問題