sdchy 发表于 2018-8-19 08:45:43

linux-shell

  shell基本语法
  变量
  表达式
  判断语句
  if表达式
  文件名以一般以.sh结尾
  #!/bin/bash    #显示后面的命令以哪种shell执行
  #:表示注释
  执行
  1.chmod +x file.sh ------- ./file.sh
  2./usr/local/scripts/file.sh(也需要有执行权限)
  3.bash ./file.sh(不需要执行权限)
  shell变量
  1.临时变量:shell程序内部定义的,其使用范围仅限于定义它的程序,对其它程序不可见
  2.永久变量:是环境变量,其值不随shell脚本的执行结束而消失
  用户自定义变量:由字母或下划线打头。由字母、数字或下划线组成,并且区分大小写。变量名长度没有限制
  引用变量值时,要在变量名前加上前缀$
  变量赋值用"=",两边不能有空格
  将命令的值赋给一个变量。如:A=dateB=$(date)
  可以利用变量和其它字符组成一个新的字符串.
  set:列出所有的变量
  给变量赋值多个单词(用单引号或双引号引起来)。A="one two"
  单引号之间的内容原封不动地指定给变量
  双引号取消了空格的作用,特殊符号的含义保留。
  删除变量unset 变量名
  位置变量和特殊变量
  $n:这个脚本的第n个参数
  $0:脚本名
  $*:这个程序的所有的参数
  $#:这个程序的参数个数
  $$:这个程序的PID
  $!:执行上一个后台程序的PID
  $?:执行上一条命令的返回值
  read命令
  作用:从键盘读入数据,赋给变量
  expr命令(用于计算)
  test str1==str2
  test str1 != str2
  test str1#测试字符串是否不空
  test -n str1 #测试字符串长度不为零,则返回结果为真
  test -z str1 #测试字符串长度为零,则返回结果为真
  test int1 -eq int2
  test int1 -ge int2
  test int1 -gt int2
  test int1 -le int2
  test int1 -lt int2
  test int1 -ne int2
  说明:也可以省略test写成这样:[ int1 -lt int2 ]
  文件测试
  test -d file#测试是否为目录
  test -f file
  test -x file
  test -r file
  test -w file
  test -e file
  test -s file #测试大小是否为空
  可简单写成这样:[ -x file ]
  =========流程控制语句=========================
  if 条件; then
  command
  fi
  if 条件1; then
  command
  else
  command
  fi
  多个条件的联合
  -a或&&:仅当两个条件都成立时,结果为真
  -o或||:逻辑或。两个条件有一个成立,结果为真
  if 条件1; then
  command
  elif 条件2; then
  command
  elif 条件3; then
  command
  else
  command
  fi

页: [1]
查看完整版本: linux-shell