可以在bash中将namerefs(declare -n)与数组一起使用吗?当声明-n无法应用于数组时,文档的含义是什么?

时间:2016-11-14 16:43:47

标签: bash

在stackoverflow的另一个问题中,我问过如何将数组传递给函数。一个答案建议我以前的答案。   一个答案表明,使用-n选项声明引用,可以将数组传递给函数,如下所示,

declare -a array=( 1 2 )

function array_pass_by_reference_func() {
  local -n aug=${1:-dummy}
  echo "pass by reference : array[0] = ${aug[0]}"
  echo "pass by reference : array[1] = ${aug[1]}"
  echo "pass by reference : array = ${aug[@]}"
}

# execute an example function above
array_pass_by_reference_func "array"

# output
pass by reference : array[0] = 1
pass by reference : array[1] = 2
pass by reference : array = 1 2

看起来效果很好。 我的问题是关于在bash手册中声明-n的说明,

The -n attribute cannot be applied to array variables.

我想确认我可以使用declare -n选项将数组传递给函数吗? 我原来的问题被标记为重复,而之前的问题太旧了,无法提出这个问题。所以请让我在这里问一下。 非常感谢你。

1 个答案:

答案 0 :(得分:3)

EOFError Traceback (most recent call last) C:\Anaconda3\lib\site-packages\paramiko\sftp_client.py in _read_response(self, waitfor) 759 try: --> 760 t, data = self._read_packet() 761 except EOFError as e: C:\Anaconda3\lib\site-packages\paramiko\sftp.py in _read_packet(self) 172 def _read_packet(self): --> 173 x = self._read_all(4) 174 # most sftp servers won't accept packets larger than about 32k, so C:\Anaconda3\lib\site-packages\paramiko\sftp.py in _read_all(self, n) 158 if len(x) == 0: --> 159 raise EOFError() 160 out += x EOFError: During handling of the above exception, another exception occurred: SSHException Traceback (most recent call last) <ipython-input-49-b52d34c6bd07> in <module>() ----> 1 sftp.get('/GEO/OUT/test.csv','ESRI_OUT/te.csv') C:\Anaconda3\lib\site-packages\paramiko\sftp_client.py in get(self, remotepath, localpath, callback) 719 """ 720 with open(localpath, 'wb') as fl: --> 721 size = self.getfo(remotepath, fl, callback) 722 s = os.stat(localpath) 723 if s.st_size != size: C:\Anaconda3\lib\site-packages\paramiko\sftp_client.py in getfo(self, remotepath, fl, callback) 697 fr.prefetch(file_size) 698 return self._transfer_with_callback( --> 699 reader=fr, writer=fl, file_size=file_size, callback=callback 700 ) 701 C:\Anaconda3\lib\site-packages\paramiko\sftp_client.py in _transfer_with_callback(self, reader, writer, file_size, callback) 596 size = 0 597 while True: --> 598 data = reader.read(32768) 599 writer.write(data) 600 size += len(data) C:\Anaconda3\lib\site-packages\paramiko\file.py in read(self, size) 209 read_size = max(self._bufsize, read_size) 210 try: --> 211 new_data = self._read(read_size) 212 except EOFError: 213 new_data = None C:\Anaconda3\lib\site-packages\paramiko\sftp_file.py in _read(self, size) 163 size = min(size, self.MAX_REQUEST_SIZE) 164 if self._prefetching: --> 165 data = self._read_prefetch(size) 166 if data is not None: 167 return data C:\Anaconda3\lib\site-packages\paramiko\sftp_file.py in _read_prefetch(self, size) 143 if self._prefetch_done or self._closed: 144 break --> 145 self.sftp._read_response() 146 self._check_exception() 147 if offset is None: C:\Anaconda3\lib\site-packages\paramiko\sftp_client.py in _read_response(self, waitfor) 760 t, data = self._read_packet() 761 except EOFError as e: --> 762 raise SSHException('Server connection dropped: %s' % str(e)) 763 msg = Message(data) 764 num = msg.get_int() SSHException: Server connection dropped: 页面的man标题下,它还说:

  

但是,nameref变量可以引用数组变量和下标数组变量。

换句话说:

  • 您不能将nameref变量本身声明为数组(PARAMETERS会导致语法错误)。
  • 但是一个nameref变量可以引用一个数组。

因此,您的方法应该是安全的。