当前位置:首页 » 编程软件 » shell脚本main函数

shell脚本main函数

发布时间: 2025-08-21 08:38:27

⑴ shell 如何合并多个文件

需求描述
现有多个具有相同命名格式及内容格式的文件,要求编写shell脚本将它们合并到一个文件中。
被合并文件的命名格式为:YYYYMMDDHHMISS.r,例如:20161018030205.r;文件中包含了若干行记录,每行记录包含26个字符,其中第一个字符为标识位,第7到12个字符为时间(格式:YYMMDD),例如:000000161019002925000003N0,该记录的第一个字符0为标识位,第7到12个字符161019表示时间,即16年的10月19日;合并之后的文件的命名格式为:YYYYMMDD.txt,例如:20161018.txt。
对于合并操作,具体要求为:
1)当天只合并前一天的文件,如今天(10月20日)只合并昨天(10月19日)的文件,文件时间通过文件命名即可看出。
2)标识位为0的记录会被写到合并之后的文件中,其他记录将被过滤掉。
3)时间(即第7到12个字符的值)为前一天的记录会被写到合并之后的文件中,其他记录将被过滤掉。
shell脚本
#!/bin/bash

srcparh=/home/zhou/src
exportpath=/home/zhou/export
linenum=0

return_fail()
{
exit 1
}

function check_config_dir
{
if [ ! -d ${srcparh} ];then
echo "[error]:${srcparh} has not existed!!"
return_fail
fi

if [ ! -d ${exportpath}]; then
echo "[error]:${exportpath} has not existed!!"
return_fail
fi
}

function merge_file
{
##YESTERDAY DATE YYMMDD
YES_DATE_YY=`date -dyesterday +%y%m%d`

##YESTERDAY filename
YES_FILENAME=`date -dyesterday +%Y%m%d`.txt

ONE_DAY_AGO=`date -dyesterday +%y%m%d`

echo"YESTERDAY:${ONE_DAY_AGO}"

echo "`date+%Y-%m-%d` `date +%T`----begin to merge file"

if [ -s ${YES_FILENAME}]; then
echo "warn:yesterday file ${YES_FILENAME} has existed!! now backup it to${YES_FILENAME}_bak."
mv ${YES_FILENAME}${YES_FILENAME}_bak
fi

cd ${srcparh}

file_list_temp=`ls | grep-E "${ONE_DAY_AGO}"`
file_list_count=`ls |grep -E "${ONE_DAY_AGO}" | wc -l`

echo " "
echo "there are${file_list_count} yesterday file(s) to be merged."
echo " "

>${exportpath}/${YES_FILENAME}

for file_name in$file_list_temp
do
echo "now to merge ${file_name}"
cat ${file_name} | grep "^0" >${file_name}_filter_firstline

while read line
do
echo ""
echo "nowto deal this line: ${line}"
echo ""

start_data=+${line:6:6}+

echo"${start_data}" | grep "+${ONE_DAY_AGO}+"
if [ $? -eq 0 ]
then
echo"${line}" >> ${exportpath}/${YES_FILENAME}
linenum=$[linenum+1]
fi
done <${file_name}_filter_firstline

rm*_filter_firstline
done

if [ ${linenum} -gt 0 ]
then
echo "Totally ${linenum} lines havemerged."
fi

if [ ! -s${exportpath}/${YES_FILENAME} ]
then
echo "warn:there is no yesterday file record!!,${exportpath}/${YES_FILENAME} isblank!"
echo " ">${exportpath}/${YES_FILENAME}
fi
}

main()
{
echo " "

echo "this mergetool begins running --------------------"

check_config_dir;

merge_file;

echo"-------------end ---------------------"
}

## Execute main function
main $*576576

脚本说明
第一,在脚本的第3到5行,定义了三个变量,其中srcparh用于存放被合并的文件,exportpath用于存放合并之后的文件,linenum用于表示本次写到合并之后的文件中的记录的条数。
第二,return_fail用于在执行出现异常(如srcparh或exportpath所表示的路径不存在)时退出程序而不进行后续处理。
第三,check_config_dir函数用于检查srcparh或exportpath所表示的路径是否存在,如不存在,则不进行后续处理。
第四,merge_file函数是本脚本的核心,它的主要功能是找出srcparh下满足时间条件的文件,并按照需求要求将文件中的记录筛选出来,放到结果文件中。如果有满足条件的记录,那么脚本会显示写入到结果文件中的记录的条数。
第五,main函数是整个程序的入口(就像c语言中的main函数一样),它调用了check_config_dir和merge_file函数。
脚本执行结果
第一,当srcparh所表示的路径不存在时,执行结果如下:
> ./file_merge_tool.sh

this merge tool begins running --------------------
[error]: /home/zhou/src has not existed!!12341234

