Chinaunix首页 | 论坛 | 博客
  • 博客访问: 173162
  • 博文数量: 43
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 451
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-28 09:10
文章分类
文章存档

2016年(43)

我的朋友

分类: LINUX

2016-05-25 17:45:51


点击(此处)折叠或打开

  1. #!/bin/bash

  2. echo "let us begin our shell travel"

  3. echo "would you like to join us ? y/n"
  4. #read ynstring
  5. #变量赋值,注意=左右两边不需要留空格
  6. ynstring="y"
  7. #判断字符串是否为空,注意[]里前后要有空格
  8. if [ -z "$ynstring" ]
  9. then
  10.     echo "please input your choice"
  11.     exit 1
  12. fi

  13. echo $PWD $PATH
  14. #判断目录或者文件是否存在
  15. if [ -e /home/hbx/myshell.sh ]
  16. then
  17.     echo "the file exit"
  18. elif [ -e /home/hbx/ ]
  19. then
  20.     echo "the directory exit"
  21. else
  22.     echo "the directory/file exit does not exit"
  23. fi

  24. #变量赋值整数
  25. let "num=40"
  26. echo "num=$num"

  27. #case语句使用
  28. case "$num" in
  29. 1)
  30.     echo "it is 1";;
  31. 2)
  32.     echo "it ist 2";;
  33. 40)
  34.     echo "it is 40";;
  35. *)
  36.     echo "it is wrong number";;
  37. esac    

  38. #for循环语句的使用
  39. for num in 1 2 3 4
  40. do
  41.     echo "num=$num"
  42. done
  43. #另一种for循环,注意这里是双括号
  44. for((i=0;i<5;i++))
  45. do
  46.     echo "num=$i"
  47. done

  48. #while循环使用
  49. i=0
  50. while (( i < 5 ))
  51. do
  52.     let "i=i+1"
  53.     echo "i=$i"
  54.     #注意if和 []之间需要空格,否则提示“syntax error”的错误
  55.     if [ "$i" -gt 4 ]
  56.     then
  57.         echo "break"
  58.         break
  59.     fi
  60. done

  61. #统计文件行数
  62. maxline=$(wc -l < myshell.sh)
  63. echo $maxline

  64. #清空文件的方法
  65. :>abc

  66. #大括号{}括起来的是子shell,子shell中的变量,父shell是不可见的
  67. { #进入子shell
  68.     chlidstring="abc"
  69.     #echo $chlidstring
  70. }
  71. #奇怪,测试结果是子shell中的变量,父shell中是可见的,这和理论不一致呀
  72. echo $chlidstring
  73. if [ -z "$chlidstring" ]
  74. then
  75.     echo "childstring is null"
  76. fi

  77. #trap 命令用于捕捉信号
  78. #trap "echo "you hit ctrl+c!"" INT

  79. #函数的使用
  80. Isdirectory()
  81. {
  82.     #在函数内部可以使用local定义局部变量,局部变量只在函数内部可见
  83.     local text="it is a local varial"
  84.     #函数可以传入参数,$1表示第一个参数
  85.     if [ -d $1 ]
  86.     then
  87.         echo "$1 it is a directory"
  88.     else
  89.         echo "$1 it is not a directory"
  90.     fi
  91. }
  92. #在脚本中使用函数
  93. Isdirectory /home    
  94. Isdirectory /home/hbx/shell/myshell.sh


  95. #数组的使用
  96. city[0]="beijing"
  97. city[1]="shenzhen"
  98. #注意要使用大括号括起来
  99. echo ${city[0]}
  100. #数组初始化也可以这样
  101. cityone=("shanghai" "nanjing")
  102. echo ${cityone[0]}


  103. #shell常用调试手段
  104. #1.trap
  105. #在每一条命令执行之前打印行号,trap之后的命令才会打印
  106. #trap 'echo "DEBUG line:$LINENO" ' DEBUG
  107. #echo "trap begin"

  108. #捕捉命令执行出错信号,但测试发现并没有捕捉到ERR信号,不知道为什么
  109. #ipconfig
  110. #trap 'echo "open wrong directory ,line:$LINENO" ' ERR
  111. #捕捉退出信号
  112. trap 'echo "EXIT ,line:$LINENO" ' EXIT

  113. #2.tee tee命令主要用于调试管道,通常情况下,管道的输出不会打印到终端,增加调试难道,但是用tee可以把管道输出保存到文件
  114. #比如 cat ./myshell.sh|grep "city" 可以使用如下方式
  115. cat ./myshell.sh|tee -a >debug.txt|grep "city"|tee -a >debug.txt

  116. #3.调试钩子,这个有点类似C语言中的printf调试函数,置变量1,开启打印,置变量0,关闭打印

  117. #4.shell选项
  118. #4.1 在shell脚本里 加上 set -n 后,执行这个脚本只进行语法检查,不执行命令,或者也可以如下这样
  119. sh -n myshell.sh
  120. #4.1 在shell脚本里 加上 set -x 后,执行脚本的时候打印每一个执行命令

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