Shell入门基础学习笔记

目录

第1章 Shell概述

第2章 Shell解析器

第3章 Shell脚本入门 

第4章 Shell中的变量

4.1 系统变量

 4.2 自定义变量

4.3 特殊变量:$n

4.4 特殊变量:$#

4.5 特殊变量:$*、$@

4.6 特殊变量:$?

第5章 运算符      

第6章 条件判断

第7章 流程控制(重点)

7.1 if 判断

7.2 case 语句

​编辑 7.3 for 循环

 7.4 while 循环

第8章 read读取控制台输入

第9章 函数

9.1 系统函数

9.2 自定义函数

第10章 Shell工具(重点)

10.1 cut

​编辑 10.2 sed

10.3 awk

10.4 sort


第1章 Shell概述

大数据程序员为什么要学习Shell呢?

1)需要看懂运维人员编写的Shell程序。

2)偶尔会编写一些简单Shell程序来管理集群、提高开发效率。

第2章 Shell解析器

(1)Linux提供的Shell解析器有:

[atguigu@hadoop101 ~]$ cat /etc/shells 
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh

(2)bash和sh的关系

[atguigu@hadoop101 bin]$ ll | grep bash
-rwxr-xr-x. 1 root root 941880 5月  11 2016 bash
lrwxrwxrwx. 1 root root      4 5月  27 2017 sh -> bash

(3)Centos默认的解析器是bash

[atguigu@hadoop102 bin]$ echo $SHELL
/bin/bash

第3章 Shell脚本入门 

1. 脚本格式

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

2.第一个Shell脚本:helloworld

(1)需求:创建一个Shell脚本,输出helloworld

(2)案例实操:

[atguigu@hadoop101 datas]$ touch helloworld.sh
[atguigu@hadoop101 datas]$ vi helloworld.sh
chmod
在helloworld.sh中输入如下内容
#!/bin/bash
echo "helloworld"

(3)脚本的常用执行方式 

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

       sh+脚本的相对路径

[atguigu@hadoop101 datas]$ sh helloworld.sh 
Helloworld

sh+脚本的绝对路径

[atguigu@hadoop101datas]$sh /home/atguigu/datas/helloworld.sh 
helloworld

bash+脚本的相对路径

[atguigu@hadoop101 datas]$ bash helloworld.sh 
Helloworld

bash+脚本的绝对路径

[atguigu@hadoop101datas]$bash/home/atguigu/datas/helloworld.sh 
Helloworld
  • 第二种:采用输入脚本的绝对路径或相对路径执行脚本(必须具有可执行权限+x

(a)首先要赋予helloworld.sh 脚本的+x权限

[atguigu@hadoop101 datas]$ chmod 777 helloworld.sh

(b)执行脚本

相对路径

[atguigu@hadoop101 datas]$ ./helloworld.sh 
Helloworld

绝对路径

[atguigu@hadoop101 datas]$ /home/atguigu/datas/helloworld.sh 
Helloworld

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

 3.第二个Shell脚本:多命令处理

(1)需求:

在/home/atguigucho/目录下创建一个banzhang.txt,在banzhang.txt文件中增加“I love cls”。

(2)案例实操:

[atguigu@hadoop101 datas]$ touch batch.sh
[atguigu@hadoop101 datas]$ vi batch.sh在batch.sh中输入如下内容
#!/bin/bashcd /home/atguigu
touch cls.txt
echo "I love cls" >>cls.txt

 注意:

>(单箭头)

  • > 是覆盖式的重定向运算符,它会将输出的内容写入到指定的文件中。如果文件已经存在,那么该文件的内容将被完全覆盖。

>>(双箭头)

  • >> 是追加式的重定向运算符,它会将输出内容追加到文件的末尾,而不会覆盖原有内容。如果文件不存在,则创建该文件。

第4章 Shell中的变量

4.1 系统变量

1. 常用系统变量

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

2.案例实操

(1)查看系统变量的值

[atguigu@hadoop101 datas]$ echo $HOME
/home/atguigu

(2)显示当前Shell中所有变量:set 

[atguigu@hadoop101 datas]$ set
BASH=/bin/bash
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()

 4.2 自定义变量

1.基本语法

(1)定义变量:变量=值

(2)撤销变量:unset 变量

(3)声明静态变量:readonly变量,注意:不能unset

2.变量定义规则

       (1)变量名称可以由字母、数字和下划线组成,但是不能以数字开头,环境变量名建议大写

       2)等号两侧不能有空格

       (3)在bash中,变量默认类型都是字符串类型,无法直接进行数值运算。

       (4)变量的值如果有空格,需要使用双引号或单引号括起来。

