shell脚本常用命令

shell概述

shell是一个命令行解释器,它接收应用程序/用户命令,然后调用操作系统内核。
shell还是一个功能强大的编程语言,易编写、易调试、灵活性强。
在这里插入图片描述

shell解析器

  1. 查看系统自带的所有shell解析器
    cat /etc/shells
  2. 查看系统默认的shell解析器
    echo $SHELL

shell脚本入门

  1. 脚本格式
    脚本以#!/bin/bash作为第一行(指定shell解析器)
  2. shell脚本 hello world
#!/bin/bash
echo "hello world"
  1. 脚本执行方式
# 方式1
mayanan@debian-01:~/test/test_shell$ bash helloworld.sh
hello world

# 方式2
mayanan@debian-01:~/test/test_shell$ ./helloworld.sh
-bash: ./helloworld.sh: Permission denied

方式1本质上是bash解析器帮我们执行脚本,所以脚本本身不需要执行权限;方式2本质上是脚本自己需要执行,所以需要执行权限。

  1. shell脚本,多命令处理
    在linux家目录下创建一个banzhang.txt文件,并在文件中写入"I Love You"字符
#!/usr/bin/env bahs
cd /home/mayanan
touch banzhang.txt
echo "I Love You" >> banzhang.txt

shell中的变量

  1. 常用系统变量
    $HOEM $PWD $SHELL $USER
mayanan@debian-01:~/test/test_shell$ echo $HOME
/home/mayanan
mayanan@debian-01:~/test/test_shell$ echo $PWD
/home/mayanan/test/test_shell
mayanan@debian-01:~/test/test_shell$ echo $USER
mayanan
mayanan@debian-01:~/test/test_shell$ echo $SHELL
/bin/bash
  1. 自定义变量
  • 基本语法
    (1)定义变量:变量=值,等号两边不能留有空格
    (2)撤销变量:unset 变量
    (3)输出变量:echo $变量
    (4)声明静态变量:readonly 变量,注意:不能unset
mayanan@debian-01:~/test/test_shell$ A=1
mayanan@debian-01:~/test/test_shell$ echo $A
1
mayanan@debian-01:~/test/test_shell$ unset A
  1. 变量定义规则
    (1)变量名称可以以字母、数字、下划线组成,不能以数字开头,环境变量名建议大写
    (2)等号两侧不能有空格
    (3)在bash中,变量默认类型都是字符串类型,无法直接进行数值运算
    (4)变量的值如果有空格,需要使用双引号或者单引号括起来
mayanan@debian-01:~/test/test_shell$ D="A B  C"
mayanan@debian-01:~/test/test_shell$ echo $D
A B C

(5)可把变量提升为全局变量,可供其它shell程序使用
export 变量

特殊变量

  1. $n (描述:n为数字,$0代表脚本名称,10以内参数用$1-9 表 示 , 10 以 上 的 需 要 用 大 括 号 包 含 {10})
#!/bin/bash
echo "$0 $1 $2 $3"
  1. $# (功能描述:获取所有输入参数个数,常用于循环)
#!/bin/bash
echo "$0 $1 $2 $3"
echo $#
  1. ∗ 和 *和 @
  • $* (描述:代表命令行中所有的参数,把所有参数看成一个整体)
  • $@ (描述:也代表命令行中所有的参数,不过把每个参数区分对待)
mayanan@debian-01:~/test/test_shell$ ./param.sh aa 11 bb
./param.sh aa 11 bb
3
aa 11 bb
aa 11 bb
  1. $?
    $? (描述:最后一次执行命令的状态,0:正确执行)

运算符

  1. $((运算式)) 或 $[运算式]
  2. expr +,-,*,/,% 加减乘除取余
    expr运算符间要有空格
mayanan@debian-01:~/test/test_shell$ s=$[(2+3)*4]
mayanan@debian-01:~/test/test_shell$ echo $s
20
mayanan@debian-01:~/test/test_shell$ expr 2 + 3
5
mayanan@debian-01:~/test/test_shell$ expr `expr 2 + 3` \* 4
20
mayanan@debian-01:~/test/test_shell$ s=$(((2+3)*4))
mayanan@debian-01:~/test/test_shell$ echo $s
20

条件判断

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

  2. 常用判断条件
    两个整数之间比较
    符号 描述
    -lt (less than)小于
    -le (less equal) 小于等于
    -eq (equal)等于
    -gt (greater than) 大于
    -ge (greater equal) 大于等于
    -ne (not equal) 不等于

  3. 文件权限判断

    • -r 有读的权限
    • -w 有写的权限
    • -x 有执行的权限
  4. 文件类型判断

    • -f 文件存在,并且是一个常规文件
    • -e 文件存在
    • -d 文件存在并且是一个目录
# 判断23是否大于3
mayanan@debian-01:~/test/test_shell$ [ 23 -gt 3 ]
mayanan@debian-01:~/test/test_shell$ echo $?
0

# 判断helloworld.sh是否有写入权限
mayanan@debian-01:~/test/test_shell$ [ -w helloworld.sh ]
mayanan@debian-01:~/test/test_shell$ echo $?
0

