Shell

文章目录

  • Shell
    • shell概述
    • Shell脚本入门
      • 1. 脚本格式
      • 2. 第一个Shell脚本:helloworld
      • 3. 第二个Shell脚本:多命令处理
    • Shell中的变量
      • 系统变量
        • 1. 常用系统变量
        • 2. 案例实操
      • 自定义变量
        • 1. 基本语法
        • 2. 变量定义规则
      • 特殊变量:$n
      • 特殊变量:$#
      • 特殊变量:\$*、\$@
      • 特殊变量:$?
    • 运算符
      • 1. 基本语法
      • 2. 案例实操
    • 条件判断
      • 1. 基本语法
      • 2. 常用判断条件
      • 案例实操
    • 流程控制
      • if判断
        • 1. 基本语法
        • 2. 案例实操
      • case语句
        • 1. 基本语法
        • 2. 案例实操
      • for循环
        • 1. 基本语法1
        • 2. 案例实操
        • 3. 基本语法2
        • 4. 案例实操
      • while循环
        • 1. 基本语法
        • 2. 案例实操
    • read读取控制台输入
      • 1. 基本语法
      • 2. 案例实操
    • 函数
      • 系统函数
        • 1. basename 基本语法
        • 2. 案例实操
        • 3. dirname基本语法
        • 4. 案例实操
      • 自定义函数
        • 1. 基本语法
        • 2. 注意事项
        • 3. 案例实操
    • Shell工具
      • 1. cut
        • 1. 基本用法
        • 2. 选项参数说明
        • 3. 案例实操
      • 2. sed
        • 1. 基本用法
        • 2. 选项参数说明
        • 3. 命令功能描述
        • 5. 案例实操
      • 3. awk
        • 1. 基本用法
        • 2. 选项参数说明
        • 3. 案例实操
        • awk的内置变量
        • 案例实操
      • 4. sort
        • 1. 基本语法
        • 2. 选项
        • 3. 案例实操
    • 面试题
      • 京东
      • 搜狐
      • 新浪

shell概述

Shell是一个命令行解释器,他接收应用程序/用户命令,然后调用操作系统内核,Shell是一个功能相当强大的编程语言,易于编写,调试,灵活性强。

Shell脚本入门

1. 脚本格式

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

2. 第一个Shell脚本:helloworld

首先我们创建一个shell脚本文件:helloworld.sh

#!/bin/bash
echo "helloworld"

保存之后运行

sh执行:

sh helloworld.sh

bash执行:

bash helloworld.sh

绝对路径sh执行:

sh /home/zsb/helloworld.sh

但是./helloworld.sh不行,为什么呢?

因为sh与bash是代替我们去执行,而我们自己执行时会显示没权限,脚本本身不需要执行权限,第二种本质是脚本需要自己执行,所以需要执行权限,我们查看一下文件的权限:

发现没有执行权限,故chmod就可以执行了:

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

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

#!/bin/bashcd /home/zsb/
touch banzhang.txt
echo "I love cls" >> banzhang.txt

Shell中的变量

系统变量

1. 常用系统变量

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

2. 案例实操
[zsb@VM-4-8-centos ~]$ echo $HOME   //家目录
/home/zsb
[zsb@VM-4-8-centos ~]$ echo $PWD   //当前所处目录
/home/zsb
[zsb@VM-4-8-centos ~]$ echo $SHELL  //当前默认shell
/bin/bash
[zsb@VM-4-8-centos ~]$ echo $USER  //当前用户
zsb

自定义变量

1. 基本语法
  • 定义变量:变量=值
  • 撤销变量:unset 变量
  • 声明静态变量:readonly 变量,注意:不能unset
[root@VM-4-8-centos zsb]# A=1
[root@VM-4-8-centos zsb]# echo $A
1
[root@VM-4-8-centos zsb]# unset A
[root@VM-4-8-centos zsb]# echo $A[root@VM-4-8-centos zsb]# readonly A=1
[root@VM-4-8-centos zsb]# echo $A
1
[root@VM-4-8-centos zsb]# unset A
bash: unset: A: cannot unset: readonly variable

readonly变量在关掉ssh后自动消除。

2. 变量定义规则
  • 变量名称可以由字母、数字和下划线组成,但是不能以数字开头,环境变量名建议大写。
  • 等号两侧不能有空格。
  • 在 bash中,变量默认类型都是字符串类型,无法直接进行数值运算。
  • 变量的值如果有空格,需要使用双引号或单引号括起来。
  • 可把变量提升为全局变量,可供其他shell程序使用,export 变量