3.案例实操

       (1)定义变量A

[atguigu@hadoop101 datas]$ A=5
[atguigu@hadoop101 datas]$ echo $A
5

       (2)给变量A重新赋值

[atguigu@hadoop101 datas]$ A=8
[atguigu@hadoop101 datas]$ echo $A
8

       (3)撤销变量A

[atguigu@hadoop101 datas]$ unset A
[atguigu@hadoop101 datas]$ echo $A

(4)声明静态的变量B=2,不能unset

[atguigu@hadoop101 datas]$ readonly B=2
[atguigu@hadoop101 datas]$ echo $B
2
[atguigu@hadoop101 datas]$ B=9
-bash: B: readonly variable

(5)在bash中,变量默认类型都是字符串类型,无法直接进行数值运算 

[atguigu@hadoop102 ~]$ C=1+2
[atguigu@hadoop102 ~]$ echo $C
1+2

(6)变量的值如果有空格,需要使用双引号或单引号括起来

[atguigu@hadoop102 ~]$ D=I love banzhang
-bash: world: command not found
[atguigu@hadoop102 ~]$ D="I love banzhang"
[atguigu@hadoop102 ~]$ echo $A
I love banzhang

(7)可把变量提升为全局环境变量,可供其他Shell程序使用

export 变量名

[atguigu@hadoop101 datas]$ vim helloworld.sh 在helloworld.sh文件中增加echo $B
#!/bin/bashecho "helloworld"
echo $B[atguigu@hadoop101 datas]$ ./helloworld.sh 
Helloworld

发现并没有打印输出变量B的值。

[atguigu@hadoop101 datas]$ export B
[atguigu@hadoop101 datas]$ ./helloworld.sh 
helloworld
2

4.3 特殊变量:$n

1.基本语法

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

2.案例实操

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

[atguigu@hadoop101 datas]$ touch parameter.sh 
[atguigu@hadoop101 datas]$ vim parameter.sh#!/bin/bash
echo "$0  $1   $2"[atguigu@hadoop101 datas]$ chmod 777 parameter.sh[atguigu@hadoop101 datas]$ ./parameter.sh cls  xz
./parameter.sh  cls   xz

4.4 特殊变量:$#

1.基本语法

       $#   (功能描述:获取所有输入参数个数,常用于循环)。

2.案例实操

(1)获取输入参数的个数

[atguigu@hadoop101 datas]$ vim parameter.sh#!/bin/bash
echo "$0  $1   $2"
echo $#[atguigu@hadoop101 datas]$ chmod 777 parameter.sh[atguigu@hadoop101 datas]$ ./parameter.sh cls  xz
parameter.sh cls xz 
2

4.5 特殊变量:$*、$@

1.基本语法

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

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

2.案例实操

(1)打印输入的所有参数

[atguigu@hadoop101 datas]$ vim parameter.sh#!/bin/bash
echo "$0  $1   $2"
echo $#
echo $*
echo $@[atguigu@hadoop101 datas]$ bash parameter.sh 1 2 3
parameter.sh  1   2
3
1 2 3
1 2 3

4.6 特殊变量:$?

1.基本语法

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

2.案例实操

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

