shell中實現保證同一時間內只有一個進程在實現: 1.lockf命令 2.簡單實現:i=`ps -ef|grep [n]ew_mvbackup|wc -l`if [ $i -gt 1 ];thenecho "wait for the other mvbackup finish!"exit -1else#do something here 3.另外一種實現:lock_on(){ local f=$1 local freefd=6 ## do not use fd 5 ##
為了刪除日誌,需要不斷切換使用者去指定目錄刪除,所以想能否指令碼自動實現。直接su發現不行,所以採用telnet到原生方式。 #!/usr/bin/kshINFILE="file.in"OUTFILE="file.out"rm $INFILErm $OUTFILEmknod $INFILE ptouch $OUTFILE# file description 7 for out and 8 for inexec 7<>$OUTFILE
example about linux shell #!/bin/bashecho "Hello world!"echo "Hello $LOGNAME,It's nice talking to you"echo "U present working on a machine is `uname -n`"echo "U present working directory is $(pwd)"echo "Bye for now $LOGNAME.The time is $(date +%T)"#=
1. if 語句"if"運算式如果條件為真,則執行then後的部分:Toggle line numbers 1 if ....; then 2 .... 3 elif ....; then 4 .... 5 else 6 .... 7 fi大多數情況下,可以使用測試命令來對條件進行測試,比如可以比較字串、判斷檔案是否存在及是否可讀等等……通常用" [ ]
1、find命令選項f i n d命令有很多選項或運算式,每一個選項前面跟隨一個橫杠-。 -name 按照檔案名稱尋找檔案。 -perm 按照檔案許可權來尋找檔案。 -prune 使用這一選項可以使f i n d命令不在當前指定的目錄中尋找,如果同時使用了- d e p t h選項,那麼- p r u n e選項將被f i n d命令忽略。 -user 按照檔案屬主來尋找檔案。 -group 按照檔案所屬的組來尋找檔案。 -mtime -n +n
1、使用&& :& &左邊的命令(命令1)返回真(即返回0,成功被執行)後,& &右邊的命令(命令2)才能夠被執行;2、使用|| :如果| |左邊的命令(命令1)未執行成功,那麼就執行| |右邊的命令(命令2);3、用()和{ }將命令結合在一起 : 為了在當前s h e l l中執行一組命令,可以用命令分隔字元隔開每一個命令,並把所有的命令用圓括弧()括起來。如果使用{ }來代替(),那麼相應的命令將在子s h e l l而不是當前s h e
此片文章總結我常用的一些shell指令或一些簡短的指令1. 查看某個工具編譯出來的時間;ll $(which appname)2. 常用for迴圈for i in 1 2 3 4 5; do echo "$i"; done;for((i=140;i<200;i++)); do git tag -d v1.3.$i;done;3. 常用讀取檔案cat filename | while read linedo echo $linedone4. "$@" 的神奇之處for i in "$@
PS命令中的%CPU是指一個進程佔用CPU的時間百分比,那麼具體的含義是什麼呢? PS的man手冊的解釋是這樣的:cpu utilization of the process in "##.#" format. Currently, it is the CPU time used divided by the time the process has been running (cputime/realtime ratio),expressed as a percentage. It will