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) #两者的区别是前者无阻塞,会和主程序并行运行,后者必须等待命令执行完毕,如果想要前者编程阻塞可以这样
child2 = subprocess.Popen(["wc"], stdin=child1.stdout,stdout=subprocess.PIPE)
out = child2.communicate()
print out
(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
- scala.sys.process.ProcessIO: provides the low level abstraction.
- scala.sys.process.ProcessLogger: provides a higher level abstraction for output, and can be created through its object companion
- scala.sys.process.BasicIO: a library of helper methods for the creation of
ProcessIO
. - This package object itself, with a few implicit conversions.
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)的更多相关文章
- 详解Linux交互式shell脚本中创建对话框实例教程_linux服务器
本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息 ...
- linux中shell变量$#,$@,$0,$1,$2的含义解释
linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...
- 使用C#给Linux写Shell脚本(下篇)
在上篇的<使用C#给Linux写Shell脚本>结尾中,我们留下了一个关于C#如何调用BashShell的问题.在文章发布之后,我留意到有读者留言推荐使用“Pash”(一款类PowerSh ...
- linux中用shell获取昨天、明天或多天前的日期
linux中用shell获取昨天.明天或多天前的日期 时间 -- :: BlogJava-专家区 原文 http://www.blogjava.net/xzclog/archive/2015/12/0 ...
- linux中用shell获取时间,日期
linux中用shell获取昨天.明天或多天前的日期:在Linux中对man date -d 参数说的比较模糊,以下举例进一步说明:# -d, --date=STRING display time d ...
- Linux 的shell 字符串截取很有用。有八种方法。
一 Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.linuxidc.com/123.htm 1 # 号截取,删除左边字符,保留右边字符. echo ${va ...
- Linux Bash Shell 快速入门
BASH 的基本语法 最简单的例子 —— Hello World! 关于输入.输出和错误输出 BASH 中对变量的规定(与 C 语言的异同) BASH 中的基本流程控制语法 函数的使用 2.1 ...
- linux,shell输入反斜杠显示'W'。
linux,shell输入反斜杠显示'W'. solution: 字体必须为"Courier New".
- 06 Linux下Shell介绍
一.概述 每个人在成功登陆Linux后,系统会出现不同的提示符号,例如$,~,#等,然后你就可以开始输入需要的命令.若命令正确,系统就会依据命令的要求来执行,直到注销系统为止,在登陆到注销期间,输入的 ...
随机推荐
- 【转】Android开发在路上:少去踩坑,多走捷径
本文是我订阅"腾讯大讲堂"公众帐号时,他们推送的一篇文章,但在腾讯大讲堂官网上我并没有找到这篇文章,不过其它专门"爬"公众号文章的网站倒是有.我觉得写的很不错. ...
- [Android Security] 如何把java代码转换成smali代码
copy :https://www.cnblogs.com/gordon0918/p/5466514.html 1.概述 Smali是Android系统中Dalvik虚拟机指令语言,在apk逆向过程中 ...
- 可重入读写锁ReentrantReadWriteLock基本原理分析
前言 本篇适用于了解ReentrantLock或ReentrantReadWriteLock的使用,但想要进一步了解原理的读者.见于之前的分析都是借鉴大量的JDK源码,这次以流程图的形式代替源码,希望 ...
- c#利用SWIG调用c++dll学习总结【转】
开发环境: 操作系统:windows 7 IDE:Microsoft Visual Studio Professional 2015 SWIG: 3.0.12 swig的介绍 详细介绍可看官网,一下贴 ...
- Asp.net WebAPi gzip压缩和json格式化
现在webapi越来越流行了,很多时候它都用来做接口返回json格式的数据,webapi原本是根据客户端的类型动态序列化为json和xml的,但实际很多时候我们都是序列化为json的,所以webapi ...
- CentOS中zip压缩和unzip解压缩命令详解
以下命令均在/home目录下操作cd /home #进入/home目录1.把/home目录下面的mydata目录压缩为mydata.zipzip -r mydata.zip mydata #压缩myd ...
- MFC中位图的显示
分析: 首先,我们要明确一点,窗口的绘制包括两个步骤,首先:擦除窗口背景,然后再对窗口重新进行绘制:当擦除窗口背景时,程序会发生一个WM_ERASEBKGND消息,因此可以在此响应函数中完成位图的显示 ...
- Python3 简单验证码识别思路及实例
1.介绍 在爬虫中经常会遇到验证码识别的问题,现在的验证码大多分计算验证码.滑块验证码.识图验证码.语音验证码等四种.本文就是识图验证码,识别的是简单的验证码,要想让识别率更高, 识别的更加准确就需要 ...
- 【Scala】Scala-None-null引发的血案
Scala-None-null引发的血案 Overview - Spark 2.2.0 Documentation Spark Streaming - Spark 2.2.0 Documentatio ...
- Centos6.4下安装protobuf-c问题及解决办法
1.前言 protobuf是Google提供的结构持久化工具,类型XML,但要比XML更加灵活,效率要高.protobuf当初支持C++.JAVA和Python,后来有了支持C语言的Protobuf- ...