[atguigu@hadoop101 datas]$ ./helloworld.sh 
hello world
[atguigu@hadoop101 datas]$ echo $?
0

第5章 运算符      

1.基本语法

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

(2)expr  + , - , \*,  /,  %    加,减,乘,除,取余

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

2.案例实操:

(1)计算3+2的值

[atguigu@hadoop101 datas]$ expr 2 + 3
5

(2)计算3-2的值 

[atguigu@hadoop101 datas]$ expr 3 - 2 
1

(3)计算(2+3)X4的值

       (a)expr一步完成计算

[atguigu@hadoop101 datas]$ expr `expr 2 + 3` \* 4
20

(b)采用$[运算式]方式

[atguigu@hadoop101 datas]# S=$[(2+3)*4]
[atguigu@hadoop101 datas]# echo $S

第6章 条件判断

1.基本语法

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

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

2. 常用判断条件

(1)两个整数之间比较

= 字符串比较

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

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

-ge 大于等于(greater equal)    -ne 不等于(Not equal)

(2)按照文件权限进行判断

-r 有读的权限(read)              -w 有写的权限(write)

-x 有执行的权限(execute)

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

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

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

3.案例实操

       (1)23是否大于等于22

[atguigu@hadoop101 datas]$ [ 23 -ge 22 ]
[atguigu@hadoop101 datas]$ echo $?
0

       2helloworld.sh是否具有写权限

[atguigu@hadoop101 datas]$ [ -w helloworld.sh ]
[atguigu@hadoop101 datas]$ echo $?
0

3/home/atguigu/cls.txt目录中的文件是否存在 

[atguigu@hadoop101 datas]$ [ -e /home/atguigu/cls.txt ]
[atguigu@hadoop101 datas]$ echo $?
1

(4)多条件判断(&& 表示前一条命令执行成功时,才执行后一条命令,|| 表示上一条命令执行失败后,才执行下一条命令

[atguigu@hadoop101 ~]$ [ condition ] && echo OK || echo notok
OK
[atguigu@hadoop101 datas]$ [ condition ] && [ ] || echo notok
notok

第7章 流程控制(重点)

7.1 if 判断

1.基本语法

if [ 条件判断式 ];then

  程序

fi

或者

if [ 条件判断式 ]

  then

    程序

fi

       注意事项:

(1)[ 条件判断式 ],中括号和条件判断式之间必须有空格

(2)if后要有空格

2.案例实操

(1)输入一个数字,如果是1,则输出banzhang zhen shuai,如果是2,则输出cls zhen mei,如果是其它,什么也不输出。

atguigu@hadoop101 datas]$ touch if.sh
[atguigu@hadoop101 datas]$ vim if.sh#!/bin/bashif [ $1 -eq "1" ]
thenecho "banzhang zhen shuai"
elif [ $1 -eq "2" ]
thenecho "cls zhen mei"
fi[atguigu@hadoop101 datas]$ chmod 777 if.sh 
[atguigu@hadoop101 datas]$ ./if.sh 1
banzhang zhen shuai

7.2 case 语句

1.基本语法

case $变量名 in

  "值1")

    如果变量的值等于值1,则执行程序1

    ;;

  "值2")

    如果变量的值等于值2,则执行程序2

    ;;

  …省略其他分支…

  *)

    如果变量的值都不是以上的值,则执行此程序

    ;;

esac

注意事项:

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

2.案例实操

(1)输入一个数字,如果是1,则输出banzhang,如果是2,则输出cls,如果是其它,输出renyao。

[atguigu@hadoop101 datas]$ touch case.sh
[atguigu@hadoop101 datas]$ vim case.sh!/bin/bashcase $1 in
"1")echo "banzhang"
;;"2")echo "cls"
;;
*)echo "renyao"
;;
esac[atguigu@hadoop101 datas]$ chmod 777 case.sh
[atguigu@hadoop101 datas]$ ./case.sh 1
1

 7.3 for 循环