# 判断目录中文件是否存在
mayanan@debian-01:~/test/test_shell$ [ -e /home/mayanan/test/test_shell ]
mayanan@debian-01:~/test/test_shell$ echo $?
0
  1. 多条件判断
    && ||
mayanan@debian-01:~/test/test_shell$ [ -e /home/mayanan/test ] && echo OK || echo 不ok
OK
mayanan@debian-01:~/test/test_shell$ echo $?
0
mayanan@debian-01:~/test/test_shell$ [ -e /home/mayanan/test02 ] && echo OK || echo 不ok
不ok
mayanan@debian-01:~/test/test_shell$ echo $?
0

流程控制

  1. IF判断
#!/bin/bash
if [ $1 -eq 1 ]
then
        echo "班长真帅"
elif [ $1 -eq 2 ]
then
        echo "班长真丑"
fi

case语句

#!/usr/bin/env bash

case $1 in
1)
        echo 班长
;;
2)
        echo 学习委员
;;
3)
        echo 体育委员
;;
*)
        echo cls
;;
esac

mayanan@debian-01:~/test/test_shell$ bash case.sh 2
学习委员

for循环

  1. 语法1
#!/usr/bin/env bash

s=0
for ((i=1;i<=100;i++))
do
        s=$[$s+$i]
done
echo $s

写可以这样写

#!/usr/bin/env bash

s=0
for ((i=1;i<=100;i++))
do
        s=$(($s+$i))
done
echo $s
  1. 语法2
#!/usr/bin/env bash

for i in $*
do
        echo $i
done

mayanan@debian-01:~/test/test_shell$ vim for2.sh
mayanan@debian-01:~/test/test_shell$ bash for2.sh 11 22 33
11
22
33

当然了, ∗ 换成 *换成 换成@也是可以的:

#!/usr/bin/env bash

for i in $@
do
        echo $i
done

while循环

#!/usr/bin/env bash

s=0
i=1
while [ $i -le 100 ]
do
        s=$[$s+$i]
        i=$[$i+1]
done
echo $s

mayanan@debian-01:~/test/test_shell$ bash while.sh
5050

read读取控制台输入

read(选项)(参数)

  • -p 指定读取值时的提示符
  • -t 指定读取值时等待的时间(秒)
#!/usr/bin/env bash

read -t 7 -p "在7秒内输入你的名字" NAME
echo $NAME

mayanan@debian-01:~/test/test_shell$ bash read.sh
在7秒内输入你的名字mayanan
mayanan

函数

  1. basename
basename [string / pathname] [suffix] (描述:basename命令会删掉所有的前缀包括最后一个‘/’字符,然后将字符串显示出来)
mayanan@debian-01:~/test/test_shell$ basename /home/mayanan/test/test_shell/read.sh
read.sh

mayanan@debian-01:~/test/test_shell$ basename /home/mayanan/test/test_shell/read.sh .sh
read
  1. dirname
dirname 文件绝对路径 (描述:从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然后返回剩下的路径(目录的部分))
mayanan@debian-01:~/test/test_shell$ dirname /home/mayanan/test/test_shell/read.sh
/home/mayanan/test/test_shell

自定义函数

# 格式
[ function ] funname[()]
{
	Action:
	[return int;]
}
funname
#!/usr/bin/env bash

function sum(){
        s=0
        s=$[$1+$2]
        echo $s
}

read -p "input your param1: " P1
read -p "input your param2: " P2
sum $P1 $P2

# mayanan@debian-01:~/test/test_shell$ bash sum.sh
input your param1: 100
input your param2: 200
300

shell工具

  1. cut
    cut命令从文件的每一行剪切字节,字符和字段并将这些字节,字符和字段输出
    cut [选项参数] filename
    • -f 列号,提取第几列
    • -d 分隔符,按照指定分隔符分隔列
mayanan@debian-01:~/test/test_shell$ cat cut.txt
dong shen
guan zhen
wo wo
lai lai
le le
mayanan@debian-01:~/test/test_shell$ cut -d " " -f 2,3 cut.txt
shen
zhen
 wo
 lai
 le

获取第二行第一个单词:

mayanan@debian-01:~/test/test_shell$ cat cut.txt | grep guan | cut -d " " -f 1
guan

选取系统PATH变量,第二个": "开始后的所有路径:

mayanan@debian-01:~/test/test_shell$ echo $PATH | cut -d ":" -f 3-
/usr/bin:/bin:/usr/local/games:/usr/games:/usr/share/pcre:/usr/share/pcre/bin:/usr/local/nginx/sbin

切割ip addr后的ip地址

mayanan@debian-01:~/test/test_shell$ ip addr | grep eth0 | grep inet | cut -d " " -f 6 | cut -d "/" -f 1
172.17.82.242

sed

sed是一种流编辑器,它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,成为“模式空间”,接着sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕,接着处理下一行,这样不断重复,知道文件末尾,文件内容并没有改变,除非你使用重定向存储输出
sed [选项参数] ‘command’ filename
* -e 直接在指令列模式上进行sed的动作编辑
命令功能描述
* a 新增
* d 删除
* s 查找并替换
案例:sed.txt原文件

