if [ "$x" == "valid" ]; then
echo "x has the value 'valid'"
fi
Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts
Tuesday, May 16, 2017
Monday, July 18, 2016
Linux: bashrc 的自訂函數
在Linux中可以自訂自己的函數而不需要使用sh xx.sh運行bash,做法很簡單。
然後重啓一次terminal。輸入指令︰
這樣它就會每10秒打印一次日期hello了。當然command部份可以改運行什麼都可以。
(謎之聲︰這篇不是記錄自訂函數嗎?) ((謎之回答︰隨便就好。))
- 首先回到自己目錄 cd ~
- 建立 .bashrc文件,注意前面真的是一個點,別漏了,這個是系統加載時自動運行的檔案。
- 把以下代碼寫進去做儲存吧︰
function timer(){ if [ $# -eq 0 ] then echo "Usage: $0 [display_seconds] [command]" else time=$1 time="${time:-1}" shift while (true); do date eval $@ sleep $time wait done; fi }
然後重啓一次terminal。輸入指令︰
timer 10 echo 'hello'
這樣它就會每10秒打印一次日期hello了。當然command部份可以改運行什麼都可以。
(謎之聲︰這篇不是記錄自訂函數嗎?) ((謎之回答︰隨便就好。))
Subscribe to:
Posts (Atom)