以下是我尝试使用python执行的代码
from subprocess import Popen, PIPE
cmd = 'if (-e "../a.txt") then \n ln -s ../a.txt . \n else \n echo "file is not present " \n endif'
ret_val = subprocess.call(cmd,shell="True")
执行时会出现以下错误消息
/bin/sh: -c: line 5: syntax error: unexpected end of file
答案 0 :(得分:6)
在sh脚本中,if
以fi
而不是endif
终止。
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
或者只是在Python中编写代码:
import os
if os.path.exists('../a.txt'):
print 'Exists'
os.symlink('../a.txt', 'a.txt')
else:
print 'Does not exist'
如果你真的想运行tcsh命令,那么:
import shlex
import subprocess
args = ['tcsh', '-c'] + shlex.split(some_tcsh_command)
ret = suprocess.call(args)