zoukankan      html  css  js  c++  java
  • Shell 学习笔记

    #表示该脚本以bash方式运行
    1. #!/bin/bash
    
    #接受输入并将值赋给变量
    2. read 变量名     
              
    #使用变量
    3. ${变量名} 
    
    #此变量只读
    4. readonly 变量名          
    
    #删除此变量, 不能删除只读变量
    5. unset 变量名
    
    #表示当前shell的pid
    6. $$
    
    #(Ess下方键) 命令替换, 将执行结果保存
    7. var=`command`
    
    #计算
    8. var=`expr 2+2`            
    
    #注释
    9. #
    10. 字符串
         a. 'string'                         #单引号的字符串会原样输出,且里面的变量无效,且单引号内不能再出现单引号,转义也不行
         b. "string"                         #双引号字的符串,可以有变量,可以有转义符
         c. greeting                         #连接字符串 
         d. ${#string}                       #获取字符中长度
         e. ${string:1}/${string1:2}         #截取字符串
         f. `expr index ${string} str`       #查找字符串 str,返回索引
    11. 数组
         a. 用 "( )" 定义, 用 "空格" 符号来分隔元素
         b. array[0]="a"                        #定义指定下标的值, 下标可以不连续, 下标没有上限
         c. ${array[0]}                          #获取指定下标的值
         d. ${array[*]}/${array[@]}           #获取数组所有元素
         e. ${#array[*]}/${#array[@]}       #获取数组长度
         d. ${#array[n]}                         #获取数组指定下标的元素的长度
    
    12. if...else
         a. if [ express ]  #1. [ 两边必须有空格 ], 等价于 if test express then command fi
         then        #2. [ $a = $b] ,判断符之间也必须有空格
          command
            if
         b. if [ express ]
         then
          command1
            else
          command2
            fi
         c. if [ express ]
            then
          command1
            elif
            then
          command2
            else
          command3
            fi
    13. case  in
      case express in  #表达式可以是变量或常量, 后面跟 in 关键字
      match1)       #匹配值后跟 )
        command1    #匹配成功后执行命令
      ;;          #跳出, 相当于 break
      macth2)
        command2
      ;;
      *)          #默认匹配值, 相当于 default
        command5
      ;;
      esac         #结束符
    14. for
        for var in exp1 exp2 exp3  #表达式用" "(空格)分隔
        do
            command
        done
    15. while
      whil [ express ]  #表达式为 true 继续执行
      do
        command
      done
    16. until                                   
      unitl [ express ]  #表达式为 false 继续执行
      do
        command
      done
    17. break / continue  #跳出 或 继续, 后面都可以跟一个整数, 表示到第几层循环
    18. 函数
      a. function func_name(){  #定义函数, function可省略
          command
        }              #只能返回整数数字, 如果没有指定返回值, 会将最后一条命令执行结果返回
      b. func_name          #调用函数, 不需要加 ( ) , 函数要在调用前定义
      c. $1 ... $9 ${10}      #取参数, 参数个数超过10后需要加 { }
    19. 输入/出重定向
      a. < 输入重定向
      b. > 输出重定向
    20. 引用其它文件
      a. . fileName
      b. source fileName
    

    本文版权归作者和博客园共有,来源网址:http://www.cnblogs.com/code-boy/欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    HDU 5280 Senior's Array (暴力,水)
    LeetCode Intersection of Two Linked Lists (找交叉点)
    LeetCode Pascal's Triangle II (杨辉三角)
    LeetCode Lowest Common Ancestor of a Binary Search Tree (LCA最近公共祖先)
    LeetCode Binary Tree Level Order Traversal (按层收集元素)
    LeetCode Balanced Binary Tree (判断平衡树)
    LeetCode Maximum Depth of Binary Tree (求树的深度)
    LeetCode Palindrome Linked List (回文链表)
    jstl表达式的应用的条件
    dao层写展示自己需要注意的问题
  • 原文地址:https://www.cnblogs.com/code-boy/p/5008151.html
Copyright © 2011-2022 走看看