1.基本语法1

       for (( 初始值;循环控制条件;变量变化 ))

  do

    程序

  done

2.案例实操

(1)从1加到100

[atguigu@hadoop101 datas]$ touch for1.sh
[atguigu@hadoop101 datas]$ vim for1.sh#!/bin/bashs=0
for((i=0;i<=100;i++))
dos=$[$s+$i]
done
echo $s[atguigu@hadoop101 datas]$ chmod 777 for1.sh 
[atguigu@hadoop101 datas]$ ./for1.sh 
“5050”

 3.基本语法2

for 变量 in 值1 值2 值3…

  do

    程序

  done

4.案例实操

       (1)打印所有输入参数

[atguigu@hadoop101 datas]$ touch for2.sh
[atguigu@hadoop101 datas]$ vim for2.sh#!/bin/bash
#打印数字for i in $*doecho "ban zhang love $i "done[atguigu@hadoop101 datas]$ chmod 777 for2.sh 
[atguigu@hadoop101 datas]$ bash for2.sh cls xz bd
ban zhang love cls
ban zhang love xz
ban zhang love bd

(2)比较$*和$@区别

(a)$*$@都表示传递给函数或脚本的所有参数,不被双引号“”包含时,都以$1 $2 …$n的形式输出所有参数。

[atguigu@hadoop101 datas]$ touch for.sh
[atguigu@hadoop101 datas]$ vim for.sh#!/bin/bash for i in $*
doecho "ban zhang love $i "
donefor j in $@
do      echo "ban zhang love $j"
done[atguigu@hadoop101 datas]$ bash for.sh cls xz bd
ban zhang love cls 
ban zhang love xz 
ban zhang love bd ban zhang love cls
ban zhang love xz
ban zhang love bd

(b)当它们被双引号“”包含时,“$*”会将所有的参数作为一个整体,以“$1 $2 …$n”的形式输出所有参数;“$@”会将各个参数分开,以“$1” “$2”…”$n”的形式输出所有参数。 

[atguigu@hadoop101 datas]$ vim for.sh#!/bin/bash for i in "$*" 
#$*中的所有参数看成是一个整体,所以这个for循环只会循环一次 do echo "ban zhang love $i"done for j in "$@" 
#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次 do echo "ban zhang love $j" 
done[atguigu@hadoop101 datas]$ chmod 777 for.sh
[atguigu@hadoop101 datas]$ bash for.sh cls xz bd
ban zhang love cls xz bd
ban zhang love cls
ban zhang love xz
ban zhang love bd

 7.4 while 循环

1.基本语法

while [ 条件判断式 ]

  do

    程序

  done

2.案例实操

       (1)从1加到100

[atguigu@hadoop101 datas]$ touch while.sh
[atguigu@hadoop101 datas]$ vim while.sh#!/bin/bash
s=0
i=1
while [ $i -le 100 ]
dos=$[$s+$i]i=$[$i+1]
doneecho $s[atguigu@hadoop101 datas]$ chmod 777 while.sh 
[atguigu@hadoop101 datas]$ ./while.sh 
5050

第8章 read读取控制台输入

1.基本语法

       read(选项)(参数)

       选项:

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

-t:指定读取值时等待的时间(秒)。

参数

       变量:指定读取值的变量名

2.案例实操

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

[atguigu@hadoop101 datas]$ touch read.sh
[atguigu@hadoop101 datas]$ vim read.sh#!/bin/bashread -t 7 -p "Enter your name in 7 seconds " NAME
echo $NAME[atguigu@hadoop101 datas]$ ./read.sh 
Enter your name in 7 seconds xiaoze
xiaoze

第9章 函数

9.1 系统函数

1.basename基本语法

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

选项:

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

2.案例实操

(1)截取该/home/atguigu/banzhang.txt路径的文件名称

[atguigu@hadoop101 datas]$ basename /home/atguigu/banzhang.txt 
banzhang.txt
[atguigu@hadoop101 datas]$ basename /home/atguigu/banzhang.txt .txt
banzhang

