读取-a未知选项

时间:2015-08-11 20:31:34

标签: python linux bash shell

我正在使用python脚本执行shell命令。这是命令:

ntpservlist=( $OMC_NTPSERV ) && IFS=',' read -ra ntplist <<< "$ntpservlist" &&  for i in "${ntplist[@]}" ; do echo "server $i" >> /etc/inet/ntp.conf ; done

当我使用脚本执行命令时,出现以下错误:

/bin/sh[1]: read: -a: unknown option
Usage: read [-ACprsv] [-d delim] [-u fd] [-t timeout] [-n count] [-N count]
            [var?prompt] [var ...]

但是如果我使用命令行执行相同的命令,它会正确执行而不会出现任何错误。

我正在使用:

proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    (out, err) = proc.communicate()

执行命令。

1 个答案:

答案 0 :(得分:0)

您的交互式shell是bash,但Popen使用的系统shell有点ksh。要改为使用bash,请使用executable选项:

proc = subprocess.Popen(command,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        shell=True,
                        executable="/bin/bash") # or whatever the right path is
(out, err) = proc.communicate()

您的大部分命令似乎都有效ksh,但有一点不同之处在于read -A,而非read -a用于填充数组。