关于python调用shell命令

moxiaomomo
博客专家认证
2014-01-15 11:27:05

val = "test_res"
os.environ['var'] = val
os.popen('echo -ne $var')


运行的结果为 -ne test_res

-ne是用来限制不换行刷新输出的,不应该直接被echo啊。
请问echo命令下选项-ne为什么没有被正确引用呢?
...全文
281 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
szqh97 2014-01-27
  • 打赏
  • 举报
回复
http://stackoverflow.com/questions/905221/os-system-execute-command-under-which-linux-shell os.system() just calls the system() system call ("man 3 system"). On most *nixes this means you get /bin/sh. Note that export VAR=val is technically not standard syntax (though bash understands it, and I think ksh does too). It will not work on systems where /bin/sh is actually the Bourne shell. On those systems you need to export and set as separate commands. (This will work with bash too.) 也只有这样才能讲通了, 在/bin/sh下 echo 是没有 -e这项的,
livesguan 2014-01-24
  • 打赏
  • 举报
回复
这还有其它的方法 1.用os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 如a=os.popen(cmd).read() 使用 a.rstrip() 进行去除换行符“\n" 2.用 commands 模块。其实也是对popen的封装。 3.subprocess.Popen
livesguan 2014-01-24
  • 打赏
  • 举报
回复
用os.system(cmd) 取不了返回值。
moxiaomomo 2014-01-15
  • 打赏
  • 举报
回复
换用了subprocess.call(['echo', '-n', '-e', val]) 就可以了。os.system还是不知道怎么解决。
moxiaomomo 2014-01-15
  • 打赏
  • 举报
回复
不好意思,写错了...是os.system

    val = "test_res"
    os.environ['var'] = val
    os.system('echo -ne $var')

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