3.    dirname基本语法

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

4.案例实操

(1)获取banzhang.txt文件的路径

[atguigu@hadoop101 ~]$ dirname /home/atguigu/banzhang.txt 
/home/atguigu

9.2 自定义函数

1.基本语法

[ function ] funname[()]

{

       Action;

       [return int;]

}

funname

2.经验技巧

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

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

3.案例实操

       (1)计算两个输入参数的和

[atguigu@hadoop101 datas]$ touch fun.sh
[atguigu@hadoop101 datas]$ vim fun.sh#!/bin/bash
function sum()
{s=0s=$[ $1 + $2 ]echo "$s"
}read -p "Please input the number1: " n1;
read -p "Please input the number2: " n2;
sum $n1 $n2;[atguigu@hadoop101 datas]$ chmod 777 fun.sh
[atguigu@hadoop101 datas]$ ./fun.sh 
Please input the number1: 2
Please input the number2: 5
7

第10章 Shell工具(重点)

10.1 cut

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

1.基本用法

cut [选项参数]  filename

说明:默认分隔符是制表符

2.选项参数说明

选项参数功能
-f列号,提取第几列
-d分隔符,按照指定分隔符分割列

3.案例实操

(0)数据准备

[atguigu@hadoop101 datas]$ touch cut.txt
[atguigu@hadoop101 datas]$ vim cut.txt
c

(1)切割cut.txt第一列 

[atguigu@hadoop101 datas]$ cut -d " " -f 1 cut.txt 
dong
guan
wo
lai
le

(2)切割cut.txt第二、三列

[atguigu@hadoop101 datas]$ cut -d " " -f 2,3 cut.txt 
shen
zhenwolaile

(3)在cut.txt文件中切割出guan

[atguigu@hadoop101 datas]$ cat cut.txt | grep "guan" | cut -d " " -f 1
guan

(4)选取系统PATH变量值,第2个“:”开始后的所有路径:

[atguigu@hadoop101 datas]$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/atguigu/bin[atguigu@hadoop102 datas]$ echo $PATH | cut -d: -f 2-
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/atguigu/bin

5)切割ifconfig 后打印的IP地址 

[atguigu@hadoop101 datas]$ ifconfig eth0 | grep "inet addr" | cut -d: -f 2 | cut -d" " -f1
192.168.1.102

另解:

 10.2 sed

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

  1. 基本用法

sed [选项参数]  ‘command’  filename

     2. 选项参数说明

选项参数        功能
-e直接在指令列模式上进行sed的动作编辑。

      3. 命令功能描述

命令功能描述
a

 新增,a的后面可以接字串,在下一行出现

d删除
s查找并替换 

         4.案例实操

        (0)数据准备

[atguigu@hadoop102 datas]$ touch sed.txt
[atguigu@hadoop102 datas]$ vim sed.txt
dong shen
guan zhen
wo  wo
lai  laile  le

         (1)将“mei nv”这个单词插入到sed.txt第二行下,打印。 

[atguigu@hadoop102 datas]$ sed '2a mei nv' sed.txt 
dong shen
guan zhen
mei nv
wo  wo
lai  laile  le
[atguigu@hadoop102 datas]$ cat sed.txt 
dong shen
guan zhen
wo  wo
lai  laile  le

注意:文件并没有改变

(2)删除sed.txt文件所有包含wo的行

[atguigu@hadoop102 datas]$ sed '/wo/d' sed.txt
dong shen
guan zhen
lai  laile  le

(3)将sed.txt文件中wo替换为ni 

[atguigu@hadoop102 datas]$ sed 's/wo/ni/g' sed.txt 
dong shen
guan zhen
ni  ni
lai  laile  le

注意:‘g’表示global,全部替换,.

g:表示全局替换(global)。默认情况下,sed 只会替换每行中第一次出现的匹配项,而加上 g 选项后,表示替换每行中所有出现的匹配项。

 (4)将sed.txt文件中的第二行删除并将wo替换为ni