第二,当exportpath所表示的路径不存在时,执行结果如下:
> ./file_merge_tool.sh

this merge tool begins running --------------------
[error]: /home/zhou/export has not existed!!12341234

第三,当srcparh所表示的路径存在但不包含任何文件时,执行结果如下:
> ./file_merge_tool.sh

this merge tool begins running --------------------
YESTERDAY:161019
2016-10-20 16:30:06----begin to merge file

there are 0 yesterday file(s) to be merged.

warn: there is no yesterday filerecord!!,/home/zhou/export/20161019.txt is blank!
-------------end ---------------------1234567891012345678910

第四,现有四个文件20161018030205.r、20161019030254.r、20161019182531.r、20161019213456.r,每个文件的内容如下:
20161018030205.r文件:
000000161019002925000003N0
000000161019002931000003N0
300000161018002931000003N0
000000161019002926000009Y0
000000161019003150000003N0
20161019030254.r文件:
000000161019004925000003N0
000000161019006931000003N0
100000161019006971000004N0
000000161019007926000009Y0
200000161019006871000004N0
000000161019008150000003N0
20161019182531.r文件:
000000161019001925000003N0
000000161019004931000003N0
000000161018007926000009Y0
000000161019007926000009Y0
000000161019009150000003N0
000000161017007926000009Y0
600000161019007426000009Y0
20161019213456.r文件:
000000161019002925000003N0
000000161019002931000003N0
000000161019002926000009Y0
800000161019002961000003N0
000000161019003150000003N0
将它们上传到srcparh目录下,运行脚本,结果如下:
> ./file_merge_tool.sh

this merge tool begins running --------------------
YESTERDAY:161019
2016-10-20 17:08:24----begin to merge file

there are 3 yesterday file(s) to be merged.

now to merge 20161019030254.r

now to deal this line: 000000161019004925000003N0

+161019+

now to deal this line: 000000161019006931000003N0

+161019+

now to deal this line: 000000161019007926000009Y0

+161019+

now to deal this line: 000000161019008150000003N0

+161019+
now to merge 20161019182531.r

now to deal this line: 000000161019001925000003N0

+161019+

now to deal this line: 000000161019004931000003N0

+161019+

now to deal this line: 000000161018007926000009Y0

now to deal this line: 000000161019007926000009Y0

+161019+

now to deal this line: 000000161019009150000003N0

+161019+

now to deal this line: 000000161017007926000009Y0

now to merge 20161019213456.r

now to deal this line: 000000161019002925000003N0

+161019+

now to deal this line: 000000161019002931000003N0

+161019+

now to deal this line: 000000161019002926000009Y0

+161019+

now to deal this line: 000000161019003150000003N0

+161019+
Totally 12 lines have merged.
-------------end ---------------------

对照被合并的文件和结果文件,一共有4个文件,但只有3个文件(20161019030254.r、20161019182531.r、20161019213456.r)满足时间条件,这3个文件中满足过滤条件(标识位为0、时间为前一天)的记录条数为12条,和脚本执行结果一致。
大家也可对本脚本进行更多的测试。
总结
shell脚本在基于Linux的开发中有极为广泛的应用,因为它靠近底层,执行效率高、部署方便。本文中的脚本也可以作为定时任务部署到机器上,让它在每天的同一个时间里自动执行。

⑵ 一个Shell 脚本调用另外一个Shell 脚本函数

实现Shell脚本相互调用,主要通过source命令实现。
创建函数脚本:

开始,编写包含目标函数的脚本文件。例如,创建名为functions.sh的文件,内含所需函数。
调用函数:

在另一个脚本中,使用source或.命令引入functions.sh脚本,借此导入所选函数。例如,创建名为main.sh的脚本。
执行脚本:

赋予脚本执行权限,然后运行它。

这就是Shell脚本中调用另一脚本函数的基本步骤。

⑶ 如何在shell脚本中,判断一个基本命令执行是否成功

命令执行是否成功主要由 命令自己确定,如果它出错 它会返回一个非0的退出状态
这个退出状态通过 $? 内置变量获取
在shell脚本的编写过程中 逻辑运算 ( && ||)就是由退出状态决定
0 表示 true(正常) 非零表示false(异常),异常退出值可以有很多不同的值,这些值就表示了错误类型。
function exit_status(){
( exit $1 )

}
这个函数会根据参数返回错误状态
exit_status 123
echo $? #输出退出状态 123
另外解释以下 c语言和 shell 脚本的关系。

标准 c/c++语言 main函数
int main(int argc,char **argv){

}
main的返回值就是这个程序的命令行退出状态。

⑷ 如何让Android系统或Android应用执行shell脚本

