mahonglin123456 发表于 2018-8-28 09:18:10

shell脚本编程简介

  1.什么是shell
  2. 还是hello world程序
  3. shell中的变量
  3.1 系统变量
  3.2 用户定义变量
  3.2.1 用户定义变量规则
  3.2.3 shell如何使用变量
  3.2.3 全局变量 vs 局部变量
  4. shell编程中的控制结构
  4.1 条件判定
  4.1.1 简单条件判定
  4.1.2 组合判定
  4.2if - else
  4.3 for
  4.4 while
  4.5 case
  5. shell中的函数
  5.1 函数声明和定义
  5.2 函数调用
  6. shell脚本调试
  6.1 万能的echo
  6.2两个命令
  7. 参考资料及shell脚本下载
. 什么是shell
  shell扮演者操作系统内核和用户的中间人的角色,用户通过键入shell command,然后shell通过解析用户输入,然后将请求转发给操作系统的内核进行处理。
http://images.cnblogs.com/cnblogs_com/xuqiang/295076/r_codingtips.png 1. 一个系统可以存在多个shell,可以通过cat /etc/shells命令查看系统中安装的shell,不同的shell可能支持的命令语法是不相同的。
http://images.cnblogs.com/cnblogs_com/xuqiang/295076/r_codingtips.png 2. 可以通过echo $SHELL查看当前使用的shell
. 还是hello world程序
  首先使用vim编辑器(或者是linux下任意的文本编辑器)编写文件helloshell.sh(没有必要使用.sh后缀名):
  #!/bin/bash
  echo "hello shell";
  保存上面的文件,增加该文件的执行权限:
  xuqiang@ubuntu:~/shell$ sudo chmod +x ./helloshell.sh
  运行该shell程序:
  xuqiang@ubuntu:~/shell$ ./helloshell.sh
  hello shell
  通过上面的程序没有什么实际的含义,但是通过第一个shell程序了解shell程序的执行过程。
. shell中的变量
3.1 系统变量
  linnux下的shell脚本中的变量分为“系统变量”和“用户自定义变量”,可以通过set命令查看那系统变量。
  xuqiang@ubuntu:~/shell$ set
  ... 略去内容
  xuqiang@ubuntu:~/shell$ echo $HOME
  /home/xuqiang
3.2 用户定义变量
  shell中用户可以自定义变量,shell中的变量是没有数据类型的,shell将根据当前的环境自动进行转化,例如:
  msg="hello world"
  上面的语句定义变量msg,并设置初始值是为hello world。
http://images.cnblogs.com/cnblogs_com/xuqiang/295076/r_codingtips.png Tip 1. 需要注意的是定义变量时,=两边是没有空格的
  3.2.1用户定义变量规则
  变量必须是以字母开头,后跟字母或者是下划线,变量的命名是大小写敏感的,并且可以定义一个变量的值为NULL。
  xuqiang@ubuntu:~/shell$ vech=
  xuqiang@ubuntu:~/shell$ echo $vec
  3.2.2shell中如何使用变量
  如果想要得到shell变量中存储的值的话,需要在变量名前增加$符号,例如:
  xuqiang@ubuntu:~/shell$ vech="value"
  xuqiang@ubuntu:~/shell$ echo $vech # this will print the value of vech
  value
  xuqiang@ubuntu:~/shell$ echo vech # this will print the string "vech"
  vech
  3.2.3 全局变量 vs 局部变量
  默认在shell中编写的变量全部是局部变量,如果重新打开console的话,那么这些变量将全部丢失,全局的变量可以写在文件~/.bashrc文件。
. 控制结构
4.1 条件判定
  在shell中条件判断是通过test命令或者是[ ]实现, 判断条件如下:
  数学运算:
  a -eq b :a == b
  a -ne b : a != b
  a -lt b : a < b
  a -le b : ab
  a -ge b : a >= b
  string比较:
  string1 = string2
  string1 != string2
  string1 : string1 不是NULL,或者是没有定义
4.2 组合判定
  逻辑运算符:
  ! : not
  exp1 -a exp2 : a && b
  exp1 -o exp2 : exp1 || exp2
4.3 if-else结构:
http://common.cnblogs.com/images/copycode.gif
  !/bin/sh#
  # see whether arguments is positive
  #
  if [ $# -ne 1 ]
  then
  echo "$0 : you must give/supply one integers"
  exit 1
  fi
  if test $1 -gt 0
  then
  echo "$1 number is postivie"
  else
  echo "$1 number is negative"
  fi
http://common.cnblogs.com/images/copycode.gif
http://common.cnblogs.com/images/copycode.gif
  #!/bin/sh
  osch=0
  echo "1. unix(sun os)"
  echo "2. linux(red hat)"
  echo -n "select your os choice ?"
  read osch
  if [ $osch -eq 1]
  then
  echo "you pick up unix"
  else
  #
  # nested if
  if [ $osch -eq 2 ]
  then
  echo "you pick up linux"
  else
  echo "what you donot like unix/linux"
  fi
  fi
http://common.cnblogs.com/images/copycode.gif
http://common.cnblogs.com/images/copycode.gif
  #!/bin/sh
  #
  # test the if .. elif .. else
  #
  if [ $1 -gt 0 ] ; then
  echo "$1 is positive"
  elif [ $1 -lt 0 ] ; then
  echo "$1 is negative"
  elif [ $1 -eq 0 ] ; then
  echo "$1 is zero"
  else
  echo "$1 is not a number"
  fi
http://common.cnblogs.com/images/copycode.gif
4.4 for
http://common.cnblogs.com/images/copycode.gif
  !/bin/sh
  #
  # test for
  #
  for i in 1 2 3 4 5
  do
  echo "welcome $i times"
  done
http://common.cnblogs.com/images/copycode.gif
http://common.cnblogs.com/images/copycode.gif
  #!/bin/bash
  for (( i = 0; i
页: [1]
查看完整版本: shell脚本编程简介