SHELL学习

1. shell概述

shell概述

shell是一个命令行解释器,它接收应用程序/用户指令,然后调用操作系统内核。

shell还是一个功能相当强大的编程语言、易编写、易调试、灵活性强。

  1. Linux提供的shell解释器有

    [luomuchen@hadoop100 ~]$ cat /etc/shells
    /bin/sh
    /bin/bash
    /usr/bin/sh
    /usr/bin/bash
    #由于安装的是最简单版本的centos,因此缺少了
    #/bin/tcsh
    #/bin/csh
    
  2. bash 和 sh 的关系

    [luomuchen@hadoop100 bin]$ ll | grep bash
    -rwxr-xr-x. 1 root root    964536 4月   1 2020 bash
    lrwxrwxrwx. 1 root root         4 6月  22 23:07 sh -> bash
    
  3. Centos默认的解释器是bash

    [luomuchen@hadoop100 bin]$ echo $SHELL
    /bin/bash
    

2. shell脚本入门

  1. 脚本格式

    脚本以#!/bin/bash 开头(指定解析器)

  2. 第一个shell脚本,helloworld.sh

    1. 需求:创建一个 Shell 脚本,输出 helloworld。

    2. shell脚本

      #!/bin/bash
      echo "hello world!";
      
    3. 执行方式

      1. 采用 bash 或 sh+脚本的相对路径或绝对路径(不用赋予脚本+x 权限)

        #sh+相对路径
        [luomuchen@hadoop100 shell]$ sh ./hello.sh
        hello world!
        #sh+绝对路径
        [luomuchen@hadoop100 shell]$ sh /home/luomuchen/shell/hello.sh 
        hello world!
        
      2. 采用输入脚本的绝对路径或相对路径执行脚本(必须具有可执行权限+x)

        1. 首先要赋予 helloworld.sh

          chmod +x helloworld.sh
          
        2. 执行脚本

          #相对路径
          [luomuchen@hadoop100 shell]$ ./hello.sh 
          hello world!
          #绝对路径
          [luomuchen@hadoop100 shell]$ /home/luomuchen/shell/hello.sh 
          hello world!
          
        • 注意:第一种执行方法,本质是 bash 解析器帮你执行脚本,所以脚本本身不需要执行 权限。第二种执行方法,本质是脚本需要自己执行,所以需要执行权限。

3. 变量

3.1 系统预定义变量

  1. 常用系统变量

    $HOME、$PWD、$SHELL、$USER 等

  2. 案例操作

    1. 查看系统变量的值

      [luomuchen@hadoop100 shell]$ echo $HOME
      /home/luomuchen
      
    2. 显示当前 Shell 中所有变量:set

      JAVA_HOME=/opt/module/jdk1.8.0_333
      KAFKA_HOME=/opt/module/kafka_2.12-3.2.0
      #太多了,只截取一部分
      

3.2 自定义变量

  1. 基本语法
    1. 定义变量:变量名=变量值,注意,=号前后不能有空格
    2. 撤销变量:unset 变量名
    3. 声明静态变量:readonly 变量,注意:不能 unset
  2. 变量定义规则
    1. 变量名称可以由字母、数字和下划线组成,但是不能以数字开头,环境变量名建议大写。
    2. 等号两侧不能有空格
    3. 在 bash 中,变量默认类型都是字符串类型,无法直接进行数值运算。
    4. 变量的值如果有空格,需要使用双引号或单引号括起来

3.3 特殊变量

3.3.1 $n
  1. 基本语法

    $n (功能描述:n 为数字,$0 代表该脚本名称,$1-$9 代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如${10})

  2. 示例

    #!/bin/bash
    echo '=========$n============='
    echo "script name:$0"
    echo script name:$0
    echo paramater:$1
    echo patamater:$2
    echo $#
    
    [luomuchen@hadoop100 shell]$ ./parameter.sh sss aaa
    =========$n=============
    script name:./parameter.sh
    script name:./parameter.sh
    paramater:sss
    patamater:aaa
    2
    