[atguigu@hadoop102 datas]$ sed -e '2d' -e 's/wo/ni/g' sed.txt 
dong shen
ni  ni
lai  laile  le

10.3 awk

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

  1. 基本用法

awk [选项参数] ‘pattern1{action1}  pattern2{action2}...’ filename

pattern:表示AWK在数据中查找的内容,就是匹配模式

action:在找到匹配内容时所执行的一系列命令

   2. 选项参数说明

      选项参数功能
-F指定输入文件折分隔符
-v赋值一个用户定义变量

    3. 案例实操

(0)数据准备

[atguigu@hadoop102 datas]$ sudo cp /etc/passwd ./

(1)搜索passwd文件以root关键字开头的所有行,并输出该行的第7列。 

[atguigu@hadoop102 datas]$ awk -F: '/^root/{print $7}' passwd 
/bin/bash

(2)搜索passwd文件以root关键字开头的所有行,并输出该行的第1列和第7列,中间以“,”号分割。

[atguigu@hadoop102 datas]$ awk -F: '/^root/{print $1","$7}' passwd 
root,/bin/bash

注意:只有匹配了pattern的行才会执行action

(3)只显示/etc/passwd的第一列和第七列,以逗号分割,且在所有行前面添加列名user,shell在最后一行添加"dahaige,/bin/zuishuai"。

[atguigu@hadoop102 datas]$ awk -F : 'BEGIN{print "user, shell"} {print $1","$7} END{print "dahaige,/bin/zuishuai"}' passwd
user, shell
root,/bin/bash
bin,/sbin/nologin
。。。
atguigu,/bin/bash
dahaige,/bin/zuishuai

注意:BEGIN 在所有数据读取行之前执行;END 在所有数据执行之后执行。

(4)将passwd文件中的用户id增加数值1并输出

[atguigu@hadoop102 datas]$ awk -v i=1 -F: '{print $3+i}' passwd
1
2
3
4

        4. awk的内置变量

       变量说明
FILENAME文件名
NR已读的记录数
NF浏览记录的域的个数(切割后,列的个数)

       5.案例实操

(1)统计passwd文件名,每行的行号,每行的列数

[atguigu@hadoop102 datas]$ 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

 (2)切割IP

[atguigu@hadoop102 datas]$ ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk -F " " '{print $1}' 
192.168.1.102

另解:

ip addr show lo | grep "inet " | awk '{print $2}' | awk -F/ '{print $1}'

 {print $2}:表示打印第二个字段(以空格分隔的字段)

awk -F/ '{print $1}':

-F/:指定分隔符为斜杠 /,表示将每行按 / 分隔。

{print $1}:表示打印被 / 分隔的第一个字段。

(3)查询sed.txt中空行所在的行号

[atguigu@hadoop102 datas]$ awk '/^$/{print NR}' sed.txt 
5

10.4 sort

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

  1. 基本语法

sort(选项)(参数)

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

2. 案例实操

(0)数据准备

[atguigu@hadoop102 datas]$ touch sort.sh
[atguigu@hadoop102 datas]$ vim sort.sh 
bb:40:5.4
bd:20:4.2
xz:50:2.3
cls:10:3.5
ss:30:1.6

(1)按照“:”分割后的第三列倒序排序。

[atguigu@hadoop102 datas]$ sort -t : -nrk 3  sort.sh bb:40:5.4
bd:20:4.2
cls:10:3.5
xz:50:2.3
ss:30:1.6

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1551127.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

4.模拟电子技术笔记——半导体三极管

写在前面 这个是第四个模电笔记&#xff0c;我们讲半导体三极管 这一章的很多概念都很重要&#xff0c;并且有一些需要记忆的内容&#xff0c;要认真对待 笔记部分 1.半导体三极管的基本原理简述 1.1结构&#xff1a; 1.这个箭头是PN结正向导通方向 2.有两个类型&#xf…