dong shen
guan zhen
wo wo
lai lai

le le

将 mei nv 这个单词插入到sed.txt文件的第二行下面:

mayanan@debian-01:~/test/test_shell$ sed "2a meinv" sed.txt
dong shen
guan zhen
meinv
wo wo
lai lai

le le

将包含wo的一行删除掉:

mayanan@debian-01:~/test/test_shell$ sed "/wo/d" sed.txt
dong shen
guan zhen
lai lai

le le

将sed.txt文件中wo替换为ni:

mayanan@debian-01:~/test/test_shell$ sed "s/wo/ni/g" sed.txt
dong shen
guan zhen
ni ni
lai lai

le le

注意:加g是全局替换,否则只替换第一个

将sed.txt文件中第二行删除,并将wo替换为ni

mayanan@debian-01:~/test/test_shell$ sed -e "2d" -e "s/wo/ni/g" sed.txt
dong shen
ni ni
lai lai

le le

awk

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

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

pattern: 表示AWK在数据中查找的内容,就是匹配模式
action: 在找到匹配内容时所执行的一系列命令
-F 指定输入文件分隔符
-v 赋值一个用户定义变量

案例实操:

  1. 数据准备
    sudo cp /etc/passwd ./
  2. 搜索passwd文件以root关键字开头的所有行,并输出该行的第7列
mayanan@debian-01:~/test/test_shell$ awk -F : '/^root/ {print $7}' passwd
/bin/bash

切记使用单引号
3. 搜索passwd文件以root关键字开头的所有行,并输出该行的第1列和第7列,中间以 “,” 分割

mayanan@debian-01:~/test/test_shell$ awk -F : '/^root/ {print $1 "," $7}' passwd
root,/bin/bash
  1. 只显示/etc/passwd的第一列和第七列,以逗号分割,且在第一行添加列名user, shell在最后一行添加:“xiaomage, /bin/zuishuai”
mayanan@debian-01:~/test/test_shell$ awk -F : 'BEGIN{print "user, shell"} {print $1 "," $7} END{print "xiaomage, /bin/zuishuai"}' passwd

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

  1. 将passwd文件中的用户id增加数值1并输出
mayanan@debian-01:~/test/test_shell$ awk -F : -v i=1 '{print $3+i}' passwd
1
2
3
4
  1. awk的内置变量
  • FILENAME 文件名
  • NR 已读的记录数
  • NF 浏览记录的域的个数(切割后,列的个数)
    案例1:
mayanan@debian-01:~/test/test_shell$ awk -F : '{print "filename:" FILENAME "NR:" NR "NF:", NF}' passwd
filename:passwdNR:1NF: 7
filename:passwdNR:2NF: 7
filename:passwdNR:3NF: 7
filename:passwdNR:4NF: 7

案例2:切割IP

mayanan@debian-01:~/test/test_shell$ ip addr | grep eth0 | grep inet | awk -F " " '{print $2}' |awk -F / '{print $1}'
172.17.82.242

案例3:查询sed.txt中空行所在的行号

# mayanan@debian-01:~/test/test_shell$ awk '/^$/{print NR}' sed.txt
5

# mayanan@debian-01:~/test/test_shell$ cat sed.txt
dong shen
guan zhen
wo wo
lai lai

le le

sort

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

sourt [选项] (参数)

参数 描述
-n 依照数值大小排序
-r 以相反的顺序排序
-t 设置排序时使用的分隔字符
-k 指定需要排序的列

mayanan@debian-01:~/test/test_shell$ sort -t : -rk 2 sort.sh
xz:50:2.3
bb:40:5.4
ss:30:1.6
bd:20:4.2
cls:10:3.5

企业真实面试题

  1. 使用linux命令查询file1中空行所在的行号
mayanan@debian-01:~/test/test_shell$ awk '/^$/ {print NR}' file1
5
  1. 有文件chengji .txt内容如下:
# mayanan@debian-01:~/test/test_shell$ cat chengji.txt
张三 40
李四 50
王五 60
# mayanan@debian-01:~/test/test_shell$ cat chengji.txt | awk -F " " '{sum+=$2} END{print sum}'
150
  1. shell脚本里如何检查一个文件是否存在?如果不存在,该如何处理
# mayanan@debian-01:~/test/test_shell$ cat file.sh
#!/usr/bin/env bash

if [ -f sed.txt ];then
        echo 文件存在
else
        echo 文件不存在
fi
  1. 用shell写一个脚本,对文本中无序的一系列数字进行排序
mayanan@debian-01:~/test/test_shell$ sort -n test.txt
1
3
5
6
8
9
11
88
  1. 请用shell脚本写出查找当前文件夹(/home)下所有的文本文件内容中包含有字符 “shen” 的文件名称
mayanan@debian-01:~/test/test_shell$ grep -r shen | cut -d ":" -f 1
sed2.txt
cut.txt
  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

专职

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

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

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

打赏作者

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

抵扣说明:

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

余额充值