3.3.2 $#
  1. 基本语法

    $# (功能描述:获取所有输入参数个数,常用于循环,判断参数的个数是否正确以及加强脚本的健壮性)。

  2. 示例,同上

3.3.3 $*、$@
  1. 基本语法

    $* (功能描述:这个变量代表命令行中所有的参数,$*把所有的参数看成一个整体)

    $@ (功能描述:这个变量也代表命令行中所有的参数,不过$@把每个参数区分对待)

  2. 示例

    #脚本
    #!/bin/bash
    echo '=====n======'
    echo $0
    echo $1
    echo $2
    echo '======$#======='
    echo $#
    echo '=======$@==========='
    echo $@
    echo '=======$*====='
    echo $*
    
    [luomuchen@hadoop100 shell]$ ./paramerter2.sh a b c d e f g
    =====n======
    ./paramerter2.sh
    a
    b
    ======$#=======
    7
    =======$@===========
    a b c d e f g
    =======$*=====
    a b c d e f g
    
3.3.4 $?
  1. 基本语法

    $?(功能描述:最后一次执行的命令的返回状态。如果这个变量的值为 0,证明上一个命令正确执行;如果这个变量的值为非 0(具体是哪个数,由命令自己来决定),则证明上一个命令执行不正确了。)

  2. 案例实操

    判断 helloworld.sh 脚本是否正确执行

    [luomuchen@hadoop100 shell]$ ./hello.sh 
    hello world!
    [luomuchen@hadoop100 shell]$ echo $?
    0
    

4. 运算符

  1. 基本语法

    “$((运算式))” 或 “$[运算式]”

  2. 示例

    #计算(2+3)* 4 的值
    [luomuchen@hadoop100 shell]$ s=$[(2+3)*4]
    [luomuchen@hadoop100 shell]$ echo $s
    20
    [luomuchen@hadoop100 shell]$ s2=$(((2+3)*4))
    [luomuchen@hadoop100 shell]$ echo $s2       
    20
    

5. 条件判断

  1. 基本语法

    1. test condition

    2. [ condition ](注意 condition 前后要有空格)

      注意:条件非空即为 true,[ luomuchen ]返回 true,[ ]返回false

  2. 常用的条件判断

    1. 两个数值的比较

      -eq 等于(equal) -ne 不等于(not equal)

      -lt 小于(less than) -le 小于等于(less equal)

      -gt 大于(greater than) -ge 大于等于(greater equal)

      注:如果是字符串之间的比较 ,用等号“=”判断相等;用“!=”判断不等

    2. 按照文件权限进行判断

      -r 有读的权限(read)

      -w 有写的权限(write)

      -x 有执行的权限(execute)

    3. 按照文件类型进行判断

      -e 文件存在(existence)

      -f 文件存在并且是一个常规的文件(file)

      -d 文件存在并且是一个目录(directory)

    #多条件判断(&& 表示前一条命令执行成功时,才执行后一条命令,
    #|| 表示上一条命令执行失败后,才执行下一条命令)
    [luomuchen@hadoop100 shell]$ [ luomuchen ] && echo OK || echo notOK
    OK
    [luomuchen@hadoop100 shell]$ [  ] && echo OK || echo notOK         
    notOK
    #这里利用了&& || 的优先级,&&优先级高于||,因此两条命令可以看作
    #([ luomuchen ] && echo OK)|| (echo notOK)
    #当判断不为空的时候,会继续执行,输出ok,此时整条判断为true,不再继续执行。否则执行||后面的语句。
    

6. 流程控制(重点)

