c程序shell脚本获取返回值的方法
2017-05-08 22:44
435 查看
shell脚本
文件名:getpath#!/bin/bash curpath=$PWD temp1=$(mount) temp2=${temp1%%$curpath*} temp3=${temp2%*on*} mountpath=${temp3:0-9:9} echo $mountpath exit 01
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
pwd命令获取的结果: /media/xx/usb
mount命令获取结果:……/dev/sdb1 on /media/xx/usb ……
这里为什么pwd命令和mount命令的调用方式不同,本人暂时也不太清楚。
解析:
curpath=$PWD代表执行系统命令pwd,获取当前路径,存储与变量curpath中。 curpath=/media/xx/usb
temp1=$(mount)代表执行系统命令mount,获取当前挂载情况,存储与变量temp1中。 temp1=……/dev/sdb1 on /media/xx/usb ……
temp2=${temp1%%$curpath*}代表从mount获取的结果中temp1,从末尾删除包含curpath最长字符。temp2=……/dev/sdb1 on
temp3=${temp2%*on*}代表从上一步删除的结果中,从右侧删除第一个包含“on”字样的内容。temp3=……/dev/sdb1
mountpath=${temp3:0-9:9}代表从上一步结果temp3中,裁剪由保留右侧的9个字符。 所以mountpath=/dev/sdb1
最后输出mountpath。
C程序:
文件名:test.c#include <stdio.h> void main() { FILE *fp; char buffer[80]; fp = popen("./getpath","r"); fgets(buffer,sizeof(buffer),fp); printf("\n%s\n",buffer); pclose(fp); }1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
解析:popen一般应用于进程管道,这里的应用场合也解释的通,函数原型声明
FILE *popen(const char *command, const char *open_mode)
这里
fp = popen("./getpath","r");以读方式打开了shell脚本getpath,即mountpath。
通过
fgets(buffer,sizeof(buffer),fp);获取了getpath运行结果,即mountpath。
然后通过printf打印出来。
今天无意中看到这些,摸棱两可的。似懂非懂。先分享这,再说。
相关文章推荐
- c程序调用shell脚本获取返回值方法
- 【转载】linux c程序中获取shell脚本输出的实现方法
- linux c程序中获取shell脚本输出的实现方法
- linux c程序中获取shell脚本输出的实现方法
- linux c程序中获取shell脚本输出的实现方法
- linux c程序中获取shell脚本输出的实现方法
- linux c程序中获取shell脚本输出
- linux C程序中获取shell脚本输出
- linux c程序中获取shell脚本输出
- linux c程序中获取shell脚本输出
- 在shell脚本中调用另一个脚本或程序的不同方法(fork, exec, source)
- linux c程序中获取shell脚本输出
- linuxC程序中获取shell脚本输出
- linux C 程序中获取shell脚本输出
- C程序调用shell脚本共有三种方法
- linuxC程序中获取shell脚本输出
- Java后台执行Shell脚本, Java方法不用等待Shell程序执行完成并保证关掉Java程序Shell脚本正常运行
- linux C程序中获取shell脚本输出(如获取system命令输出)
- C程序调用shell脚本共有三种方法
- C程序调用shell脚本共有三种方法