PCL 移动立方体重建(HOPPE)

目录 一、概述二、代码三、结果 一、概述 PCL中的 pcl::MarchingCubes<pcl::PointXYZRGBNormal>:函数实现移动立方体重建的代码示例。 二、代码 #include <iostream> #include <pcl/io/pcd_io.h> #include <pcl/io/ply_io.h> #include <pcl/point…

生成-理解大一统:一文浅谈多模态大模型最新研究进展

在过去几年中&#xff0c;多模态智能的两个关键支柱——理解和生成&#xff0c;取得了显著进展。多模态大型语言模型&#xff08;MLLMs&#xff09;&#xff0c;如 LLaVA&#xff0c;在视觉语言任务&#xff08;例如视觉问答&#xff09;中表现出色。同时&#xff0c;去噪扩散概…

转行AI产品经理前真后悔没看到这篇…

最近AI行业发展势头正盛&#xff0c;很多人私信我说都来问我AI产品经理转行的事&#xff0c;希望我能给一些意见 文科生能不能做产品经理&#xff0c;大家对这些是一头雾水&#xff0c;也不知道AI产品经理具体都做些什么&#xff0c;又要具备那些能力 因为在不同的业务发展不一…

领夹麦克风性价比最高?一文看懂领夹麦克风什么牌子的好

近几年随着网络直播、短视频等新兴行业的发展&#xff0c;筑就了一个全民视频创作的时代。而领夹麦克风也是凭借轻便、便携的特性&#xff0c;获得了广大短视频创作者的青睐&#xff0c;领夹麦克风的需求量也是不断增加。也正是因为如此&#xff0c;如今市面上的领夹麦克风品牌…

一文上手SpringSecurity【八】

RBAC&#xff08;Role-Based Access Control&#xff09;&#xff0c;基于角色的访问控制。通过用户关联角色&#xff0c;角色关联权限&#xff0c;来间接的为用户赋予权限。 一、RBAC介绍 RBAC&#xff08;Role-Based Access Control&#xff09;&#xff0c;即基于角色的访…

企业微信:客户联系自带群发工具和聊天工具

前言 上篇博客介绍了一些客户联系的开启和配置&#xff0c;接下来我们来使用客户联系自带群发工具和聊天工具。 突然发现官方的文档已经很详细了&#xff0c;我这里给出一些简单的描述&#xff1a; 企业微信如何使用群发助手&#xff1f;-帮助中心-企业微信 群发工具 群发消息给…

Python 中的lambda函数表达式

lambda x:xn 这是一个 Python 中的lambda函数表达式。它定义了一个匿名函数&#xff0c;该函数接受一个参数x&#xff0c;并返回xn的值。再定义常数n。 n 5 my_function lambda x: x n print(my_function(3)) 运行结果&#xff1a; 在上述代码中&#xff0c;首先定义了…

win10专业版永久关闭自动更新功能

如何关闭 Windows 10 自动更新 Windows 10 默认情况下会自动安装更新&#xff0c;这可能会导致系统不稳定或问题。如果您想关闭自动更新&#xff0c;可以使用以下方法&#xff1a; 方法 1&#xff1a;使用设置应用程序 打开“设置”应用程序。转到“更新和安全”。在“Windo…

获取本函数所在代码域内的所有局部变量和值以字典形式返回locals()

【小白从小学Python、C、Java】 【考研初试复试毕业设计】 【Python基础AI数据分析】 获取本函数所在代码域内的 所有局部变量和值 以字典形式返回 locals() 选择题 关于以下代码输出的结果说法正确的是&#xff1f; a 10 def x(): b 20 print(locals()) print("【执行…

TTT大语言模型架构发布,成功撼动了Transformer与Mamba模型

