scala VS python2 (linux or shell)

PS:只考虑最新版的scala和python2.x,由于python3.x和python2.x区别比较大,而且主流的一些开源项目都用的python2,目前python2一点点在兼容python3

1.安装

PS:python2和scala的安装都很简单

(1)python->

到官网下载相应的版本http://www.python.org/ftp/python/

 以Python-2.6.6为例

解压 tar xvzf Python-2.6.6.tgz
     cd Python-2.6.6

编译安装python
./configure –prefix=/usr/local/python2.6
make
make install

创建一个python的链接
ln -sf /usr/local/python/bin/python2.6 /usr/bin/python

python -V 显示版本信息

Python 2.6.6

(2)scala

官网下载相应版本 http://www.scala-lang.org/download/all.html 以scala-2.10.4为例

解压 tar xvzf scala-2.10.4.tgz

配置环境变量

export SCALA_HOME=/usr/scala/scala-2.10.3

export PATH=$PATH:$SCALA_HOME/bin

scala -version 显示版本信息

 Scala code runner version 2.10.4 -- Copyright 2002-2013, LAMP/EPFL

2.python scala 调用相同的linux和shell命令对比

(1)python的功能是按照模块划分,调用linux命令有三种方式,及三个模块可以使用

    一,os 模块

import os

#system方法 会创建子进程运行外部程序,方法只返回外部程序的运行结果。这个方法比较适用于外部程序没有输出结果的情况。
os.system("echo \"Hello World\"")
Hello World #直接打印命令信息
0 #命令正常返回值

# 使用val接收返回值
val = os.system("ls -lt | grep \"test\"")  #输出信息
print val
256 #调用一个没有返回结果的命令返回256 有返回结果返回0
#popen方法 当需要得到外部程序的输出结果时,本方法非常有用,返回一个类文件对象,调用该对象的read()或readlines()方法可以读取输出内容

os.popen('ls -lt') #不能输出命令信息,返回对象
print os.popen('ls -lt').read() #调用read()方法输出命令信息


 

  

    二,commands 模块

 

import commands

commands模块的getoutput方法,同popen的区别是一个返回类文件对象,而getoutput将外部程序的输出结果当作字符串返回
#commands.getstatusoutput(cmd)        返回(status, output)
#commands.getoutput(cmd)              只返回输出结果
#commands.getstatus(file)             返回ls -ld file的执行结果字符串,调用了getoutput
#---------------------------------------------------
commands.getstatusoutput('ls -lt')
commands.getoutput('ls -lt')
commands.getstatus('ls -lt')

 

  三,subprocess 模块  启动一个新的进程并且与之通信

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions: os.system os.spawn* os.popen* popen2.* commands.*

PS:官网说该模块意在替换那些旧的模块,包括上边描述的两个模块

import subprocess

subprocess.Popen(
args, #可以是字符串或者序列类型(如:list,元组),用于指定进程的可执行文件及其参数。如果是序列类型,第一个元素通常是可执行文件的路径。我们也可以显式的使用executeable参数来指定可执行文件的路径。
bufsize=0,#指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲(全缓冲)
executable=None,
stdin=None,#分别表示程序的标准输入、输出、错误句柄。他们可以是PIPE,文件描述符或文件对象,也可以设置为None,表示从父进程继承。
stdout=None,
stderr=None,
preexec_fn=None,#只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用。
close_fds=False,#在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。我们不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。
shell=False,#设为true,程序将通过shell来执行。
cwd=None,#用于设置子进程的当前目录
env=None,#是字典类型,用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承
universal_newlines=False,#不同操作系统下,文本的换行符是不一样的。如:windows下用'/r/n'表示换,而Linux下用'/n'。如果将此参数设置为 True,Python统一把这些换行符当作'/n'来处理。startupinfo与createionflags只在windows下用效,它们将被 传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等。
startupinfo=None,#startupinfo与createionflags只在windows下有效,它们将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等。
creationflags=0
)

#Popen方法

