美文网首页
linux系列之shell编程(一)

linux系列之shell编程(一)

作者: 趣学程序_shaofeer | 来源:发表于2020-07-21 08:30 被阅读0次

    linux系列之shell编程(一)

    shell概述

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

    shell解释器

    1. Linux提供的解释器有
    [shaofei@upuptop-pc ~]$ cat /etc/shells 
    /bin/sh
    /bin/bash
    /usr/bin/sh
    /usr/bin/bash
    
    
    1. bash和sh的关系
    [shaofei@upuptop-pc bin]$ ll | grep bash
    -rwxr-xr-x    1 root root    964600 Aug  8  2019 bash
    lrwxrwxrwx    1 root root         4 Oct 28  2019 sh -> bash
    
    1. Centos默认的解析器是bash
    [shaofei@upuptop-pc bin]$ echo $SHELL
    /bin/bash
    

    Shell脚本入门

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

    2. 第一个shell脚本

    [shaofei@upuptop-pc sh]$ touch helloworld.sh
    [shaofei@upuptop-pc sh]$ vim helloworld.sh 
    
    
    #!/bin/bash
    echo "helloworld"
    
    
    1. 脚本的常用执行方式

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

    [shaofei@upuptop-pc sh]$ sh helloworld.sh 
    helloworld
    
    [shaofei@upuptop-pc sh]$ bash helloworld.sh 
    helloworld
    

    (2)采用输入脚本的绝对路径或相对路径执行脚本(必须具有可执行权限+x)

    [shaofei@upuptop-pc sh]$ chmod 777 helloworld.sh 
    [shaofei@upuptop-pc sh]$ ./helloworld.sh 
    helloworld
    [shaofei@upuptop-pc sh]$ /home/shaofei/sh/helloworld.sh 
    helloworld
    

    注意:第一种执行方法,本质是bash解析器帮你执行脚本,所以脚本本身不需要执行权限。第二种执行方法,本质是脚本需要自己执行,所以需要执行权限。

    1. 多命令处理
    [shaofei@upuptop-pc sh]$ touch batch.sh
    [shaofei@upuptop-pc sh]$ vim batch.sh 
    
    #!/bin/bash
    echo 'hello'
    cd /home/shaofei/sh
    echo 'cccc' > a.txt
    
    
    

    Shell中的变量

    系统变量

    1. 常用的系统变量

    PWD,HOME,USER,SHELL等

    1. 案例
    [shaofei@upuptop-pc sh]$ echo $HOME
    /home/shaofei
    [shaofei@upuptop-pc sh]$ echo $PWD
    /home/shaofei/sh
    [shaofei@upuptop-pc sh]$ echo $USER
    shaofei
    
    显示当前Shell中所有变量:set
    
    [shaofei@upuptop-pc sh]$ set
    BASH=/bin/bash
    BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
    BASH_ALIASES=()
    BASH_ARGC=()
    BASH_ARGV=()
    BASH_CMDS=()
    BASH_LINENO=()
    BASH_SOURCE=()
    
    ………………
    
    

    自定义变量

    1. 基本语法
      a. 定义变量: 变量名=变量值
      b. 撤销变量: unset 变量名
      c. 声明静态变量: readonly 变量, 注意不能unset

    2. 定义规则
      a. 变量名可以使用字母、数字、下划线组成,但是不能以数字开头。环境变量建议全部大写
      b. <font color=red>等号前后不能有空格</font>
      c. 在bash中,变量类型默认是字符串类型,无法直接进行数值计算
      d. 变量的值如果有空格必须要用"双引号"引起来

    1. 案例
    创建变量A并赋值为5
    
    [shaofei@upuptop-pc sh]$ A=5
    [shaofei@upuptop-pc sh]$ echo $A
    5
    
    给变量A重新赋值为9
    [shaofei@upuptop-pc sh]$ A=9
    [shaofei@upuptop-pc sh]$ echo $A
    9
    
    撤销变量A
    [shaofei@upuptop-pc sh]$ unset A
    [shaofei@upuptop-pc sh]$ echo $A
    
    创建静态的变量B
    [shaofei@upuptop-pc sh]$ readonly B=2
    [shaofei@upuptop-pc sh]$ echo $B
    2
    
    静态变量不能重新赋值
    [shaofei@upuptop-pc sh]$ B=10
    -bash: B: readonly variable
    
    静态变量不能unset
    [shaofei@upuptop-pc sh]$ unset B
    -bash: unset: B: cannot unset: readonly variable
    
    在bash中,变量默认类型都是字符串类型,无法直接进行数值运算
    [shaofei@upuptop-pc sh]$ C=1+2
    [shaofei@upuptop-pc sh]$ echo $C
    1+2
    
    变量的值如果有空格,需要使用双引号或单引号括起来
    [shaofei@upuptop-pc sh]$ D=I LOVE YOU
    -bash: LOVE: command not found
    [shaofei@upuptop-pc sh]$ D="I LOVE YOU"
    [shaofei@upuptop-pc sh]$ echo $D
    I LOVE YOU
    
    可把变量提升为全局环境变量,可供其他Shell程序使用
    [shaofei@upuptop-pc sh]$  vim helloworld.sh 
    
    在helloworld.sh文件中增加echo $B
    #!/bin/bash
    
    echo "helloworld"
    echo $B
    
    没有打印$B的值
    
    [shaofei@upuptop-pc sh]$ sh helloworld.sh 
    helloworld
    
    修改B变量为全局环境变量
    [shaofei@upuptop-pc sh]$ export B
    [shaofei@upuptop-pc sh]$ sh helloworld.sh 
    helloworld
    2
    
    
    

    特殊变量:$n

    1. 基本语法

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

    1. 案例

    输出该脚本的文件名称、输入参数1和输入参数2的值

    [shaofei@upuptop-pc sh]$ touch param.sh
    [shaofei@upuptop-pc sh]$ vim param.sh 
    
    #!/bin/bash
    echo $0  $1 $2
    
    [shaofei@upuptop-pc sh]$ sh param.sh  1 2 3
    param.sh 1 2
    

    特殊变量:$#

    1. 基本语法

    $# (获取所有的参数个数,常用于循环)

    1. 案例
    [shaofei@upuptop-pc sh]$ vim param.sh 
    #!/bin/bash
    echo $#
    [shaofei@upuptop-pc sh]$ sh param.sh 1 2 3 4 5
    5
    

    特殊变量:$*$@

    1. 基本说法

    $* (功能描述:这个变量代表命令行中所有的参数,$*把所有的参数看做一个整体)
    $@(功能描述: 这个变量代表命令行中所有的参数,不过$@把每个参数区别对待)

    1. 案例
    [shaofei@upuptop-pc sh]$ vim param.sh 
    
    #!/bin/bash
    echo $@
    echo $*
    
    [shaofei@upuptop-pc sh]$ sh param.sh 1 2 3 4 5
    1 2 3 4 5
    1 2 3 4 5
    

    特殊变量:$?

    1. 基础语法

    $? (功能描述:最后一次执行的命令的返回状态。如果这个变量的值为0,证明上一个命令正确执行;如果这个变量的值为非0,则证明上一个命令执行不正确了)

    1. 示例:
    [shaofei@upuptop-pc sh]$vim param.sh 
    #!/bin/bash
    echo $?
    
    [shaofei@upuptop-pc sh]$ ./helloworld.sh 
    helloworld
    
    [shaofei@upuptop-pc sh]$ sh param.sh 
    0
    

    运算符

    1. 基础语法

    (1) $((运算式))$[运算式]
    (2) expr +,-,*,/,% 加,减,乘,除,取余

    注意:expr 运算符之间要有空格

    1. 实例
      (1)计算3+2的值
    [shaofei@upuptop-pc sh]$ expr 3 + 2
    5
    

    (2)计算3-2的值

    [shaofei@upuptop-pc sh]$ expr 3 - 2  
    1
    

    (3)计算(2+3)* 4的值

    第一种方式
    [shaofei@upuptop-pc sh]$ expr `expr 2 + 3 ` \* 4
    20
    
    第二种方式
    [shaofei@upuptop-pc sh]$ echo $(((3+2)*4))
    20
    
    第三种方式  
    [shaofei@upuptop-pc sh]$ echo $[(2+3)*4]
    20
    
    

    条件判断

    1. 基本语法
      [ condition ]注意:condition前后有空格

    2. 常用的判断条件

    (1) 两个整数之间比较

    = 字符串比较
    -lt 小于(less than)
    -le 小于等于(less equal)
    -eq 等于(equal)
    -gt 大于(greater)
    -ge 大于等于(greater equal)
    -ne 不等于(Not equal)

    (2) 按照文件权限进行比较
    -r 有读的权限(read)
    -w 有写的权限(write)
    -x 有执行的权限(execute)

    (3) 按照文件类型进行判断

    -f 文件存在并且是一个常规的文件(file)
    -e 文件存在(existence)
    -d 文件存在且是一个目录(directory)

    案例:

    1. 23 是否大于等于 22
    [shaofei@upuptop-pc ~]$ [ 22 -ge 23 ]
    [shaofei@upuptop-pc ~]$ echo $?      
    1
    [shaofei@upuptop-pc ~]$ [ 23 -ge 23 ] 
    [shaofei@upuptop-pc ~]$ echo $?      
    0
    
    1. HelloWorld.sh 是否有写的权限
    -rw-rw-r-- 1 shaofei shaofei  5 May  8 23:02 a.txt
    -rw-rw-r-- 1 shaofei shaofei 65 May  8 23:01 batch.sh
    -rwxrwxrwx 1 shaofei shaofei 38 May  8 23:36 helloworld.sh
    -rw-rw-r-- 1 shaofei shaofei 31 Dec  8 01:01 k.sh.template
    -rw-rw-r-- 1 shaofei shaofei 22 May  9 21:56 param.sh
    -rw-rw-r-- 1 shaofei shaofei 59 Dec  8 01:01 start.sh.template
    -rwxrwxrwx 1 shaofei shaofei 21 Nov 20 09:58 test1.sh
    [shaofei@upuptop-pc sh]$ [ -r helloworld.sh ]
    [shaofei@upuptop-pc sh]$ echo $?
    0
    [shaofei@upuptop-pc sh]$ [ -x batch.sh ]
    [shaofei@upuptop-pc sh]$ echo $?
    1
    
    
    1. /home/shaofei/aaa.txt 是否存在
    [shaofei@upuptop-pc sh]$ [ -e /home/shaofei/aaa.txt ]
    [shaofei@upuptop-pc sh]$ echo $?
    1
    
    1. 多条件判断(&& 表示前一条命令执行成功时,才执行后一条命令,|| 表示上一条命令执行失败后,才执行下一条命令)
    [shaofei@upuptop-pc sh]$ [ -e /home/shaofei/aaa.txt ] || echo false
    false
    [shaofei@upuptop-pc sh]$ [ -e /home/shaofei/aaa.txt ] && echo false  
    [shaofei@upuptop-pc sh]$ 
    

    流程语句(重点)

    if判断

    1. 基本语法
    
    if [ 条件判断式 ]; then
    
       程序代码
    
    fi
    
    

    或者

    
    if [ 条件判断式 ]
    
    then 
        程序代码
    
    fi
    
    

    注意:

    • [ 条件表达式 ] 中括号和条件判断式之间必须有空格
    • if后面要有空格
    • 第一种方式 then 前面要有分号
    1. 案例

    输入一个数字,如果是1 则输出 true 如果是2 则输出 false 如果是其他数字则不做任何操作

    [shaofei@upuptop-pc sh]$ vim if.sh 
    
    #!/bin/bash
    
    if [ $1 -eq 1 ]; then
            echo true
    fi
    
    if [ $1 -eq 2 ]; then
            echo false
    fi
    
    [shaofei@upuptop-pc sh]$ sh if.sh 1
    true
    [shaofei@upuptop-pc sh]$ sh if.sh 2
    false
    
    [shaofei@upuptop-pc sh]$ sh if.sh 123
    [shaofei@upuptop-pc sh]$ 
    
    

    case语句

    1. 基础语法
    
    case $变量名 in
        "value1")
            如果变量等于value1,执行程序
        ;;
       
        "value2")
            如果变量等于value2,执行程序
        ;;
       
       ……省略其他分支……
    
    esac
    

    注意

    • case行尾必须为单词 in,每一个模式匹配必须以)结束。
    • 双分号;;表示命令序列结束,相当于java中的break
    • 最后可以使用*)表示默认模式,相当于java中的break
    • 最后以esac结束
    1. 案例

    输入一个数字,如果是1 则输出 true 如果是2 则输出 false 如果是其他数字输出default

    [shaofei@upuptop-pc sh]$ vim case.sh 
    #!/bin/bash
    
    case $1 in
            1)
            echo true
    ;;
            2)
            echo false
    ;;
            *)
            echo default
    ;;
    
    esac
    
    
    
    [shaofei@upuptop-pc sh]$ sh case.sh 1
    true
    [shaofei@upuptop-pc sh]$ sh case.sh 2
    false
    [shaofei@upuptop-pc sh]$ sh case.sh 3
    default
    [shaofei@upuptop-pc sh]$ 
    
    

    for循环

    1. 基本语法
    • 第一种方式
    for  (( 初始值;循环控制条件;变量变化 ))
    
        do
            程序
        done
    
    
    
    • 第二种方式
    
    for  变量 in 变量1,变量2,变量
    
        do
            程序
        done
    
    
    1. 实例

    计算1-100的和

    
    [shaofei@upuptop-pc sh]$ vim for1.sh 
    
    #!/bin/bash
    
    sum=0
    for ((i=1;i<=100;i++))
        do
            sum=$[$sum+$i] # or sum=$(( $sum+$i ))
        done
    
    echo $sum
    
    [shaofei@upuptop-pc sh]$ sh for1.sh 
    
    
    

    打印所有的输入参数 比较* 和@

    • $*$@都不被双引号""包括的时候,没有区别,$*$@都表示传递给函数或脚本的所有参数,不被双引号""包含时,都以$1 $2 …$n的形式输出所有参数。
    
    [shaofei@upuptop-pc sh]$ vim  for2.sh
    
    #!/bin/bash
    
    echo ---------$*
    
    for i in $*
            do
                    echo $i
            done
    
    
    echo --------$#
    
    for j in $@
            do
                    echo $j
            done
    
    echo --------end
    
    [shaofei@upuptop-pc sh]$ sh for2.sh 1 2 3 4
    ---------1 2 3 4
    1
    2
    3
    4
    --------4
    1
    2
    3
    4
    --------end
    
    
    
    • 当它们被双引号""包含时,"$*"会将所有的参数作为一个整体,以"$1 $2 …$n"的形式输出所有参数;"$@"会将各个参数分开,以"$1" "$2"…"$n"的形式输出所有参数。
    
    [shaofei@upuptop-pc sh]$ vim  for3.sh
    
    #!/bin/bash
    
    echo ---------"$*"
    
    for i in "$*"
            do
                    echo $i
            done
    
    
    echo --------$#
    
    for j in "$@"
            do
                    echo $j
            done
    
    echo --------end
    
    
    [shaofei@upuptop-pc sh]$ sh for3.sh 1 2 3 4
    ---------1 2 3 4
    1 2 3 4
    --------4
    1
    2
    3
    4
    --------end
    
    

    while循环

    1. 基本语法
    while [ 条件表达式 ] 
        do
            程序
        done
        
    
    1. 案例

    计算1-100的和

    
    [shaofei@upuptop-pc sh]$ vim while.sh 
    #!/bin/bash
    
    sum=0
    i=0
    while [ $i -le 100 ]
        do
            sum=$(( $sum+$i ))
            i=$[$i+1]
        done
    
    echo  $sum
    
    [shaofei@upuptop-pc sh]$ sh while.sh  
    5050
    
    

    注意: while后面有空格.

    read(读取用户输入)

    1. 基本语法
    
    read(选项)(参数)
        选项:
            -p:指定读取值时的提示符;
            -t:指定读取值时等待的时间(秒)。
        参数
            变量:指定读取值的变量名
    
    
    1. 实例
    
    [shaofei@upuptop-pc sh]$ vim read.sh 
    
    #!/bin/bash
    
    read -p "input your name: " -t 3 NAME
    
    echo "Your Name is $NAME !"
    
    
    
    [shaofei@upuptop-pc sh]$ sh read.sh 
    input your name: shaofeer
    Your Name is shaofeer !
    

    函数

    系统函数

    • basename
    1. basename基本语法
    
    basename [string/pathname][suffix]
    
    
    (功能描述:basename命令会删掉所有的前缀包括最后一个(‘/’)字符,然后将字符串显示出来。
    选项:
        suffix为后缀,如果suffix被指定了,basename会将pathname或string中的suffix去掉。
    
    
    1. 案例实操
    [shaofei@upuptop-pc sh]$ basename /home/shaofei/123.txt 
    123.txt
    
    [shaofei@upuptop-pc sh]$ basename /home/shaofei/123.txt .txt
    123
    
    • dirname
    1. dirname基本语法
    
    dirname 文件绝对路径  
        (功能描述:从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然后返回剩下的路径(目录的部分))
    
    
    1. 案例实操

    获取a.txt文件的路径

    
    [shaofei@upuptop-pc sh]$ dirname /home/shaofei/sh/a.txt 
    /home/shaofei/sh
    
    

    自定义函数

    1. 基本语法
    
    
    [ function ] funname[()]
    {
        Action;
        [return int;]
    }
    funname
    
    
    1. 经验技巧
      -(1)必须在调用函数地方之前,先声明函数,shell脚本是逐行运行。不会像其它语言一样先编译。
      -(2)函数返回值,只能通过$?系统变量获得,可以显示加:return返回,如果不加,将以最后一条命令运行结果,作为返回值。return后跟数值n(0-255)

    3.案例实操
    (1)计算两个输入参数的和

    [shaofei@upuptop-pc sh]$ vim fun.sh 
    
    #!/bin/bash
    
    function sum(){
            sum=$[$1+$2]
            return $sum
    
    }
    
    
    sum 1 2
    
    echo $?
    
    
    [shaofei@upuptop-pc sh]$ sh fun.sh 
    3
    
    

    shell工具(重点)

    cut

    cut的工作就是“剪”,具体的说就是在文件中负责剪切数据用的。cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段输出。

    1. 基本用法
    
    cut[选项参数] filename
    
    说明:默认分隔符是制表符
    
    
    
    
    1. 参数说明

    -f 列号,提取第几列
    -d 分隔符,按照指定分隔符分割列

    1. 实例

    准备数据

    [shaofei@upuptop-pc sh]$ touch txt
    [shaofei@upuptop-pc sh]$ vim txt 
    hello world
    java andorid python shell spark nodejs vue
    
    
    1.切出第一列
    
    [shaofei@upuptop-pc sh]$ cut -d " " -f 1 txt
    hello
    java
    
    
    2.切割cut.txt第一、三列
    [shaofei@upuptop-pc sh]$ cut -d " " -f 1,3 txt
    hello
    java python
    
    3.切割cut.txt第二、三列
    
    [shaofei@upuptop-pc sh]$ cut -d " " -f 2,3 txt
    world
    andorid python
    
    4.在cut.txt文件中切割出java
    
    [shaofei@upuptop-pc sh]$ cat txt  | grep java | cut -d " " -f 1
    java
    
    
    

    sed

    sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”,接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。

    1. 基本用法
    sed [选项参数] 'command'  filename
    
    1. 选项参数说明
    参数 说明
    -e 直接在指令列模式上进行sed的动作编辑。
    1. 命令功能描述
    参数 说明
    a 新增,a的后面可以接字串,在下一行出现
    d 删除
    s 查找并替换
    1. 案例实操
      (0)数据准备
    
    [shaofei@upuptop-pc sh]$ touch sed.txt
    [shaofei@upuptop-pc sh]$ vim sed.txt 
    
    dong shen
    guan zhen
    wo wo
    lai lai
    
    le le
    
    将“shaofeer”这个单词插入到sed.txt第二行下,打印。
    
    [shaofei@upuptop-pc sh]$  sed '2a shaofeer' sed.txt 
    dong shen
    guan zhen
    shaofeer
    wo wo
    lai lai
    
    le le
    
    
    源文件并没有变化
    
    [shaofei@upuptop-pc sh]$ cat sed.txt 
    dong shen
    guan zhen
    wo wo
    lai lai
    
    le le
    
    删除sed.txt文件所有包含wo的行
    
    [shaofei@upuptop-pc sh]$ sed '/wo/d' sed.txt 
    dong shen
    guan zhen
    lai lai
    
    le le
    
    将sed.txt文件中wo替换为ni
    [shaofei@upuptop-pc sh]$ sed 's/wo/ni/g' sed.txt
    dong shen
    guan zhen
    ni ni
    lai lai
    
    le le
    
    注意:‘g’表示global,全部替换
    
    
    
    

    awk

    一个强大的文本分析工具,把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行分析处理。

    1. 基本用法
    
    awk [选项参数] 'pattern1{action1}  pattern2{action2}...' filename
    pattern:表示AWK在数据中查找的内容,就是匹配模式
    action:在找到匹配内容时所执行的一系列命令
    
    
    
    1. 选项参数说明
    选项参数 功能
    -F 指定输入文件折分隔符
    -v 赋值一个用户定义变量
    1. 案例实操
    
    数据准备
    [shaofei@upuptop-pc sh]$ sudo cp /etc/passwd ./
    (1)搜索passwd文件以root关键字开头的所有行,并输出该行的第7列。
    [shaofei@upuptop-pc sh]$ awk -F: '/^root/{print $7}' passwd 
    /bin/bash
    
    (2)搜索passwd文件以root关键字开头的所有行,并输出该行的第1列和第7列,中间以“,”号分割。
    [shaofei@upuptop-pc sh]$ awk -F: '/^root/{print $1","$7}' passwd 
    root,/bin/bash
    
    注意:只有匹配了pattern的行才会执行action
    
    
    (3)只显示/etc/passwd的第一列和第七列,以逗号分割,且在所有行前面添加列名user,shell在最后一行添加"dahaige,/bin/zuishuai"。
    [shaofei@upuptop-pc sh]$ awk -F : 'BEGIN{print "user, shell"} {print $1","$7} END{print "dahaige,/bin/zuishuai"}' passwd
    user, shell
    root,/bin/bash
    bin,/sbin/nologin
    ……
    www,/sbin/nologin
    dahaige,/bin/zuishuai
    
    注意:BEGIN 在所有数据读取行之前执行;END 在所有数据执行之后执行。
    
    (4)将passwd文件中的用户id增加数值1并输出
    [shaofei@upuptop-pc sh]$  awk -v i=1 -F: '{print $3+i}' passwd
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    
    
    1. awk的内置变量
    变量 说明
    FILENAME 文件名
    NR 已读的记录数
    NF 浏览记录的域的个数(切割后,列的个数)
    1. 案例实操
    (1)统计passwd文件名,每行的行号,每行的列数
    [shaofei@upuptop-pc sh]$  awk -F: '{print "filename:"  FILENAME ", linenumber:" NR  ",columns:" NF}' passwd 
    filename:passwd, linenumber:1,columns:7
    filename:passwd, linenumber:2,columns:7
    filename:passwd, linenumber:3,columns:7
    filename:passwd, linenumber:4,columns:7
    filename:passwd, linenumber:5,columns:7
    filename:passwd, linenumber:6,columns:7
    
    (3)查询sed.txt中空行所在的行号
    [shaofei@upuptop-pc sh]$ awk '/^$/{print NR}' sed.txt 
    5
    

    sort

    sort命令是在Linux里非常有用,它将文件进行排序,并将排序结果标准输出。

    1. 基本语法
      sort(选项)(参数)
    选项 说明
    -n 依照数值的大小排序
    -r 以相反的顺序来排序
    -t 设置排序时所用的分隔字符
    -k 指定需要排序的列

    参数:指定待排序的文件列表

    1. 案例实操
      (0)数据准备
    
    [shaofei@upuptop-pc sh]$ vim sort.txt 
    bb:40:5.4
    bd:20:4.2
    xz:50:2.3
    cls:10:3.5
    ss:30:1.6
    
    按照“:”分割后的第三列倒序排序。
    [shaofei@upuptop-pc sh]$ sort -t : -nrk 3  sort.txt
    bb:40:5.4
    bd:20:4.2
    cls:10:3.5
    xz:50:2.3
    ss:30:1.6
    
    

    面试题

    • 问题1:使用Linux命令查询file1中空行所在的行号
      答案:
    [shaofei@upuptop-pc sh]$  awk '/^$/{print NR}' sed.txt 
    5
    
    
    • 问题2:有文件chengji.txt内容如下:

    张三 40
    李四 50
    王五 60

    使用Linux命令计算第二列的和并输出

    [shaofei@upuptop-pc sh]$  cat chengji.txt | awk -F " " '{sum+=$2} END{print sum}'
    150
    
    • 问题3:Shell脚本里如何检查一个文件是否存在?如果不存在该如何处理?
    #!/bin/bash
    
    if [ -f file.txt ]; then
       echo "文件存在!"
    else
       echo "文件不存在!"
    fi
    
    
    • 问题4:用shell写一个脚本,对文本中无序的一列数字排序
    
    [shaofei@upuptop-pc sh]$ cat test.txt
    9
    8
    7
    6
    5
    4
    3
    2
    10
    1
    [shaofei@upuptop-pc sh]$ sort -n test.txt|awk '{a+=$0;print $0}END{print "SUM="a}'
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    SUM=55
    
    
    • 问题5:请用shell脚本写出查找当前文件夹(/home)下所有的文本文件内容中包含有字符”shen”的文件名称
    
    [shaofei@upuptop-pc sh]$ grep -r "shen" /home | cut -d ":" -f 1
    /home/shaofeer/datas/sed.txt
    /home/shaofeer/datas/cut.txt
    
    

    个人学习总结

    热门文章推荐

    结尾

    好记性不如烂笔头,本文为个人学习总结。

    我这里整理了很多的学习资料、编程源码、学习笔记、面试心得,如果有需要,可以通过关注“趣学程序”公众号来获取哟~

    20200703005907544_13701

    相关文章

      网友评论

          本文标题:linux系列之shell编程(一)

          本文链接:https://www.haomeiwen.com/subject/hzxicktx.html