[root@VM-4-8-centos zsb]# B = 1
bash: B: command not found
[root@VM-4-8-centos zsb]# B= 1
bash: 1: command not found[root@VM-4-8-centos zsb]# B=1+1
[root@VM-4-8-centos zsb]# echo $B
1+1[root@VM-4-8-centos zsb]# B="1 2 3"
[root@VM-4-8-centos zsb]# echo $B
1 2 3

我们在前面的helloworld.sh中编写:

#!/bin/bashecho "hello world"
echo $B

我们发现打印不出B的值,原因是我们定义的B变量只是局部的,并不是全局的,所以我们可以将B变量提升为全局变量,供我们的helloworld.sh程序使用:

[root@VM-4-8-centos shell_study]# export B
[root@VM-4-8-centos shell_study]# ./helloworld.sh
hello world
1 2 3

此时就打印出来了。

特殊变量:$n

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

例如:

parameter.sh:

#!/bin/bashecho $0 $1 $2 $3
[root@VM-4-8-centos shell_study]# ./parameter.sh
./parameter.sh
[root@VM-4-8-centos shell_study]# ./parameter.sh cls
./parameter.sh cls
[root@VM-4-8-centos shell_study]# ./parameter.sh cls xz
./parameter.sh cls xz
[root@VM-4-8-centos shell_study]# ./parameter.sh cls xz zsb
./parameter.sh cls xz zsb

特殊变量:$#

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

parameter.sh:

#!/bin/bashecho $0 $1 $2 $3
echo $#
[root@VM-4-8-centos shell_study]# ./parameter.sh
./parameter.sh
0
[root@VM-4-8-centos shell_study]# ./parameter.sh  cls
./parameter.sh cls
1
[root@VM-4-8-centos shell_study]# ./parameter.sh  cls cls xz
./parameter.sh cls cls xz
3

特殊变量:$*、$@

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

parameter.sh:

#!/bin/bashecho $0 $1 $2 $3
echo $#
echo $*
echo $@
[root@VM-4-8-centos shell_study]# ./parameter.sh cls xz zsb
./parameter.sh cls xz zsb
3
cls xz zsb
cls xz zsb

特殊变量:$?

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

[root@VM-4-8-centos shell_study]# ./helloworld.sh
hello world
1 2 3
[root@VM-4-8-centos shell_study]# echo $?
0
[root@VM-4-8-centos shell_study]# $?
bash: 0: command not found
[root@VM-4-8-centos shell_study]# echo $?
127

运算符

1. 基本语法

(1) “$((运算式))”或“$[运算式]”
(2) expr +,-,\*,/, % 加,减,乘,除,取余
注意: expr运算符间要有空格

2. 案例实操

(1) 计算3+2的值

[root@VM-4-8-centos ~]# expr 3 + 2
5
[root@VM-4-8-centos ~]# echo $((3+2))
5
[root@VM-4-8-centos ~]# echo $[3+2]
5

(2) 计算3-2的值

[root@VM-4-8-centos ~]# expr 3 - 2
1
[root@VM-4-8-centos ~]# echo $((3-2))
1
[root@VM-4-8-centos ~]# echo $[3-2]
1

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

[root@VM-4-8-centos ~]# expr `expr 2 + 3` \* 4
20
[root@VM-4-8-centos ~]# echo $(((2+3)*4))
20
[root@VM-4-8-centos ~]# echo $[(2+3)*4]
20

条件判断

1. 基本语法

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

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

2. 常用判断条件

  • 两个整数之间比较

= 字符串比较

-lt 小于(less than)

-le 小于等于(less equal)

-eq 等于(equal)

-gt 大于(greater than)

-ge 大于等于(greater equal)

-ne 不等于(Not equal)

  • 按照文件权限进行判断

-r 有读的权限(read)

-w 有写的权限(write)

-x 有执行的权限(execute)

  • 按照文件类型进行判断

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

-e 文件存在(existence)

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

案例实操

(1) 23是否大于等于22

[root@VM-4-8-centos ~]# [ 23 -ge 22 ]
[root@VM-4-8-centos ~]# echo $?
0
[root@VM-4-8-centos ~]# [ 23 -le 22 ]
[root@VM-4-8-centos ~]# echo $?
1

0为true,非0为false

(2) helloworld.sh是否具有写权限

[root@VM-4-8-centos shell_study]# [ -w helloworld.sh ]
[root@VM-4-8-centos shell_study]# echo $?
0