android中执行shell有两种清芹方式:
直接在代码中用java提供的Runtime 这个类来执行命令,以下为完整示例代码。
public void execCommand(String command) throws IOException {
// start the ls command running
//String[] args = new String[]{"sh", "-c", command};
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command); //这句话就是shell与高级语言间的调用
//如果有参数的话可以用另外一个被重载的exec方法
//实际上这样执行时启动了一个子进程,它没有父进程的控制台
//也就看不到输出,所以需要用输出流来得到shell执行后铅正燃的输槐虚出
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
// read the ls output
String line = "";
StringBuilder sb = new StringBuilder(line);
while ((line = bufferedreader.readLine()) != null) {

⑸ 怎么让Android系统或Android应用执行shell脚本

一、Android应用启动服务执行脚本
1 如何写服务和脚本
在android源码根目录下有/device/tegatech/tegav2/init.rc文件相信大家对这个文件都不陌生(如果不明白就仔细研读下android启动流程)。如果在该脚本文件中添加诸如以下服务:
service usblp_test /data/setip/init.usblpmod.sh
oneshot
disabled
注解:每个设备下都会有自己对应的init.rc,init.设备名.rc脚本文件。oneshot disabled向我们说明了在系统启动的时候这个服务是不会自动启动的。并且该服务的目的是执行/data/setip/init.usblpmod.sh脚本。脚本的内容你可以随便写,只要符合shell语法就可以了,比如脚本可以是简单的设置eth0:
# ! /system/bin/sh //脚本的开头必须这样写。
Ifconfig eth0 172.16.100.206 netmask 255.255.0.0 up//设置ip的命令
2、如何在应用中启动服务
1)首先了解下在服务启动的流程
1. 在你的应用中让init.rc中添加的服务启动起来。
首先了解下在服务启动的流程:
在设备目录下的init.c(切记并不是system/core/init/init.rc)
Main函数的for(;;)循环中有一个handle_property_set_fd(),函数:
for (i = 0; i < fd_count; i++) {
if (ufds[i].revents == POLLIN) {
if (ufds[i].fd == get_property_set_fd())
handle_property_set_fd();
else if (ufds[i].fd == get_keychord_fd())
handle_keychord();
else if (ufds[i].fd == get_signal_fd())
handle_signal();
}
}
这个函数的实现也在system/core/init目录下,该函数中的check_control_perms(msg.value, cr.uid, cr.gid)函数就是检查该uid是否有权限启动服务(msg.value就是你服务的名字),如果应用为root或system用户则直接返回1.之后就是调用handle_control_message((char*) msg.name + 4, (char*) msg.value),该函数的参数就是去掉1.ctl.后的start和2.你服务的名字。这个函数的详细内容:
void handle_control_message(const char *msg, const char *arg)
{
if (!strcmp(msg,"start")) {
msg_start(arg);
} else if (!strcmp(msg,"stop")) {
msg_stop(arg);
} else if (!strcmp(msg,"restart")) {
msg_stop(arg);
msg_start(arg);
} else {
ERROR("unknown control msg '%s'\n", msg);
}
}
匹配start后调用msg_start.服务就这样起来了,我们的解决方案就是在检查权限的地方“下点功夫”,因为我们不确定uid,所以就让check_control_perms这个函数不要检查我们的uid,直接检查我们服务的名字,看看这个函数:
static int check_control_perms(const char *name, unsigned int uid, unsigned int gid) {
int i;
if (uid == AID_SYSTEM || uid == AID_ROOT)
return 1;
/* Search the ACL */
for (i = 0; control_perms[i].service; i++) {
if (strcmp(control_perms[i].service, name) == 0) {
if ((uid && control_perms[i].uid == uid) ||
(gid && control_perms[i].gid == gid)) {
return 1;
}
}
}
return 0;
}
这个函数里面是必须要检查uid的,我们只要在for循环上写上。
if(strcmp(“usblp_test”,name)==0) //usblp_test就是我们服务的名字。
return 1;
这样做不会破坏android原本的结构,不会有什么副作用。
init.c和init.rc都改好了,现在就可以编译源码了,编译好了装到机子开发板上就可以了。

热点内容
车压缩机 发布:2025-08-21 10:47:48 浏览:386
菜鸟编程教程 发布:2025-08-21 10:47:06 浏览:706
android启动线程 发布:2025-08-21 10:41:48 浏览:407
机器人编程培训机构加盟 发布:2025-08-21 10:37:20 浏览:39
宽带账户密码如何修改查询 发布:2025-08-21 10:36:25 浏览:957
我的世界各大服务器ip地址教程 发布:2025-08-21 10:31:20 浏览:485
android开机横屏 发布:2025-08-21 10:17:33 浏览:545
ios上传图片处理 发布:2025-08-21 10:08:55 浏览:972
安卓手机双屏如何使用 发布:2025-08-21 10:05:50 浏览:695
发卡分销源码 发布:2025-08-21 09:56:26 浏览:703