2011
Nov
21

基礎

  • wc -l :計算行數
Example
  1. [user]conf$ cat php.ini | wc -l
  2. 1299
  • 比較數字大小 -lt , -gt
Example
  1. a=1
  2. b=2
  3. c=3
  4.  
  5. if [ "$a" -lt "$b" ]; then
  6. echo "$a is smaller then $b";
  7. fi
  8.  
  9. if [ "$c" -gt "$b" ]; then
  10. echo "$c is bigger then $b";
  11. fi
  • for 迴圈
Example
  1. for i in 1 2 3 4 5 6 7 8 9 10
  2. do
  3. echo -n "...$i"
  4. done
  • 字串擷取,試著擷取第2到第5的文字
Example
  1. data="a apple a day keeps the doctor away"
  2. echo "${data:2:5}"
  3. echo "${data:10:3}"
  • 印出所有的檔案
Example
  1. r_file(){
  2. dir=$1
  3. files=$dir/*
  4. for filename in $files
  5. do
  6. if [ -d $filename ]; then
  7. echo $filename
  8. r_file "$filename"
  9. elif [ -f $filename ]; then
  10. echo $filename
  11. else
  12. #echo "not file" $filename
  13. echo ""
  14. fi
  15. done
  16. }
  17. r_file /home/

tr

tr (translate): 字母轉換

用 16 進位的方式存檔

hex
  1. echo -e '\x61\x62' > tmp

回應 (Leave a comment)