6.1 if语句

  1. 基本语法

    1. 单分支

      if [ 条件判断式 ];then
      程序
      fi
      #或者
      if [ 条件判断式 ]
      then
      程序
      fi
      
    2. 多分支

      if [ 条件判断式 ]
      then
      程序
      elif [ 条件判断式 ]
      then
      程序
      else
      程序
      fi
      

    注意

    1. [ 条件判断式 ],中括号和条件判断式之间必须有空格
    2. if 后要有空格
  2. 示例

    输入一个数字,如果是 1,则输出 this is 1,如果是 2,则输出 this is 2, 如果是其它,this is false。

    #脚本
    #!/bin/bash
    if [ $1 -eq 1 ]
    then
        echo 'this is 1'
    elif [ $1 -eq 2 ]
    then
        echo 'this is 2'
    else
        echo 'this is false'
    fi
    
    [luomuchen@hadoop100 shell]$ sh ./if.sh 1
    this is 1 
    [luomuchen@hadoop100 shell]$ sh ./if.sh 2
    this is 2
    [luomuchen@hadoop100 shell]$ sh ./if.sh 3
    this is false
    

6.2 case语句

  1. 基本语法

    case $变量名 in
    "值 1")
    如果变量的值等于值 1,则执行程序 1
    ;;
    "值 2")
    如果变量的值等于值 2,则执行程序 2
    ;;
    …省略其他分支…
    *)
    如果变量的值都不是以上的值,则执行此程序
    ;;
    esac
    

    注意事项

    1. case 行尾必须为单词“in”,每一个模式匹配必须以右括号“)”结束。
    2. 双分号“;;”表示命令序列结束,相当于 java 中的 break。
    3. 最后的“*)”表示默认模式,相当于 java 中的 default。
  2. 示例

    输入一个数字,如果是 1,则输出 this is 1,如果是 2,则输出 this is 2, 如果是其它,this is default。

    #脚本
    #!/bin/bash
    case $1 in
    "1")
        echo 'this is 1'
        ;;
    "2")
        echo 'this is 2'
        ;;
    *)
        echo 'this is default'
        ;;
    esac      
    
    [luomuchen@hadoop100 shell]$ sh ./case.sh 1
    this is 1
    [luomuchen@hadoop100 shell]$ sh ./case.sh 2
    this is 2
    [luomuchen@hadoop100 shell]$ sh ./case.sh 3
    this is default
    

6.3 for循环

  1. 基本语句

    for (( 初始值;循环控制条件;变量变化 ))
    do
    程序
    done
    #或者
    for 变量 in 值 1 值 2 值 3…
    do
    程序
    done
    
  2. 示例

    1. 1到100相加

      #脚本
      #!/bin/bash
      sum=0
      for ((i=1;i<=100;i++))
      do
          sum=$[$sum + $i]
      done
      echo $sum
      
      [luomuchen@hadoop100 shell]$ ./for1.sh 
      5050
      
    2. 输出输入的变量

      #脚本
      #!/bin/bash
      for i in $*
      do
          echo "this is $i"
      done
      
      [luomuchen@hadoop100 shell]$ sh ./for2.sh asd 123 asd wqeq
      this is asd
      this is 123
      this is asd
      this is wqeq
      
    3. 比较$*和$@区别

      不加双引号,脚本

      #!/bin/bash
      echo '========$*======='
      #如果不加双引号,两者都是以$1,$2...的形式输出
      for i in $*
      do
         echo "this is $i"
      done
      
      echo '========$@======='
      for i in $@
      do
         echo "this is $i"
      done
      

      输出结果

      [luomuchen@hadoop100 shell]$ bash ./for3.sh as dw wqe aqewq
      ========$*=======
      this is as
      this is dw
      this is wqe
      this is aqewq
      ========$@=======
      this is as
      this is dw
      this is wqe
      this is aqewq
      

      如果加上双引号"",那么,$@还是以$1,$2...的形式输出,但是$*会以一个整体的形式输出

      #!/bin/bash
      echo '========$*======='
      for i in "$*"
      do
         echo "this is $i"
      done
      
      echo '========$@======='
      for i in "$@"
      do
         echo "this is $i"
      done
      

      输出结果

      [luomuchen@hadoop100 shell]$ bash ./for4.sh asdq eqwe qweqewqasd qweq
      ========$*=======
      this is asdq eqwe qweqewqasd qweq
      ========$@=======
      this is asdq
      this is eqwe
      this is qweqewqasd
      this is qweq
      

