gpt4 book ai didi

python - 读取-未知选项

转载 作者:太空宇宙 更新时间:2023-11-04 03:34:55 24 4
gpt4 key购买 nike

我正在使用 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()

执行命令。

最佳答案

您的交互式 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

关于python - 读取-未知选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31951396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com