linux Shell學(xué)習(xí)筆記第五天
第五天:函數(shù)與任務(wù)調(diào)度
函數(shù)的優(yōu)勢
分而治之f
協(xié)同合作
方便管理
維護(hù)簡單
函數(shù)的結(jié)構(gòu)
function 函數(shù)名()
{
命令1
命令2
命令3
}
函數(shù)的參數(shù)傳遞
向函數(shù)傳遞參數(shù)就像在一般腳本中使用特殊變量$1,$2,$3…$9一樣,函數(shù)取得所傳參數(shù)后將原始參數(shù)傳回shell腳本,因此最好先在函數(shù)內(nèi)重新設(shè)置變量保存所傳的參數(shù)。這樣如果函數(shù)有一點(diǎn)錯(cuò)誤,就可以通過已經(jīng)本地化的變量名迅速加以跟蹤。
函數(shù)文件
當(dāng)你手機(jī)一些經(jīng)常使用的函數(shù)時(shí),可以將之放入函數(shù)文件中并將文件載入shell。
文件頭應(yīng)包含語句#!/bin/bash,文件名可任意選取,但最好與相關(guān)任務(wù)有某種實(shí)際聯(lián)系。
#!/bin/bash
#注釋
function1()
{
}
函數(shù)文件示例
functions.main
#!/bin/bash
#functions.main
findit()
{
if [$# -lt 1 ];then
echo “usage:findit file”
return 1
fi
find / -name $1 –peint
}
函數(shù)使用示例
. functions.main 載入函數(shù)
set 查看是否載入函數(shù)
findit 調(diào)用函數(shù)
findit functions.main 調(diào)用函數(shù)
unset findit 刪除findit函數(shù)
單次任務(wù)調(diào)度
at用于在指定時(shí)間調(diào)度一次性的任務(wù).
格式:
at [選項(xiàng)] time
-f 從文件中讀取命令或腳本
-m在作業(yè)完成后,給用戶發(fā)電子郵件
-v 顯示作業(yè)唄執(zhí)行的時(shí)間
服務(wù)啟動(dòng)與停止
service atd start
service atd stop
刪除任務(wù) atrm
單次任務(wù)調(diào)度示例
at –f mycrontest.sh 10:00pm tomorrow
at –f mycrontest.sh 2:00am Tuesday
at –f mycrontest.sh 2:00pm Feb 11
at –f mycrontest.sh 2:00pm next week
循環(huán)調(diào)度crontab
crontab可以定期運(yùn)行一些作業(yè)任務(wù)。它是一個(gè)腳本,每次linux啟動(dòng)時(shí)都會(huì)自動(dòng)啟動(dòng)該腳本。
格式:
crontab [-e [UserName]|-l [UserName]|-r [UserName]]
-e執(zhí)行文字編輯器來設(shè)定時(shí)程表
-l 列出文字編輯器來設(shè)定時(shí)程表
-r刪除目前的時(shí)程表
-v列出用戶cron作業(yè)的狀態(tài)
crontab配置
crontab可以定期運(yùn)行一些作業(yè)任務(wù)。它是一個(gè)腳本,每次linux啟動(dòng)時(shí)都會(huì)自動(dòng)啟動(dòng)該腳本。
全局配置文件 /etc/crontab
用戶配置文件 /var/spool/cron/
crontab的用戶配置
/etc/cron.allow
/etc/cron.deny
/etc/crontab
SHELL=/bin/bash
PATH=/sbin;/bin:/usr/sbin;/usr/bin
MAILTO=root
HOME=/
01 **** root run-parts /etc/cron.hourly
02 4*** root run-parts /etc/cron.daily
22 4**0 root run-parts /etc/cron.weekly
42 41** root run-parts /etc/cron.monthly
五個(gè)字段,分 時(shí) 日 月 星期
crontab應(yīng)用場景
每五分鐘測試與網(wǎng)關(guān)192.168.1.254是否連通 ping */5 * * * *
用戶alex每個(gè)周日中午12點(diǎn)備份samba的配置文件
0 12 * * 0 tar –czvf samba.tar.gz /etc/samba/smb.conf
總結(jié)回顧
函數(shù)的優(yōu)勢
協(xié)同合作
檢查方便
高級靈活
任務(wù)調(diào)度的方式
at
crontab
- linux shell數(shù)組深入學(xué)習(xí)理解
- linux shell腳本學(xué)習(xí)xargs命令使用詳解
- Linux學(xué)習(xí)之CentOS(一)----在VMware虛擬機(jī)中安裝CentOS 7(圖文教程)
- linux Shell學(xué)習(xí)筆記第一天
- linux Shell學(xué)習(xí)筆記第三天
- Linux shell腳本基礎(chǔ)學(xué)習(xí)詳細(xì)介紹(完整版)
- linux Shell學(xué)習(xí)筆記第四天
- linux Shell學(xué)習(xí)筆記最后一節(jié),溫故與知新
- linux Shell學(xué)習(xí)筆記第二天
- linux命令學(xué)習(xí)之10個(gè)網(wǎng)絡(luò)命令和監(jiān)控命令
- Linux學(xué)習(xí)資料下載(電子書籍)
- linux shell腳本基礎(chǔ)知識學(xué)習(xí)
- Linux學(xué)習(xí)基礎(chǔ)教程
- Linux命令學(xué)習(xí)總結(jié):詳解shutdown命令
- Linux命令學(xué)習(xí)總結(jié):詳解reboot命令
- Linux學(xué)習(xí)第一天——ssh登錄和軟件安裝詳解
- 個(gè)人學(xué)習(xí)Linux知識總結(jié)
- 快速自學(xué)Linux命令的4種方法
相關(guān)文章
RouterOS實(shí)現(xiàn)自動(dòng)禁止過期帳號的腳本
本篇文章主要介紹了RouterOS實(shí)現(xiàn)自動(dòng)禁止過期帳號的腳本,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2007-12-12關(guān)于學(xué)習(xí) Flex 的一點(diǎn)建議(轉(zhuǎn)載)
關(guān)于學(xué)習(xí) Flex 的一點(diǎn)建議(轉(zhuǎn)載)...2007-01-01在InstallShield中引用WINSOCK 的示例代碼
在InstallShield中引用WINSOCK 的示例代碼...2007-03-03在InstallShield中通過主機(jī)名獲取IP地址的代碼
在InstallShield中通過主機(jī)名獲取IP地址的代碼...2007-03-03