Linux系统下高级语言调用shell脚本
2012-04-21 17:09
513 查看
1.Java调用shell
Java语言以其跨平台性和简易性而著称,在Java里面的lang包里(java.lang.Runtime)提供了一个允许Java程序与该程序所运行的环境交互的接口,这就是Runtime类,在Runtime类里提供了获取当前运行环境的接口。其中的exec函数返回一个执行shell命令的子进程。exec函数的具体实现形式有以下几种:
public Process exec(String command) throws IOException
public Process exec(String command,String[] envp) throwsIOException
public Process exec(String command,String[] envp,File dir) throwsIOException
public Process exec(String[] cmdarray) throws IOException
public Process exec(String[] cmdarray, String[] envp) throwsIOException
public Process exec(String[] cmdarray, String[] envp,File dir)throws IOException
我们在这里主要用到的是第一个和第四个函数,具体方法很简单,就是在exec函数中传递一个代表命令的字符串。exec函数返回的是一个Process类型的类的实例。Process类主要用来控制进程,获取进程信息等作用。(具体信息及其用法请参看Java doc)。
1)执行简单的命令的方法:
代码如下:
try
{
String commands = "ls -l";
Process process = Runtime.getRuntime().exec (commands);
// for showing the info on screen
InputStreamReader ir=newInputStreamReader(process.getInputStream());
BufferedReader input = new BufferedReader (ir);
String line;
while ((line = input.readLine ()) != null){
System.out.println(line);
}
}//end try
catch (java.io.IOException e){
System.err.println ("IOException " + e.getMessage());
}
上面的代码首先是声明了一个代表命令的字符串commands,它代表了ls -l这个命令。之后我们用Runtime.getRuntime().exec(commands)来生成一个子进程来执行这个命令,如果这句话运行成功,则命令 ls -l 运行成功(由于没有让它显示,不会显示ls -l的结果)。后面的流操作则是获取进程的流信息,并把它们一行行输出到屏幕。
2)执行带有参数的命令(尤其是参数需要用引号的)时则需要用String的数组来表示整个命令,而且要用转义符把引号的特殊含义去除,例如我们要执行find / -name "*mysql*" -print 时,用如下代码
try
{
String[] commands = newString[]{"find",".","-name","*mysql*","-print"};
Process process = Runtime.getRuntime().exec (commands);
InputStreamReader ir=newInputStreamReader(process.getInputStream());
BufferedReader input = new BufferedReader (ir);
String line;
while ((line = input.readLine ()) != null){
System.out.println(line);
}
}//end try
catch (java.io.IOException e){
System.err.println ("IOException " + e.getMessage());
3)执行一个自己写的脚本
非常简单,只需要在构造commands时写出它的详细路径和文件名,及参数等。
try
{
String commands = "/root/test/checkfile.sh";
Process process = Runtime.getRuntime().exec (commands);
InputStreamReader ir=newInputStreamReader(process.getInputStream());
BufferedReader input = new BufferedReader (ir);
String line;
while ((line = input.readLine ()) != null){
System.out.println(line);
}
}//end try
catch (java.io.IOException e){
System.err.println ("IOException " + e.getMessage());
如果命令中有参数,同2)要用数组的形式。
2.C程序调用shell
C程序调用shell脚本共有三种方式:system()、popen()、exec系列函数
1)system(shell命令或shell脚本路径);
system()会调用fork()产生子进程,由子进程来调用/bin/sh-cstring来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程。在调用system()期间SIGCHLD信号会被暂时搁置,SIGINT和SIGQUIT 信号则会被忽略。
返回值:如果system()在调用/bin/sh时失败则返回127,其他失败原因返回-1。若参数string为空指针(NULL),则返回非零值。如果system()调用成功则最后会返回执行shell命令后的返回值,但是此返回值也有可能为system()调用/bin/sh失败所返回的127,因此最好能再检查errno 来确认执行成功。
system命令以其简单高效的作用得到很很广泛的应用,下面是一个例子
例:在~/test/目录下有shell脚本test.sh,内容为
#!bin/bash
#test.sh
echo hello
在同层目录下新建一个c文件system_test.c,内容为:
#include<stdlib.h>
int main()
{
system("~/test/test.sh");
}
执行结果如下:
[root@localhost test]$gcc system_test.c -osystem_test
[root@localhost test]$./system_test
hello
[root@localhost test]$
2)popen(char *command,char*type)
popen()会调用fork()产生子进程,然后从子进程中调用/bin/sh-c来执行参数command的指令。参数type可使用“r”代表读取,“w”代表写入。依照此type值,popen()会建立管道连到子进程的标准输出设备或标准输入设备,然后返回一个文件指针。随后进程便可利用此文件指针来读取子进程的输出设备或是写入到子进程的标准输入设备中。此外,所有使用文件指针(FILE*)操作的函数也都可以使用,除了fclose()以外。
返回值:若成功则返回文件指针,否则返回NULL,错误原因存于errno中。注意:在编写具SUID/SGID权限的程序时请尽量避免使用popen(),popen()会继承环境变量,通过环境变量可能会造成系统安全的问题。
例:C程序popentest.c内容如下:
#include<stdio.h>
main
{
FILE * fp;
charbuffer[80];
fp=popen(“~/myprogram/test.sh”,”r”);
fgets(buffer,sizeof(buffer),fp);
printf(“%s”,buffer);
pclose(fp);
}
执行结果如下:
[root@localhost test]$ vim popentest.c
[root@localhost test]$ gcc popentest.c -o popentest
[root@localhost test]$ ./popentest
/root/test
[root@localhost test]$
Java语言以其跨平台性和简易性而著称,在Java里面的lang包里(java.lang.Runtime)提供了一个允许Java程序与该程序所运行的环境交互的接口,这就是Runtime类,在Runtime类里提供了获取当前运行环境的接口。其中的exec函数返回一个执行shell命令的子进程。exec函数的具体实现形式有以下几种:
public Process exec(String command) throws IOException
public Process exec(String command,String[] envp) throwsIOException
public Process exec(String command,String[] envp,File dir) throwsIOException
public Process exec(String[] cmdarray) throws IOException
public Process exec(String[] cmdarray, String[] envp) throwsIOException
public Process exec(String[] cmdarray, String[] envp,File dir)throws IOException
我们在这里主要用到的是第一个和第四个函数,具体方法很简单,就是在exec函数中传递一个代表命令的字符串。exec函数返回的是一个Process类型的类的实例。Process类主要用来控制进程,获取进程信息等作用。(具体信息及其用法请参看Java doc)。
1)执行简单的命令的方法:
代码如下:
try
{
String commands = "ls -l";
Process process = Runtime.getRuntime().exec (commands);
// for showing the info on screen
InputStreamReader ir=newInputStreamReader(process.getInputStream());
BufferedReader input = new BufferedReader (ir);
String line;
while ((line = input.readLine ()) != null){
System.out.println(line);
}
}//end try
catch (java.io.IOException e){
System.err.println ("IOException " + e.getMessage());
}
上面的代码首先是声明了一个代表命令的字符串commands,它代表了ls -l这个命令。之后我们用Runtime.getRuntime().exec(commands)来生成一个子进程来执行这个命令,如果这句话运行成功,则命令 ls -l 运行成功(由于没有让它显示,不会显示ls -l的结果)。后面的流操作则是获取进程的流信息,并把它们一行行输出到屏幕。
2)执行带有参数的命令(尤其是参数需要用引号的)时则需要用String的数组来表示整个命令,而且要用转义符把引号的特殊含义去除,例如我们要执行find / -name "*mysql*" -print 时,用如下代码
try
{
String[] commands = newString[]{"find",".","-name","*mysql*","-print"};
Process process = Runtime.getRuntime().exec (commands);
InputStreamReader ir=newInputStreamReader(process.getInputStream());
BufferedReader input = new BufferedReader (ir);
String line;
while ((line = input.readLine ()) != null){
System.out.println(line);
}
}//end try
catch (java.io.IOException e){
System.err.println ("IOException " + e.getMessage());
3)执行一个自己写的脚本
非常简单,只需要在构造commands时写出它的详细路径和文件名,及参数等。
try
{
String commands = "/root/test/checkfile.sh";
Process process = Runtime.getRuntime().exec (commands);
InputStreamReader ir=newInputStreamReader(process.getInputStream());
BufferedReader input = new BufferedReader (ir);
String line;
while ((line = input.readLine ()) != null){
System.out.println(line);
}
}//end try
catch (java.io.IOException e){
System.err.println ("IOException " + e.getMessage());
如果命令中有参数,同2)要用数组的形式。
2.C程序调用shell
C程序调用shell脚本共有三种方式:system()、popen()、exec系列函数
1)system(shell命令或shell脚本路径);
system()会调用fork()产生子进程,由子进程来调用/bin/sh-cstring来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程。在调用system()期间SIGCHLD信号会被暂时搁置,SIGINT和SIGQUIT 信号则会被忽略。
返回值:如果system()在调用/bin/sh时失败则返回127,其他失败原因返回-1。若参数string为空指针(NULL),则返回非零值。如果system()调用成功则最后会返回执行shell命令后的返回值,但是此返回值也有可能为system()调用/bin/sh失败所返回的127,因此最好能再检查errno 来确认执行成功。
system命令以其简单高效的作用得到很很广泛的应用,下面是一个例子
例:在~/test/目录下有shell脚本test.sh,内容为
#!bin/bash
#test.sh
echo hello
在同层目录下新建一个c文件system_test.c,内容为:
#include<stdlib.h>
int main()
{
system("~/test/test.sh");
}
执行结果如下:
[root@localhost test]$gcc system_test.c -osystem_test
[root@localhost test]$./system_test
hello
[root@localhost test]$
2)popen(char *command,char*type)
popen()会调用fork()产生子进程,然后从子进程中调用/bin/sh-c来执行参数command的指令。参数type可使用“r”代表读取,“w”代表写入。依照此type值,popen()会建立管道连到子进程的标准输出设备或标准输入设备,然后返回一个文件指针。随后进程便可利用此文件指针来读取子进程的输出设备或是写入到子进程的标准输入设备中。此外,所有使用文件指针(FILE*)操作的函数也都可以使用,除了fclose()以外。
返回值:若成功则返回文件指针,否则返回NULL,错误原因存于errno中。注意:在编写具SUID/SGID权限的程序时请尽量避免使用popen(),popen()会继承环境变量,通过环境变量可能会造成系统安全的问题。
例:C程序popentest.c内容如下:
#include<stdio.h>
main
{
FILE * fp;
charbuffer[80];
fp=popen(“~/myprogram/test.sh”,”r”);
fgets(buffer,sizeof(buffer),fp);
printf(“%s”,buffer);
pclose(fp);
}
执行结果如下:
[root@localhost test]$ vim popentest.c
[root@localhost test]$ gcc popentest.c -o popentest
[root@localhost test]$ ./popentest
/root/test
[root@localhost test]$
相关文章推荐
- Linux系统下shell脚本语言学习手册指南
- java调用windows系统的批处理(.bat文件) 和 linux系统的shell脚本(.sh文件)
- linux系统shell中调用另一个脚本的三种不同方法(source,exec,fork)
- Linux编写shell脚本处理Catalina.out长时间大量日志占用系统空间问题
- linux下保留文件系统下剩余指定数目文件的shell脚本
- Linux下的汇编语言之系统调用
- Linux基础之-利用shell脚本实现自动监控系统服务
- 利用shell脚本来监控linux系统
- (转自张戈博客)Linux系统防CC攻击自动拉黑IP增强版Shell脚本
- *linux shell 脚本 调用数据库
- 利用shell脚本来监控linux系统的负载与CPU占用情况
- Linux系统防CC攻击自动拉黑IP增强版Shell脚本
- 使用shell脚本对Linux系统和进程资源进行监控
- Shell脚本编程——了解你的Linux系统必须掌握的20个命令
- Nodejs中调用系统命令、Shell脚本和Python脚本的方法和实例
- 利用shell脚本来监控linux系统的内存 推荐
- Shell脚本查看linux系统性能瓶颈
- Shell脚本判断Linux系统是32位还是64位的几种方法分享
- 【Linux 系统编程】shell 脚本基础学习之函数(五)
- *Linux Shell 高级编程技巧4----几个常用的shell脚本例子