#1)、Popen.poll():用于检查子进程是否已经结束。设置并返回returncode属性。

#2)、Popen.wait():等待子进程结束。设置并返回returncode属性。

#3)、Popen.communicate(input=None):与子进程进行交互。向stdin发送数 据,或从stdout和stderr中读取数据。可选参数input指定发送到子进程的参数。Communicate()返回一个元组: (stdoutdata, stderrdata)。注意:如果希望通过进程的stdin向其发送数据,在创建Popen对象的时候,参数stdin必须被设置为PIPE。同样,如 果希望从stdout和stderr获取数据,必须将stdout和stderr设置为PIPE。

#4)、Popen.send_signal(signal):向子进程发送信号。

#5)、Popen.terminate():停止(stop)子进程。在windows平台下,该方法将调用Windows API TerminateProcess()来结束子进程。

#6)、Popen.kill():杀死子进程。

#7)、Popen.stdin:如果在创建Popen对象是,参数stdin被设置为PIPE,Popen.stdin将返回一个文件对象用于策子进程发送指令。否则返回None。

#8)、Popen.stdout:如果在创建Popen对象是,参数stdout被设置为PIPE,Popen.stdout将返回一个文件对象用于策子进程发送指令。否则返回None。

#9)、Popen.stderr:如果在创建Popen对象是,参数stdout被设置为PIPE,Popen.stdout将返回一个文件对象用于策子进程发送指令。否则返回None。

#10)、Popen.pid:获取子进程的进程ID。

#11)、Popen.returncode:获取进程的返回值。如果进程还没有结束,返回None。

#12)、subprocess.call(*popenargs, **kwargs):运行命令。该函数将一直等待到子进程运行结束,并返回进程的returncode。如果子进程不需要进行交互,就可以使用该函数来创建。

#13)、subprocess.check_call(*popenargs, **kwargs):与subprocess.call(*popenargs, **kwargs)功能一样,只是如果子进程返回的returncode不为0的话,将触发CalledProcessError异常。在异常对象中,包括进程的returncode信息。

retcode = subprocess.call(["ls", "-lt"])
#输出信息
print retcode
0 #有返回值
#也可以用如下方式
retcode = subprocess.check_call(["ls", "-l"])
print retcode

#脚本内容 sunxu.sh
#!/bin/sh

sbin="`dirname "$0"`"
echo "\$0=$0"
echo "\$1=$1"
echo "sbin=$sbin"
sbin="`cd "$sbin"; pwd`"
echo "sbin=$sbin"
echo ${BASH_SOURCE[0]}
echo "${BASH_SOURCE:-$0}"

#调用脚本
subprocess.Popen('/opt/users/sunxu/sunxu.sh', shell=True) #或者
subprocess.call('/opt/users/sunxu/sunxu.sh', shell=True)

#两者的区别是前者无阻塞,会和主程序并行运行,后者必须等待命令执行完毕,如果想要前者编程阻塞可以这样
  s = subprocess.Popen('/opt/users/sunxu/sunxu.sh', shell=True)
  s.wait()
 
#程序返回运行结果 它会返回一个元组:(stdoutdata, stderrdata)
s = subprocess.Popen('/opt/users/sunxu/sunxu.sh', shell=True, stdout=subprocess.PIPE)
s.communicate()
('$0=/opt/users/sunxu/sunxu.sh\n$1=\nsbin=/opt/users/sunxu\nsbin=/opt/users/sunxu\n/opt/users/sunxu/sunxu.sh\n/opt/users/sunxu/sunxu.sh\n', None)
 
