![通过 python 执行 bash 脚本出现语法错误](https://linux22.com/image/987844/%E9%80%9A%E8%BF%87%20python%20%E6%89%A7%E8%A1%8C%20bash%20%E8%84%9A%E6%9C%AC%E5%87%BA%E7%8E%B0%E8%AF%AD%E6%B3%95%E9%94%99%E8%AF%AF.png)
因此,我尝试通过 Python 获取文件。代码如下:
import os
source = 'source /home/software/cadence/release2013/setup/xyz.setup p'
os.system(source)
由此得出以下结论:
/home/software/cadence/release2013/setup/xyz.setup: line 23: syntax error near unexpected token `$HT'
/home/software/cadence/release2013/setup/xyz.setup: line 23: `switch ($HT)'
现在,我向你保证。xyz.setup 没有任何问题。它已经被无数次地引用,并且被许多人引用过。此外,当我直接通过终端引用它时
source /home/software/cadence/release2013/setup/xyz.setup p
Program Research license set
它工作得很好!我很困惑。有人能帮忙吗?如果不行,还有其他方法吗?我试过了subprocess.call
,但是参数无法传递给文件。
这是 xyz.setup,尽管其中肯定没有错误。
#Source script for blabla
# Use an argument to determine the type
set fl=false
set Usage = "Usage: source /imec/software/cadence/release2013/setup/blabla.csh [e|c|p|com]"
if ($# != 1) then
echo ""
echo "$Usage"
echo " with: e -> Educational research"
echo " c -> Cadence Academic Network (CAN)"
echo " p -> Program Research"
echo " com -> Commercial license"
exit 1
else
set option=$1
set fl=true
endif
set OK = false
setenv HT `/usr/local/bin/hosttype`
switch ($HT)
case LINUX
switch (`uname -m`)
case x86_64
setenv CDS_AUTO_64BIT ALL
set OK = true
breaksw
case i686
set OK = true
breaksw
default:
echo "Not the correct operating system, LINUX 64bit required (x86_64)"
exit 1
breaksw
endsw
breaksw
default:
echo "Not the correct operating system, LINUX 64bit required (x86_64)"
exit 1
breaksw
endsw
#if ($fl == true) then
switch ($option)
case e
setenv xuxnxu 34234@dfsdfds3
echo " license is set"
case E
setenv xuxnxu 34234@dfsdfds3
echo " license is set"
breaksw
case c
setenv xuxnxu 34234@dfsdfds3
echo "CAN license set"
breaksw
case C
setenv xuxnxu 34234@dfsdfds3
echo "CAN license set"
breaksw
case p
setenv xuxnxu 34234@dfsdfds3
echo "Program Research license set"
breaksw
case P
setenv xuxnxu 34234@dfsdfds3
echo "Program Research license set"
breaksw
case com
setenv xuxnxu 34234@dfsdfds3
echo "Commercial license set"
breaksw
case COM
setenv xuxnxu 34234@dfsdfds3
echo "Commercial license set"
breaksw
default:
set fl=false
echo "Not the correct option specified"
echo "$Usage"
breaksw
endsw
#endif
#bla bla
setenv CDS_INST_DIR /home/software/cadence/release2013/bla bla
setenv CDS_DFII_BIN $CDS_INST_DIR/tools/dfII/bin
setenv CDS_FLEX_BIN $CDS_INST_DIR/tools/bin
set path= ($CDS_INST_DIR/tools/dfII/bin \
$CDS_INST_DIR/tools/bin \
/home/software/cadence/release2013/bla bla/tools/bin $path)
#echo "Cadence setup bla bla bla sourced"
答案1
您的脚本语法正确csh
,但os.system
调用默认 shell /bin/sh
。如果您希望脚本被正确解释,请使用
subprocess.call(['/bin/csh','-c','source /home/software/cadence/release2013/setup/xyz.setup'])