上期图文&#xff0c;我们刚介绍了 transformer 模型与 Mamba 模型&#xff0c;觉得 Mamba 模型的发布可以有效改善 transformer 模型长序列输入复杂度的问题&#xff0c;应该能够成为下一个大语言模型的基石。谁想Mamba2 还没有发布几天&#xff0c;这边最新的大语言模型TTT模…

隆道携手黑龙江省中小企业协会助力区域企业数字化转型

9月28日&#xff0c;隆道公司总裁吴树贵出席“2024年黑龙江省中小企业协会副会长扩大会议暨数字化转型专题报告会”并做主题发言&#xff0c;分享了龙江惠企商城建设情况和供应链数字化转型实践经验。会上&#xff0c;隆道公司与黑龙江省中小企业协会签订了战略合作协议&#x…

【Kubernetes知识点】 解读 Service 和 EndpointSlice 之间的关系

【Kubernetes知识点】 解读 Service 和 EndpointSlice 之间的关系 目录 1 概念 1.1 Service的概念1.2 Endpoint 的概念1.3 EndpointSlice 的引入 1.3.1 EndpointSlice支持的地址1.3.2 EndpointSlice的状态1.3.3 EndpointSlice的拓扑信息 1.4 Service 、Endpoint和 EndpointSl…

自动驾驶TPM技术杂谈 ———— 高精度地图

文章目录 概述高精度地图分层架构价值体现 关键技术道路元素图像处理激光点云处理点云特征提取点云法向量点云配准点云分割 同步定位与地图构建高精度地图云端服务体系 解决方案高精度地图采集数据模型 高精度地图制作和编译数据处理编译及格式规范NDSOpenDRIVE 高精度地图质量…

入职2年的程序员,被劝退了!年纪大了,感觉好绝望!

入职2年的程序员&#xff0c;今天被劝退了&#xff01;年纪大了&#xff0c;感觉好绝望&#xff01; 我的朋友是一位程序员&#xff0c;毕业后去了BAT企业&#xff0c;前2年去了一家国企&#xff0c;至今刚满2年&#xff0c;刚进去绩效领导给打了C&#xff0c;现在被边缘化&…

可视化是工业互联网的核心技术之一,都有哪些应用场景?

一、工业互联网是什么&#xff0c;发展的来胧去脉 工业互联网是指利用互联网技术和物联网技术&#xff0c;将工业生产中的各种设备、机器、传感器等进行互联互通&#xff0c;实现信息的实时采集、传输和分析&#xff0c;从而实现生产过程的智能化、自动化和高效化。 工业互联网…

echarts实现3D柱状图(视觉层面)根据博主改编

https://blog.csdn.net/weixin_57798646/article/details/131067725 这是原贴 在这个基础上我需要实现 一根柱子 代码如下 <!DOCTYPE html> <html lang"en" style"height: 100%"><head><meta charset"utf8"> </hea…

Python画笔案例-069 绘制调皮田彩格

1、绘制调皮田彩格 通过 python 的turtle 库绘制 调皮田彩格,如下图: 2、实现代码 绘制 调皮田彩格,以下为实现代码: """调皮田彩格.py本程序需要coloradd模块支持,安装方法:pip install coloradd =""" import turtle from coloradd import…

AIGC实践|AI助力文旅短视频创作全流程

前言&#xff1a; 受到央视《AI我中华》及各地文旅AI宣传片的启发&#xff0c;本次我将尝试使用AI辅助进行城市宣传片的创作探索。我将尽可能详细的展示使用AI辅助创作城市宣传片的全过程&#xff0c;从灵感捕捉到最终成品呈现。现在&#xff0c;让我们一同踏上这段充满创意的探…

工作日志:el-table在无数据情况下,出现横向滚动条。

1、遇到一个警告。 原因&#xff1a;中的组件不能呈现动画的非元素根节点。 也就是说&#xff0c;Transition包裹的必须是一个单根的组件。 2、el-table在无数据情况下&#xff0c;出现横向滚动条&#xff0c;大概跟边框的设置有关系。 开始排查。 给.el-scrollbar加了一个…