當前位置:首頁 » 編程軟體 » 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 11:16:47 瀏覽:404
php顯示表情 發布:2025-08-21 11:00:38 瀏覽:925
pxelinux 發布:2025-08-21 11:00:31 瀏覽:123
為什麼ios和安卓要用獨創字體 發布:2025-08-21 10:59:48 瀏覽:731
臨時緩存是幹嘛的 發布:2025-08-21 10:59:34 瀏覽:489
車壓縮機 發布:2025-08-21 10:47:48 瀏覽:388
菜鳥編程教程 發布:2025-08-21 10:47:06 瀏覽:708
android啟動線程 發布:2025-08-21 10:41:48 瀏覽:409
機器人編程培訓機構加盟 發布:2025-08-21 10:37:20 瀏覽:41
寬頻賬戶密碼如何修改查詢 發布:2025-08-21 10:36:25 瀏覽:959