(3) /home/zsb/zsb.txt文件是否存在

[root@VM-4-8-centos shell_study]# [ -e zsb.txt ]
[root@VM-4-8-centos shell_study]# echo $?
1

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

条件:helloworld.sh有写权限,zsb.txt文件不存在

[root@VM-4-8-centos shell_study]# [ -w helloworld.sh ] && echo "yes"
yes[root@VM-4-8-centos shell_study]# [ -w helloworld.sh ] || echo "yes"
[root@VM-4-8-centos shell_study]#[root@VM-4-8-centos shell_study]# [ -e zsb.txt ] || echo "no"
no

流程控制

if判断

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

或者

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

注意:

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

(2) if后要有空格。

2. 案例实操

(1) 输入一个数字,如果是1,则输出 班长真帅,如果是2,则输出 班长真丑,如果是其它,什么也不输出。

#!/bin/bashif [ $1 -eq 1 ]
thenecho "好好学习,进大厂"
elif [ $1 -eq 2 ]
thenecho "不好好学习,回家养猪"
fi
[root@VM-4-8-centos shell_study]# sh if.sh 1
好好学习,进大厂
[root@VM-4-8-centos shell_study]# sh if.sh 2
不好好学习,回家养猪

case语句

1. 基本语法
case $变量名 in
"值1")如果变量的值签于值1,则执行程序1
;;
"值2")如果变量的值签于值2,则执行程序2
;;
*)如果变量的值非值1和值2,则执行该程序
;;
esac
2. 案例实操

(1) 输入1输出man,输入2输出woman,输入3输出renyao。

#!/bin/bashcase $1 in
1)echo "man"
;;
2)echo "woman"
;;
*)echo "renyao"
;;
esac
[root@VM-4-8-centos shell_study]# sh case.sh 1
man
[root@VM-4-8-centos shell_study]# sh case.sh 2
woman
[root@VM-4-8-centos shell_study]# sh case.sh 3
renyao

for循环

1. 基本语法1
for(( 初始值;循环控制条件;变量变化 ))
do程序
done
2. 案例实操

(1) 从1加到100

#!/bin/bash
s=0
for((i=1;i<=100;i++))
dos=$[$s+$i]
done
echo $s
[root@VM-4-8-centos shell_study]# sh for1.sh
5050
3. 基本语法2
for 变量 in 值1 值2 值3...
do程序
done
4. 案例实操

(1) 打印所有输入参数

#!/bin/bashfor i in $*
doecho $i
done
[root@VM-4-8-centos shell_study]# sh for2.sh 1 2 3
1
2
3

while循环

1. 基本语法
while [ 条件判断式 ]
do程序
done
2. 案例实操

(1) 从1加到100

#!/bin/bash
i=1
s=0
while [ $i -le 100 ]
dos=$[$s+$i]i=$[$i+1]
doneecho $s
[root@VM-4-8-centos shell_study]# sh while.sh
5050

read读取控制台输入

1. 基本语法

read 选项 参数
选项:-p:指定读取值时的提示符;。-t:指定读取值时等待的时间(秒)。
参数:变量:指定读取值的变量名

2. 案例实操

(1) 在7秒内输入名字,并输出。

#!bin/bashread -t 7 -p "please input your name: " NAME
echo $NAME
[root@VM-4-8-centos shell_study]# sh read.sh
please input your name: zsb
zsb

函数

系统函数

