Perl调用shell命令方法小结_基础教程

一、system
perl也可以用system调用shell的命令,它和awk的system一样,返回值也是它调用的命令的退出状态.

复制代码 代码如下:

[root@AX3sp2 ~]# cat aa.pl
#! /usr/bin/perl -w
$file = "wt.pl";
system("ls -l wt.pl");
$result = system "ls -l $file";
print "$result \n"; #输出命令的退出状态
system "date";

[root@AX3sp2 ~]# perl aa.pl
-rwxr-xr-x 1 root root 126 12-16 15:12 wt.pl
-rwxr-xr-x 1 root root 126 12-16 15:12 wt.pl
0
2010年 12月 16日 星期四 15:58:34 CST     

二、反引号
perl的system函数和awk的一样不能够返回命令的输出.
要得到命令的输出,就得使用和shell本身一样的命令:     ` `

复制代码 代码如下:

[root@AX3sp2 ~]# cat bb.pl
#! /usr/bin/perl
print `date`;
print "this is test \n";

[root@AX3sp2 ~]# perl bb.pl
2010年 12月 16日 星期四 15:51:59 CST
this is test

三、exec
最后,perl还可以使用exec来调用shell的命令. exec和system差不多,不同之处在于,调用exec之后,perl马上就退出,而不会去继续执行剩下的代码

复制代码 代码如下:

[root@AX3sp2 ~]# cat cc.pl
#! /usr/bin/perl
exec ("echo this is test");
print "good bye !\n";  #这句话不会被输出

[root@AX3sp2 ~]# perl cc.pl
this is test

时间: 2024-08-01 15:32:30

Perl调用shell命令方法小结_基础教程的相关文章

python 调用 shell 命令方法

python调用shell命令方法 1.os.system(cmd) 缺点:不能获取返回值 2.os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等  例:a=os.popen(cmd).read() 3.commands 模块,其实也是对popen的封装. 此模块主要有如下方法:commands.getstatusoutput(cmd) 返回(status, output).commands.getoutput(cmd) 只返回输出结果comma

Perl内置特殊变量总结_基础教程

[ 文件句柄 ] $| 如果非零, 则在对当前选定的文件执行写或打印操作后强制清除缓冲区$% 当前选中文件句柄的当前页码$= 当前选中文件句柄的当前页面长度$- 当前选中文件句柄的页面剩余长度$~ 当前选中文件句柄的报表格式名称$^ 当前选中文件句柄的当前页眉格式名 [ 全局变量 ] $_ 默认的输入和模式搜索空间$@ 上一个 eval.do 或 require 函数产生的错误消息$/ 输入记录的分隔符, 默认为换行符(类似于 awk 中的 RS)$\ 打印函数的输入记录分隔符. 除非设置了 $

JavaScript中循环遍历Array与Map的方法小结_基础知识

js循环数组各种方法eg1: for (var i = 0; i < myStringArray.length; i++) { alert(myStringArray[i]); //Do something } eg2: Array.prototype.foo = "foo!"; var array = ['a', 'b', 'c']; for (var i in array) { alert(array[i]); } for(var i in this.$GLOBAL_DETA

向JavaScript的数组中添加元素的方法小结_基础知识

在数组的开头添加新元素 - unshift()源代码: <!DOCTYPE html> <html> <body> <p id="demo">Click the button to add elements to the array.</p> <button onclick="myFunction()">Try it</button> <script> function

JS的Document属性和方法小结_基础知识

document.title //设置文档标题等价于HTML的title标签 document.bgColor //设置页面背景色 document.fgColor //设置前景色(文本颜色) document.linkColor //未点击过的链接颜色 document.alinkColor //激活链接(焦点在此链接上)的颜色 document.vlinkColor //已点击过的链接颜色 document.URL //设置URL属性从而在同一窗口打开另一网页 document.fileCr

sql2005 sa执行命令方法总结_安全教程

一.xp_cmdshell EXEC master..xp_cmdshell 'ipconfig' 开启xp_cmdshell: -- To allow advanced options to be changed. EXEC sp_configure 'show advanced options', 1 GO -- To update the currently configured value for advanced options. RECONFIGURE GO -- To enable

html 的 ContentType 小结_基础教程

ContentTypes : "ez","application/andrew-inset" ContentTypes : "hqx","application/mac-binhex40" ContentTypes : "cpt","application/mac-compactpro" ContentTypes : "doc","application/m

Java调用Shell命令的方法_java

本文实例讲述了Java调用Shell命令的方法.分享给大家供大家参考.具体如下: 近日项目中有这样一个需求:系统中的外币资金调度完成以后,要将调度信息生成一个Txt文件,然后将这个Txt文件发送到另外一个系统(Kondor)中.生成文件自然使用OutputStreamWirter了,发送文件有两种方式,一种是用写个一个类似于FTP功能的程序,另外一种就是使用Java来调用Shell,在Shell中完成文件的发送操作.我们选择后一种,即当完成外币资金的调度工作后,用Java的OutputStrea

php调用shell的方法_php实例

本文实例讲述了php调用shell的方法,分享给大家供大家参考.具体方法如下: 一.配置 查看php.ini中配置是否打开安全模式,主要是以下三个地方 safe_mode =  (这个如果为off下面两个就不用管了) disable_functions = safe_mode_exec_dir= 二.使用 由于PHP基本是用于WEB程序开发的,所以安全性成了人们考虑的一个重要方面.于是PHP的设计者们给PHP加了一个门:安全模式.如果运行在安全模式下,那么PHP脚本中将受到如下四个方面的限制: