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)
}

scala VS python2 (linux or shell)的更多相关文章

  1. 详解Linux交互式shell脚本中创建对话框实例教程_linux服务器

    本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息 ...

  2. linux中shell变量$#,$@,$0,$1,$2的含义解释

    linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...

  3. 使用C#给Linux写Shell脚本(下篇)

    在上篇的<使用C#给Linux写Shell脚本>结尾中,我们留下了一个关于C#如何调用BashShell的问题.在文章发布之后,我留意到有读者留言推荐使用“Pash”(一款类PowerSh ...

  4. linux中用shell获取昨天、明天或多天前的日期

    linux中用shell获取昨天.明天或多天前的日期 时间 -- :: BlogJava-专家区 原文 http://www.blogjava.net/xzclog/archive/2015/12/0 ...

  5. linux中用shell获取时间,日期

    linux中用shell获取昨天.明天或多天前的日期:在Linux中对man date -d 参数说的比较模糊,以下举例进一步说明:# -d, --date=STRING display time d ...

  6. Linux 的shell 字符串截取很有用。有八种方法。

    一 Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.linuxidc.com/123.htm 1  # 号截取,删除左边字符,保留右边字符. echo ${va ...

  7. Linux Bash Shell 快速入门

    BASH 的基本语法 最简单的例子 —— Hello World! 关于输入.输出和错误输出 BASH 中对变量的规定(与 C 语言的异同) BASH 中的基本流程控制语法 函数的使用 2.1     ...

  8. linux,shell输入反斜杠显示'W'。

    linux,shell输入反斜杠显示'W'. solution: 字体必须为"Courier New".

  9. 06 Linux下Shell介绍

    一.概述 每个人在成功登陆Linux后,系统会出现不同的提示符号,例如$,~,#等,然后你就可以开始输入需要的命令.若命令正确,系统就会依据命令的要求来执行,直到注销系统为止,在登陆到注销期间,输入的 ...

随机推荐

  1. C++点和箭头操作符用

    http://www.cnblogs.com/ManMonth/archive/2013/09/05/3302873.html C++点和箭头操作符用法区别 变量是对象的时候用“.”访问 变量是对象指 ...

  2. Hybrid App 开发初探:使用 WebView 装载页面

    Hybrid App 是混合模式应用的简称,兼具 Native App 和 Web App 两种模式应用的优势,开发成本低,拥有 Web 技术跨平台特性.目前大家所知道的基于中间件的移动开发框架都是采 ...

  3. maven + sonar, gradle + sonar

    sonar installation and configuration Download sonar http://downloads.sonarsource.com/sonarqube/ Deco ...

  4. 猫都能学会的Unity3D Shader入门指南(二)

    关于本系列 这是Unity3D Shader入门指南系列的第二篇,本系列面向的对象是新接触Shader开发的Unity3D使用者,因为我本身自己也是Shader初学者,因此可能会存在错误或者疏漏,如果 ...

  5. 实习医生格蕾第十三季/全集Grey’s Anatomy迅雷下载

    英文全名Grey's Anatomy,第13季(2016)ABC.本季看点:<实习医生格蕾>(Grey’s Anatomy)上季终集里,又一名资深演员离开了——Sara Ramirez扮演 ...

  6. command line subversion for windows

    安装 Slik SVN 之后,进入dos, 输入svn help,所有的命令和相关的信息都出来了   相关的svn命令:log.export等,可以搜索相应的文章: svn常用命令 svn log - ...

  7. 超级账本环境搭建fabric

    :gotar -C /usr/local -xzf go1.9.2.linux-amd64.tar.gz vi ~/.profile export PATH=$PATH:/usr/local/go/b ...

  8. serializeArray()与 serialize()

    serialize()序列表表格内容为字符串,用于 Ajax 请求. serializeArray()序列化表格元素 (类似 '.serialize()' 方法) 返回 JSON 数据结构数据. .s ...

  9. C++11 std::shared_ptr总结与使用

    最近看代码,智能指针用的比较多,自己平时用的少,周末自己总结总结.方便后续使用. std::shared_ptr大概总结有以下几点: (1) 智能指针主要的用途就是方便资源的管理,自动释放没有指针引用 ...

  10. VS2010新建Web网站与新建Web应用程序的区别 (转)

    在Visual Studio 2010中,除了可以使用“创建Web应用程序”的方式来构建自己的Web项目之外,还可以通过创建“Web网站”的方式来构建Web项其中,Web网站的创建方法:打开Visua ...