Shell脚本从入门到实战

Shell

shell概述

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

Shell脚本入门

1. 脚本格式

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

2. 第一个Shell脚本:helloworld

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

image-20230122153423681

#!/bin/bash
echo "helloworld"

保存之后运行

sh执行:

sh helloworld.sh

bash执行:

bash helloworld.sh

绝对路径sh执行:

sh /home/zsb/helloworld.sh

image-20230122154938909

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

image-20230122155145996

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

image-20230122155329315

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

image-20230122155416755

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

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

#!/bin/bash

cd /home/zsb/
touch banzhang.txt
echo "I love cls" >> banzhang.txt

image-20230122163418143

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/bash

echo "hello world"
echo $B

image-20230122195444469

我们发现打印不出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/bash

echo $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/bash

echo $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/bash

echo $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/bash

if [ $1 -eq 1 ]
then
	echo "好好学习,进大厂"
elif [ $1 -eq 2 ]
then
	echo "不好好学习,回家养猪"
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/bash

case $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++))
do
	s=$[$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/bash

for i in $*
do
	echo $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 ]
do
	s=$[$s+$i]
	i=$[$i+1]
done

echo $s
[root@VM-4-8-centos shell_study]# sh while.sh 
5050

read读取控制台输入

1. 基本语法
read 选项 参数
选项:
    -p:指定读取值时的提示符;。
    -t:指定读取值时等待的时间(秒)。
参数:
	变量:指定读取值的变量名	
2. 案例实操

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

#!bin/bash

read -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/bash

function sum()
{
	s=0;
	s=$[$1+$2]
	echo $s
}
read -p "请输入加数1:" p1
read -p "请输入加数2:" p2

sum $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 1500
        inet 10.0.4.8  netmask 255.255.252.0  broadcast 10.0.7.255
        inet6 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 0
        TX 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 lai

le 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 lai

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

le le

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

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

le 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 lai

le le

[root@VM-4-8-centos shell_study]# sed "s/wo/ni/g" sed.txt 
bei shen
jing zhen
ni ni
lai lai

le 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 lai

le le

或者:

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

le 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 1500
        inet 10.0.4.8  netmask 255.255.252.0  broadcast 10.0.7.255
        inet6 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 0
        TX 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/bash

if [ -e helloworld.sh ]
then
	echo "文件存在!"
else
	echo "文件不存在!"
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
  • 4
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 《shell脚本实战》是一本介绍如何使用Shell编写脚本的电子书籍。Shell脚本是一种用于自动化任务和批处理的脚本语言,广泛应用于Unix、Linux和类Unix系统中。 这本电子书逐步引导读者从基础知识开始,包括Shell脚本的语法、变量、运算符、条件判断等。然后通过实例演示了如何编写各种常见的Shell脚本,例如文件操作、字符串操作、数学计算、循环、条件语句等。 《shell脚本实战》中还介绍了如何编写高级Shell脚本,例如函数、数组、正则表达式等。同时,它还展示了如何与系统命令、文件、网络、数据库等进行交互,从而帮助读者更好地掌握Shell脚本的灵活应用。 该电子书的实战部分还包括了一些实际场景下的应用示例,如日志分析、备份脚本、系统监控等。通过这些示例,读者可以学到如何解决实际问题,并将Shell脚本运用于自己的工作中。 总而言之,《shell脚本实战》是一本适合初学者和有一定基础的读者的实用电子书。通过学习本书,读者可以系统地了解Shell脚本的编写方法和应用技巧,提高工作效率,实现自动化任务,为日常工作带来便利。 ### 回答2: 《Shell脚本实战》,是一本针对Shell脚本编程的实战性电子书。本书主要面向对Shell脚本编程有一定基础的读者,旨在通过实际案例的讲解和实战练习的方式,帮助读者提升自己在Shell脚本编程方面的技能。 本书分为七个章节,分别介绍了Shell脚本编程的基础知识、字符串处理、文件操作、流程控制、函数、正则表达式以及常用工具等内容。每个章节都以案例为例,通过实际场景的描述和具体的代码示例,向读者展示了如何使用Shell脚本来解决实际问题。 书中的案例涵盖了各个领域,包括系统管理、日志分析、文本处理、数据清洗等等,读者可以通过学习这些案例来深入理解Shell脚本的应用场景和技巧。此外,书中还提供了练习题和实战项目,读者可以通过完成这些练习和项目来巩固和应用所学知识。 总的来说,《Shell脚本实战》是一本实用性很强的电子书,适合那些希望通过实践来提升Shell脚本编程能力的读者。无论是作为初学者的入门教材,还是作为有经验的开发人员的参考书,都具有一定的价值。读者可以通过阅读这本书,掌握Shell脚本编程的基础知识和技巧,提高自己的工作效率和编程水平。 ### 回答3: Shell脚本实战电子版是一本关于Shell脚本编程的实用指南。它适合编程新手和有一定编程基础的人士学习和使用。 本书首先介绍了Shell脚本的基础知识,包括Shell环境、Shell脚本的基本语法和变量、运算符、流程控制等内容。然后,书中以实际案例为例,详细介绍了如何使用Shell脚本解决各种问题。 在本书中,读者将学习如何创建和运行Shell脚本、使用Shell脚本进行文件和目录操作、处理文本文件和日志文件、在Shell脚本中使用正则表达式和通配符等技巧。此外,本书还介绍了如何使用Shell脚本编写简单的网络脚本、检测系统性能和使用Shell脚本进行系统管理等内容。 Shell脚本实战电子版内容丰富,实用性强。通过学习本书,读者可以有效地利用Shell脚本提高工作效率,解决实际问题。无论是在Linux、Unix还是Mac系统中,Shell脚本都是一种非常强大和灵活的工具,可以节省时间和精力。 总之,Shell脚本实战电子版是一本非常实用的书籍,对于希望学习和使用Shell脚本的读者来说,它是不可多得的参考资料。无论是初学者还是有一定经验的程序员,都可以从中获得实用的技巧和知识,提升自己的编程能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小赵小赵福星高照~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值