1. basename 基本语法
basename string/pathname suffix
(功能描述: basename命令会删掉所有的前缀包括最后一个(‘/')字符,然后将字符串显示出来。
选项:
suffix为后缀,如果 suffix被指定了, basename 会将pathname或 string 中的 suffix去掉。
2. 案例实操

(1) 截取/home/zsb/helloworld.sh路径的文件名称

[root@VM-4-8-centos shell_study]# basename /home/zsb/shell_study/helloworld.sh
helloworld.sh
[root@VM-4-8-centos shell_study]# basename /home/zsb/shell_study/helloworld.sh .sh
helloworld
3. dirname基本语法
dirname文件绝对路径
(功能描述:从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然后返回剩下的路径(目录的部分)。
4. 案例实操

(1) 获取helloworld.sh文件的路径

[root@VM-4-8-centos shell_study]# dirname /home/zsb/shell_study/helloworld.sh
/home/zsb/shell_study

自定义函数

1. 基本语法
[ function ] funname[()]
{Action:[return int;]
}
funname
2. 注意事项

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

3. 案例实操

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

#!/bin/bashfunction sum()
{s=0;s=$[$1+$2]echo $s
}
read -p "请输入加数1:" p1
read -p "请输入加数2:" p2sum $p1 $p2
[root@VM-4-8-centos shell_study]# sh function.sh
请输入加数1:1
请输入加数2:2
3

Shell工具

1. cut

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

1. 基本用法

cut [选项参数] filename

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

2. 选项参数说明

-f:列号,提取第几列

-d:分隔符,按照指定分隔符分割列

3. 案例实操

(1) 切割cut.txt第二列

[root@VM-4-8-centos shell_study]# cut -d " " -f 2 cut.txt
shen
zhen
wo
lai
le

(2) 在cut.txt文件中切割出 jing

[root@VM-4-8-centos shell_study]# cat cut.txt | grep jing
jing zhen
[root@VM-4-8-centos shell_study]# cat cut.txt | grep jing | cut -d " " -f 1
jing

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

[root@VM-4-8-centos shell_study]# echo $PATH
/opt/rh/devtoolset-7/root/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@VM-4-8-centos shell_study]# echo $PATH | cut -d ":" -f 3-
/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

(4) 切割ifconfig后打印的IP地址

[root@VM-4-8-centos shell_study]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 10.0.4.8  netmask 255.255.252.0  broadcast 10.0.7.255inet6 fe80::5054:ff:fed5:55e2  prefixlen 64  scopeid 0x20<link>ether 52:54:00:d5:55:e2  txqueuelen 1000  (Ethernet)RX packets 4646630  bytes 1981270152 (1.8 GiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 3936053  bytes 697389243 (665.0 MiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@VM-4-8-centos shell_study]# ifconfig eth0 | grep "inet "inet 10.0.4.8  netmask 255.255.252.0  broadcast 10.0.7.255
[root@VM-4-8-centos shell_study]# ifconfig eth0 | grep "inet " | cut -d " " -f 10
10.0.4.8

2. sed

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

1. 基本用法

sed [选项参数] ‘command’ filename

2. 选项参数说明

-e :直接在指令列模式上进行sed的动作编辑

3. 命令功能描述

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

d:删除

s:查找并替换

5. 案例实操

sed.txt

bei shen
jing zhen
wo wo
lai laile le

(1) 在第二行增加mei nv

[root@VM-4-8-centos shell_study]# sed "2a mei nv" sed.txt
bei shen
jing zhen
mei nv
wo wo
lai laile le
并不会改变源文件:
[root@VM-4-8-centos shell_study]# cat sed.txt
bei shen
jing zhen
wo wo
lai laile le

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

[root@VM-4-8-centos shell_study]# sed "/wo/d" sed.txt
bei shen
jing zhen
lai laile le

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

[root@VM-4-8-centos shell_study]# sed "s/wo/ni/" sed.txt
bei shen
jing zhen
ni wo
lai laile le[root@VM-4-8-centos shell_study]# sed "s/wo/ni/g" sed.txt
bei shen
jing zhen
ni ni
lai laile le
//g表示全局,不加g只会替换第一个

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

[root@VM-4-8-centos shell_study]# sed "2d" sed.txt | sed "s/wo/ni/g"
bei shen
ni ni
lai laile le

或者:

[root@VM-4-8-centos shell_study]# sed -e "2d" -e "s/wo/ni/g" sed.txt
bei shen
ni ni
lai laile le

3. awk

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

1. 基本用法

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

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

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

2. 选项参数说明

-F:指定输入文件拆分隔符

-v:赋值一个用户定义变量

3. 案例实操

(0) 数据准备

[root@VM-4-8-centos shell_study]# cp -rf /etc/passwd ./
[root@VM-4-8-centos shell_study]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:998:997:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
syslog:x:996:994::/home/syslog:/bin/false
lighthouse:x:1000:1000::/home/lighthouse:/bin/bash
zsb:x:1001:1001::/home/zsb:/bin/bash

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

awk -F ":" '/^root/{print $7}' passwd

^root表示以root开头,$root表示以root结尾

[root@VM-4-8-centos shell_study]# awk -F ":" '/^root/{print $7}' passwd
/bin/bash

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

awk -F ":" '/^root/{print $1","$7}' passwd
[root@VM-4-8-centos shell_study]# awk -F ":" '/^root/{print $1","$7}' passwd
root,/bin/bash

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

[root@VM-4-8-centos shell_study]# awk -F ":" 'BEGIN {print "user,shell"} {print $1","$7} END{print "shibingge,/bin/zuishuai"}' passwd
user,shell
root,/bin/bash
bin,/sbin/nologin
daemon,/sbin/nologin
adm,/sbin/nologin
lp,/sbin/nologin
sync,/bin/sync
shutdown,/sbin/shutdown
halt,/sbin/halt
mail,/sbin/nologin
operator,/sbin/nologin
games,/sbin/nologin
ftp,/sbin/nologin
nobody,/sbin/nologin
systemd-network,/sbin/nologin
dbus,/sbin/nologin
polkitd,/sbin/nologin
libstoragemgmt,/sbin/nologin
rpc,/sbin/nologin
ntp,/sbin/nologin
abrt,/sbin/nologin
sshd,/sbin/nologin
postfix,/sbin/nologin
chrony,/sbin/nologin
tcpdump,/sbin/nologin
syslog,/bin/false
lighthouse,/bin/bash
zsb,/bin/bash
shibingge,/bin/zuishuai

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

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

[root@VM-4-8-centos shell_study]# awk -F ":" -v i=1 '{print $3+i}' passwd
1
2
3
4
5
6
7
8
9
12
13
15
100
193
82
1000
999
33
39
174
75
90
998
73
997
1001
1002
awk的内置变量

FILENAME:文件名

NR:已读的记录数(也就是第几行)

NF:浏览记录的域的个数(也就是切割后,列的个数)

案例实操

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

[root@VM-4-8-centos shell_study]# awk -F ":" '{print FILENAME "," NR "," NF}' passwd
passwd,1,7
passwd,2,7
passwd,3,7
passwd,4,7
passwd,5,7
passwd,6,7
passwd,7,7
passwd,8,7
passwd,9,7
passwd,10,7
passwd,11,7
passwd,12,7
passwd,13,7
passwd,14,7
passwd,15,7
passwd,16,7
passwd,17,7
passwd,18,7
passwd,19,7
passwd,20,7
passwd,21,7
passwd,22,7
passwd,23,7
passwd,24,7
passwd,25,7
passwd,26,7
passwd,27,7

(2) 切割IP

[root@VM-4-8-centos shell_study]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 10.0.4.8  netmask 255.255.252.0  broadcast 10.0.7.255inet6 fe80::5054:ff:fed5:55e2  prefixlen 64  scopeid 0x20<link>ether 52:54:00:d5:55:e2  txqueuelen 1000  (Ethernet)RX packets 4683407  bytes 1984688896 (1.8 GiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 3972795  bytes 703363499 (670.7 MiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@VM-4-8-centos shell_study]# ifconfig eth0 | grep "inet " | awk -F " " '{print $2}'
10.0.4.8

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

[root@VM-4-8-centos shell_study]# awk '/^$/{print NR}' sed.txt
5

4. sort

sort命令是将文件进行排序,并将排序结果标准输出

1. 基本语法

sort 选项 参数

2. 选项

-n:按照数值的大小排序

-r:以相反的顺序来排序

-t:设置排序时所用的分格字符

-k:指定需要排序的列

3. 案例实操

(0) 数据准备

bb:40:5.4
bd:20:4.2
xz:50:2.3
cls:10:3.5
ss:30:1.6

(1) 按照第2列对数据排序

sort -t : -n -k 2 sort.txt

面试题

京东

问题1:使用Linux命令查询file1中空行所在的行号

awk '/^$/{print NR}' file1

问题2:有文件chengji.txt内容如下;
张三 40
李四 50
王五 60

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

[root@VM-4-8-centos shell_study]# awk -F " " -v sum=0 '{sum+=$2} END{print sum}' chengji.txt
150

搜狐

问题1:Shell脚本里如何检查一个文件是否存在?如果不存在该如何处理?

#/bin/bashif [ -e helloworld.sh ]
thenecho "文件存在!"
elseecho "文件不存在!"
fi

新浪

问题1:用shell写一个脚本,对文本中无序的一列数字排序并求和

排序:
[root@VM-4-8-centos shell_study]# sort -n test.txt
1
2
3
4
5
6
7
8
9
10
排序并求和
[root@VM-4-8-centos shell_study]# sort -n test.txt | awk -v sum=0 '{sum+=$1;print $1} END{print sum}'
1
2
3
4
5
6
7
8
9
10
55

问题2:请用shell脚本写出查找当前文件夹(/home)下所有文本文件内容中包含有字符"shen"的文件名称

grep -r "shen" /home | cut -d ":" -f 2

Shell脚本从入门到实战相关推荐

  1. linux shell脚本,入门到实战详解(二)

    三.shell 编程流程控制语句 (点击进入Linux实战课程↑↑) 3.1 if 条件语句介绍 3.1.1 常用的单/双分支 if 条件语句: # If条件判断语句,通常以if开头,fi结尾.也可加 ...

  2. Shell 脚本 快速入门到实战 -02

    7 流程控制 (重点) ​ 7.1 case 语句 ​ 7.2 case语句 ​ 7.3 for 循环 ​ 7.4 while循环 8 read 读取控制台输入 9 函数 ​ 9.1 系统函数 ​ 9 ...

  3. 【shell笔记】Linux Shell脚本编程入门知识点全面涵盖

    本文是我对白树明老师shell课程笔记的总结,课程链接:https://www.bilibili.com/video/BV1j541157Sr?from=search&seid=9757674 ...

  4. Linux Shell 脚本编程 入门

    查看当系统有什么shell解释器:cat /etc/shells  CentOs默认用的是 bash    :  echo $SHELL Shell 脚本入门 1.脚本格式: 脚本要以  #!/bin ...

  5. 简单shell脚本编程入门

    简单的shell编程入门: 步骤一: 编写一个简单的shell程序: Shell编程开头标准写法(固定的),设定Shell解释器 步骤二: 添加可执行权限 也可以不添加权限(使用sh + 脚本名称)[ ...

  6. shell 脚本简单入门

    好久不写shell脚本,有些生疏.总结下shell的语法,以便后续参考,快速捡起来. shell 脚本执行的3种方式: 1). ./xx.sh (xx.sh 需要有执行权限) 2). source x ...

  7. Shell脚本编程入门

    文章目录 一.Shell是什么? 二.Shell脚本的格式及其运行方式 三.变量 四.运算符 五.条件判断 六.程序流程 八.函数 一.Shell是什么? shell是一个命令行解释器,它接收应用程序 ...

  8. Linux Shell脚本编程入门

    Shell Script Shell 脚本是包含一系列命令的文本本当:读取执行该文本文档就可以执行文档里面的命令: 常用于执行批量事务处理.重复性操作.自动化运维.定时执行任务等 编写一个简单 She ...

  9. php shell脚本怎么写,Shell脚本编程入门

    一.什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_tut for ((i=0; i<10; i++)); do tou ...

最新文章

  1. plsql 循环存储过程返回数据集合_Java基础(十五)——Collection集合、泛型 - 寒江雨
  2. 字符串插入_动态规划----字符串编辑最小距离
  3. Android中ImageView的旋转与缩放
  4. 25 MM配置-采购-无物料主数据的项目输入帮助
  5. 从一次线上故障思考Java问题定位思路,java初级面试笔试题
  6. 分治法——查找问题 —— 寻找一个序列中第k小的元素和查找最大和次大元素
  7. 无法打开预编译头文件的解决方法及预编译头原理[ZZ] 转
  8. proteus部分元件中英对照
  9. 主管都在用项目管理Excel表格模板管理项目
  10. MATLAB图像检索系统GUI设计
  11. html点击冒泡事件,JavaScript 浏览器事件机制(捕获、冒泡、委托)
  12. STC12系列单片机的AUXR辅助寄存器
  13. HDOJ 1859 最小长方形
  14. D3D9学习笔记之模板
  15. 金融 python 招聘,滴滴、度小满金融python工程师社招面经
  16. 台式计算机更改bios密码,台式机或笔记本忘记Bios密码解决方法
  17. 服务器ibmc无法加载js文件,weUI应用之JS常用信息提示弹层的封装
  18. Kafka Group coordinator xxx:9092 (id: xxx rack: null) is unavailable or invalid
  19. Tushare介绍、安装及快速入门
  20. 如何打理水果店,夏天水果店怎么打理

热门文章

  1. 如何关闭react的端口号_react修改端口号
  2. Ultraedit配置字数统计工具栏
  3. flash webgame架构
  4. 计算机类和鼠标类是什么关系,四种鼠标类型的优缺点
  5. 数据可视化|用帕累托图进行数据分析
  6. 洛克人java下载_JAVA 1.7并发之LinkedTransferQueue原理理解
  7. 自定义导航栏titleView在iOS10以上的问题
  8. Chrome插件(扩展)安装失败的解决方法
  9. docker 操作mysql
  10. 今日头条2018校园春季招聘研发岗位笔试(第一场)经验