Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1466961
  • 博文数量: 842
  • 博客积分: 12411
  • 博客等级: 上将
  • 技术积分: 5772
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-14 14:43
文章分类

全部博文(842)

文章存档

2013年(157)

2012年(685)

分类: LINUX

2013-03-22 15:17:25

原文地址:Shell编程(一)基础语法 作者:spyhjl


  1. #!/bin/bash
  2. #$0: bash file name with path
  3. #$?: last cmd state after execute
  4. #$*: all the param in form "param1param2pram3......"
  5. #$@: all the param in form "param1""param2""param3""......"
  6. #$#: param number
  7. #$n: n corresponding to the param position






  1. # =============== branch if example ==============
  2. #eg0: string compare in branch
  3. #-f: test file
  4. #-s: test file lenth
  5. #-d: test file dir
  6. #-z: check zero file lenth
  7. #string1 = string2:
  8. #string1 != string2:
  9. #string1 < string2:


  1. dir=./test

  2. if [ -d $dir ]
  3. then
  4.     echo "file exist"
  5. else
  6.     echo "file not exist"
  7. fi;


  1. if test -d $dir && [ -d $dir ]
  2. then
  3.     echo "yes"
  4. else
  5.     echo "no"
  6. fi;


  1. #eg1: string compare in branch
  2. #-eq:
  3. #-lt:
  4. #-le:
  5. #-gt:
  6. #-ge:


  1. tm=
  2. tm2=ndl

  1. if test -z "$tm"
  2. then
  3.     echo "yes"
  4. else
  5.     echo "no"
  6. fi;


  1. #eg3: int data compare in branch
  2. t1=2
  3. t2=2

  1. if [ $t1 -lt $t2 ]; then
  2.     echo "lt"
  3. elif [ $t1 -eq $t2 ]; then
  4. { echo "eq";echo "test 1";
  5. }
  6. else
  7.     echo "else"
  8. fi


  1. #eg4: int data compare in branch
  2. if [ $# -lt 0 ]; then
  3.     echo "$# lt zero"
  4. elif [ $# -eq 0 ]; then
  5.     echo "0"
  6. elif [ $# -eq 1 ]; then
  7.     echo $1
  8. elif [ $# -eq 2 ]; then
  9.     echo $1$2
  10. elif [ $# -eq 3 ]; then
  11.     echo $1$2$3
  12. else
  13.     echo others
  14. fi;


  1. # =============== branch case example ==============
  2. #eg1:
  3. echo "case test"
  4. case $1 in
  5.     *h | ?k) echo "0";;
  6.     *o) echo " 1 | 2";;
  7.     *) echo others;;
  8. esac;


  1. #eg2:
  2. a=`date +%H`
  3. echo $a
  4. case $a in
  5.     0[123456789] | 1[012]) echo "god morning";;
  6.     1[3456789]) echo good afternoon ;;
  7.     2[01234]) echo good evening;;
  8. esac;


  1. # =============== until example ==============
  2. until grep "gavinhuang" /etc/passwd
  3. do
  4.     echo "hello world"
  5. done;

  1. # =============== loop for example ==============
  2. p=`pwd`
  3. for dir in $p/test $p/test1
  4. do
  5.     echo $dir
  6.     cd $dir

  7.     for file in *.[c]
  8.     do
  9.         ls -l $file
  10.     done;
  11. done;




  1. # =============== read example ==============
  2. #read [param] value
  3. #param : -t timeout
  4. #param : -p prompt tittle






  1. # =============== while example ==============

  2. while read -p input: number
  3. do
  4. case $number in
  5.     [012345]?) echo no pass: $number;
  6.                 echo input a numbe\c;;
  7.     [678]?) echo good: $number;
  8.                 echo input a numbe\c;;
  9.     9? | 100) echo exe good: $number;
  10.                 echo input a numbe\c;;
  11.     *) echo input err;
  12.             echo input a numbe\c;;
  13. esac;
  14. done;



  1. # =============== select example ==============
  2. a1=Linux
  3. a2="Mac Os"
  4. a3=Windows
  5. select var in "$a1" "$a2" "$a3"
  6. do
  7.     case $var in
  8.         "$a1") echo choose option 1: $var;;
  9.         "$a2") echo choose option 2: $var;;
  10.         "$a3") echo choose option 3: $var;;
  11.     esac;
  12. break;
  13. done;


阅读(538) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~