#subprocess还有另一种更简单方法,效果一样,它会返回stdout  版本>2.6 否则报错 :AttributeError: 'module' object has no attribute 'check_output'
s = subprocess.check_output('/opt/users/sunxu/sunxu.sh', shell=True)
s
('$0=/opt/users/sunxu/sunxu.sh\n$1=\nsbin=/opt/users/sunxu\nsbin=/opt/users/sunxu\n/opt/users/sunxu/sunxu.sh\n/opt/users/sunxu/sunxu.sh\n', None)
 
 
#subprocess.PIPE实际上为文本流提供一个缓存区。直到communicate()方法从PIPE中读取出PIPE中的文本.要注意的是,communicate()是Popen对象的一个方法,该方法会阻塞父进程,直到子进程完成。
child1 = subprocess.Popen(["ls","-l"], stdout=subprocess.PIPE)
child2 = subprocess.Popen(["wc"], stdin=child1.stdout,stdout=subprocess.PIPE)
out = child2.communicate()
print out
#流程如下
child1.stdout-->subprocess.PIPE
child2.stdin<--subprocess.PIPE
child2.stdout-->subprocess.PIPE
 
child.poll()# 检查子进程状态
child.kill()# 终止子进程
child.send_signal()# 向子进程发送信号
child.terminate() # 终止子进程

 

 

 

(2)scala已经把功能封装在scala.sys.process 引用之后可以直接使用,里边包含很多隐式的方法可以调用

  

import scala.sys.process._

#理解下面的命令就明白process的用法了
"ls" #| "grep .scala" #&& Seq("sh", "-c", "scalac *.scala") #|| "echo nothing found" lines

#使用!结尾表示使用外部命令
"ls -lt"!
#管道不能在命令中直接使用 例如:"ls -lt | grep test" 正确做法
"ls -lt" #| "grep test"!

脚本内容 sunxu.sh
#!/bin/sh
echo "hello world"

"sh /opt/test/sunxu.sh"!
#输出
hello world

#存储变量数据 用!!把结果以字符串的形式返回
val result="ls -lt" #| "grep Hood"!!
#输出
println(result)

#使用逻辑操作 #&& , #|| 格式如下
cmd1 #&& cmd2
cmd1 #|| cmd2
"echo hello" #&& "echo world"!
"echo hello" #|| "echo world"!

#重定向 #< 或者 #>
#eg:把结果重定向到文件中
"ls -al .." #> new java.io.File("output.txt")!
#eg:查询网页中的内容
"grep Scala" #< new java.net.URL("http://horstmann.com/index.html")!
#把网页导成html文件
import java.io.File import java.net.URL new URL("http://www.scala-lang.org/") #> new File("scala-lang.html") !

#追加操作 #>> 或者 #<<
#eg:追加内容到文件尾部
"ls -al .." #>> new java.io.File("output.txt")! 

 

scala官网的几个列子

  • Return status of the process (! methods)
  • Output of the process as a String (!! methods)
  • Continuous output of the process as a Stream[String] (lines methods)
  • The Process representing it (run methods)
import scala.sys.process._

// This uses ! to get the exit code
def fileExists(name: String) = Seq("test", "-f", name).! == 0

// This uses !! to get the whole result as a string
val dirContents = "ls".!!

// This "fire-and-forgets" the method, which can be lazily read through
// a Stream[String]
def sourceFilesAt(baseDir: String): Stream[String] = {
  val cmd = Seq("find", baseDir, "-name", "*.scala", "-type", "f")
  cmd.lines
}

 

Handling Input and Output

import scala.sys.process._

// An overly complex way of computing size of a compressed file
def gzFileSize(name: String) = {
  val cat = Seq("zcat", name)
  var count = 0
  def byteCounter(input: java.io.InputStream) = {
    while(input.read() != -1) count += 1
    input.close()
  }
  cat ! new ProcessIO(_.close(), byteCounter, _.close())
  count
}

// This "fire-and-forgets" the method, which can be lazily read through
// a Stream[String], and accumulates all errors on a StringBuffer
def sourceFilesAt(baseDir: String): (Stream[String], StringBuffer) = {
  val buffer = new StringBuffer()
  val cmd = Seq("find", baseDir, "-name", "*.scala", "-type", "f")
  val lines = cmd lines_! ProcessLogger(buffer append _)
  (lines, buffer)
}

 

posted @ 2015-09-28 18:15  悟寰轩-叶秋  阅读(2178)  评论(0编辑  收藏  举报