2015-04-03 59 views
0

我得到一個「」語法錯誤:「(」unexpected「(expected」}「)」在我的腳本中創建一個變量時出錯。從SQLPLUS並將其返回作爲數組的輸出這是我使用的功能:圓括號問題和使用sqlplus和shell腳本聲明數組

get_list() { 
ip=$1 
port=$2 
db_user=$3 
db_pass=$4  

names_array=(`sqlplus -S $db_user/[email protected]$ip:$port << EOF 
    set heading off 
    set feedback off; 
    select username from users; 
EOF`) 

element=0 
while [ $element -lt ${names_array[*]} ] 
    do 
     echo ${element}") "${names_array[$element]} 
     let element=$element+1; 
    done 
printf "\n\n" 
read -p "Choice:" selection 

sqlplus -S $db_user/[email protected]$ip:$port << EOF 
    drop user ${names_array[$selection]}; 
    quit; 
EOF 


} 

當我在這裏聲明數組將出現錯誤這是括號即bash的抱怨:

names_array=(`sqlplus -S $db_user/[email protected]$ip:$port << EOF 

任何幫助是肯定的iated。提前致謝。

回答

1

某些shell(sh,dash)不支持數組。使用支持它們的bash。

我可以複製在衝刺錯誤:

arr=(`cat << EOF 
a 
b 
c 
EOF`) 
dash: 11: Syntax error: word unexpected (expecting ")") 

在bash,代碼工作沒有問題:

arr=(`cat << EOF 
a 
b 
c 
EOF`) 
echo ${arr[1]} 

輸出:

b