[bingo]Linux Shell復習筆記
1. 文件權限設置中,文件本身權限受文件所在目錄權限的影響。
2. find
a. find . -name "[A-B]*.log" -exec ls -l {} \;
文件名使用模式[A-B],*等的時候,需要用引號引起來
-exec對find匹配的文件執行后面的shell命令。后面固定格式{} \;
b. find /tmp -mtime -3 -print
-mtime -3 匹配3天以內的文件
c. find ~ -newer tmpfile -print
顯示比tmpfile新的文件,可以用touch -t設置一個指定時間的臨時文件
! -newer filename 比filename文件老的文件
d. find . -size +1000000c -print
顯示大于1M的文件
e. -exec "command line" {} \;
對find匹配的文件執行后面的shell命令。后面固定格式{} \;
-ok "command line" {} \;
對每個要運行SHELL的文件,進行確認。-ok rm {} \;
find . -name "*.c" -print |xargs grep "stdio.h"
xargs用管道來處理find匹配的文件,在某些系統中對-exec的參數長度有限制時就用xargs
3. crontab
-e 編輯
-l 列表
-r 刪除
0,30 18-23 * * * * echo "hello">/dev/tty1
每天18點到23點,每半小時顯示hello.
分 小時 日 月 周 命令
4. tee
輸出到標準輸出的同時,輸出到文件中,-a追加
5. 0標準輸入 1標準輸出 2錯誤輸出
./a.out 2>&1 | tee -a tmp
2>&1 將錯誤輸出輸出到標準輸出上
./a.out > filename 創建文件
command <&m 把文件描述符m作為標準輸入
command <&- 關閉標準輸入
6. 順序執行命令
命令1 && 命令2 當命令1成功執行時就執行命令2
命令1 || 命令2 當命令1執行失敗時就執行命令2
cp /home/bingocn/tmp /tmp || echo "copy error"
7. ` 反引號,將輸出作為輸入
echo `date` 將顯示出現在的系統時間
8. $# 參數個數
第一個參數
$$ 當前Shell的進程號
$? 顯示最后命令的退出狀態,0成功
9. 條件測試
a. 測試文件狀態 [ condition ] 或者 test condition
test -x tmpfile; echo $? 測試tmpfile是否是可執行文件
-d 檢測目錄
-f 普通文件
-s 非空
b. [ -r tmpfile1 -a -w tmpfile2 ] ; echo $? 測試文件一只讀,并且文件二可寫
-a 與
-o 或
! 非
c. 字符串測試 [ str1 string_operator str2 ] 或者 test "str1" string_operator "str2"
[ "$var" = "tmp" ] ; echo $? 測試變量var是否為tmp
= 相等
!= 不相等
-z 空串
10. expr 對整數、字符串進行操作
expr 2+3 輸出2+3
expr 2 + 3 輸出5
loop=0;loop=`expr $loop + 1`;echo "$loop"