6.4 while循环

  1. 基本语法

    #注意条件判断式的空格
    while [ 条件判断式 ]
    do 
       程序 
    done
    
  2. 示例

    while脚本

    #!/bin/bash
    sun=0;
    i=1;
    while [ $i -le 100 ]
    do
       sum=$[$sum+$i]
       i=$[$i+1]
    done
    echo $sum
    

    运行结果

    [luomuchen@hadoop100 shell]$ chmod +x while.sh
    [luomuchen@hadoop100 shell]$ ./while.sh   
    5050
    

7. read读取控制台输入

  1. 基本语法
    read (选项)(参数)

    1. 选项

      -p:指定读取值时的提示符

      -t:指定读取值时等待的时间(秒)如果-t不加,表示一直等待

    2. 参数

      变量:指定读取的变量名

  2. 示例

    提示7秒内,读取控制台输入的名称

    #!/bin/bash
    read -t 7 -p "Enter you name in 7 seconds" NN
    echo $NN
    

    输出结果

    [luomuchen@hadoop100 shell]$ ./read.sh
    Enter you name in 7 seconds qweqwewe111
    qweqwewe111
    

8. 函数

8.1 系统函数

  1. basename

    1. 基本语法

      basename [string / pathname] [suffix]

      功能描述:basename命令会删掉所有的前缀包括最后一个(‘/’)字符,然后将字符串显示出来。

      basename 可以理解为取路径里的文件名称

      选项:

      suffix为后缀,如果suffix被制定了,basename会将pathname或string中的suffix去掉。

    2. 示例
      截取路径的文件名称。

      [luomuchen@hadoop100 shell]$ basename /home/luomuchen/luoxiaoyi.txt
      luoxiaoyi.txt
      [luomuchen@hadoop100 shell]$ basename /home/luomuchen/luoxiaoyi.txt .txt
      luoxiaoyi
      [luomuchen@hadoop100 shell]$ basename /home/luomuchen/luoxiaoyi.txt .sh
      luoxiaoyi.txt
      
  2. dirname

    1. 基本语法

      dirname 文件绝对路径

      功能描述:从给定的包含绝对路径的文件名中取出文件名(非目录的部分),然后返回剩下的路径(目录的部分)

      dirname 可以理解为取文件路径的绝对路径名称

    2. 示例

      [luomuchen@hadoop100 shell]$ dirname /home/luomucheng/luoxiaoyi.txt
      /home/luomucheng
      

8.2 自定义函数

  1. 基本语法

    [ function ] funname[()] 
    { 
       Action; 
       [return int;] 
    }
    
  2. 经验技巧

    1. 必须在调用函数地方之前,先声明函数,shell 脚本是逐行运行。不会像其它语言一 样先编译。

    2. 函数返回值,只能通过$?系统变量获得,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。return 后跟数值 n(0-255)

  3. 示例

    计算两个数之和的函数,脚本

    #!/bin/bash
    function sum()
    {
       s=0
       echo "this is $1"
       echo "this is $2"
       s=$[$1+$2]
       echo "$s"
    }
    
    read -p "Please input the number1:" n1;
    read -p "Please input the number2:" n2;
    sum $n1 $n2;
    

    运行结果

    [luomuchen@hadoop100 shell]$ touch fun.sh
    [luomuchen@hadoop100 shell]$ vim fun.sh
    [luomuchen@hadoop100 shell]$ chmod +x fun.sh
    [luomuchen@hadoop100 shell]$ ./fun.sh  
    Please input the number1:1
    Please input the number2:2
    this is 1
    this is 2
    3
    
posted @ 2022-10-25 18:05  洛沐辰  阅读(56)  评论(0编辑  收藏  举报