Shell 文本处理工具
一:cut1.1 简介
cut 的工作就是“剪”,具体的说就是在文件中负责剪切数据用的。
cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段输出。
1.2 基本用法
cut [选项参数] filename
说明:默认分隔符是制表符
1.3 选项参数说明
1.4 案例实操1234567891011121314151617181920212223242526272829[root@hadoop100 shell]# cat cut.txt xiao hongxiao minghello worldbei jing# 按空格切分,取第一列[root@hadoop100 shell]# cut -d " " -f 1 cut.txt xiaoxiaohellobei# 按空格切分,取第二列、第三列[root@hadoop100 shell]# cut -d " " -f 2,3 cut.txt hongmingworldjing# 切割出 world[root@hadoop100 shell]# cat cut.txt | gr ...
Shell 正则表达式
一:概述
正则表达式使用单个字符串来描述、匹配一系列符合某个语法规则的字符串。
通常被用来检索、替换那些符合某个模式的文本。
在 Linux 中,grep, sed,awk 等文本处理工具都支持通过正则表达式进行模式匹配。
二:常规匹配一串不包含特殊字符的正则表达式匹配自身;
123# 匹配所有包含 ghost 的行[root@hadoop100 ~]# cat /etc/passwd | grep ghostghost:x:1000:1000:ghost:/home/ghost:/bin/bash
三:常用特殊字符(1)特殊字符:^
^ 匹配一行的开头
12345# 匹配所有以a开头的行[root@hadoop100 ~]# cat /etc/passwd | grep ^aadm:x:3:4:adm:/var/adm:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologinavahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
(2)特 ...
Shell 函数
一:系统函数1.1 basename(1)基本语法
basename [string / pathname] [suffix]
basename 命令会删掉所有的前缀包括最后一个(‘/’)字符,然后将字符串显示出来。
basename 可以理解为取路径里的文件名称
选项
suffix 为后缀,如果 suffix 被指定了,basename 会将 pathname 或 string 中的 suffix 去掉。
(2)案例实操1234[root@hadoop100 shell]# basename /root/RupertTears/shell/parameter.sh parameter.sh[root@hadoop100 shell]# basename /root/RupertTears/shell/parameter.sh .shparameter
1.2 dirname(1)基本语法
dirname 文件绝对路径
从给定的包含绝对路径的文件名中去除文件名 (非目录的部分),然后返回剩下的路径(目录的部分)
dirname 可以理解为取文件路径的 ...
Shell IO交互
一:read 读取控制台输入1.1 基本语法
read (选项) (参数)
①选项:
-p:指定读取值时的提示符;
-t:指定读取值时等待的时间(秒)如果-t 不加表示一直等待
②参数:
变量:指定读取值的变量名
1.2 案例实操123456789101112131415[root@hadoop100 shell]# cat while.sh #!/bin/bashi=0sum=0while [ $i -le 100 ]do #sum=$[$sum+$i] #i=$[$i+1] let sum+=i let i++done echo $sum[root@hadoop100 shell]# ./read.sh 请在10秒内输入参数: Ghostwelcome Ghost
Shell 流程控制
一:if 判断1.1 基本语法(1)单分支123if [ 条件判断式 ];then 程序fi
1234if [ 条件判断式 ]then 程序fi
(2)多分支123456789if [ 条件判断式 ]then 程序elif [ 条件判断式 ]then 程序else 程序fi
1.2 注意事项
① [ 条件判断式 ],中括号和条件判断式之间必须有空格 ;
② if 后要有空格;
1.3 案例实操1234567891011121314151617181920[root@hadoop100 shell]# cat if.sh #!/bin/bash# 使用字符串的拼接阻止程序报错if [ "$1"x = "1"x ]then echo "num is one"elif [ "$1"x = "2"x ]then echo "num is two"else echo "others"fi[root@hadoop100 shell]# ./if. ...
Shell 条件判断
一:基本语法
test condition
[ condition ](注意 condition 前后要有空格)
注意:条件非空即为 true,[ ] 为空返回 fasle;
二:常用判断条件2.1 两个整数之间比较
-eq 等于(equal)
-ne 不等于(not equal)
-lt 小于(less than)
-le 小于等于(less equal)
-gt 大于(greater than)
-ge 大于等于(greater equal)
注:如果是字符串之间的比较 ,用等号“=”判断相等;用“!=”判断不等。
2.2 按照文件权限进行判断
-r 有读的权限(read)
-w 有写的权限(write)
-x 有执行的权限(execute)
2.3 按照文件类型进行判断
-e 文件存在(existence)
-f 文件存在并且是一个常规的文件(file)
-d 文件存在并且是一个目录(directory)
三:案例实操3.1 数值判断123456[root@hadoop100 shell]# [ 23 -gt 22 ][root ...
Shell 运算符
一:基本语法
$((运算式))
$[运算式]
expr 运算式
二:案例操作123456789[root@hadoop100 shell]# A=$[2+3][root@hadoop100 shell]# echo $A5[root@hadoop100 shell]# A=$((3+3))[root@hadoop100 shell]# echo $A6# 加号两侧要有空格[root@hadoop100 shell]# expr 1 + 23
三:经验总结
expr 中使用 * 时,要转义;例如:a=$(expr 1 * 6)
使用$()或者反引号``,可以执行表达式获取结果;
Shell 变量
一:系统预定义变量1.1 常用系统变量
$USER、$HOME、$PWD、$SHELL等;
使用 env 查看当前系统变量;
使用 set 显示当前 shell 中所有变量;使用 unset + 变量名 清除该变量;
1.2 案例操作12345678[root@hadoop100 shell]# echo $USERroot[root@hadoop100 shell]# echo $HOME/root[root@hadoop100 shell]# echo $SHELL/bin/bash[root@hadoop100 shell]# echo $PWD/root/RupertTears/shell
二:自定义变量2.1 基本语法
定义变量:变量名=变量值,注意“=”的前后不能有空格;
撤销变量:unset + 变量;
声明静态变量:readonly + 变量,不能 unset;
2.2 变量定义规则
变量名称可以由字母、数字和下划线组成,但不能以数字开头,环境变量名称建议大写;
等号两侧不能有空格;
在 bash 中,变量默认为字符串类型,不能进行数值运算;
...
Shell 脚本
一:脚本格式1.1 指定解析器脚本以 #!/bin/bash 开头,指定使用bash解析脚本;
1.2 新建 bash 脚本
创建一个bash脚本,输出 hello world;
123[root@hadoop100 shell]# cat hello.txt #!/bin/bashecho "hello world"
二:执行方式2.1 采用 bash 或 sh + 脚本的相对路径或绝对路径(不用赋予脚本+x 权限)(1)sh + 脚本的相对路径1234[root@hadoop100 shell]# sh ./hello.txt hello world[root@hadoop100 shell]# sh hello.txt hello world
(2)sh + 脚本的绝对路径1234[root@hadoop100 shell]# pwd/root/RupertTears/shell[root@hadoop100 shell]# sh /root/RupertTears/shell/hello.txt hello world
(3)bash + 脚本的相 ...
Shell 概述
一:概述
Shell 是一个命令行解释器,它接收应用程序或者用户命令,然后调用操作系统内核。
Shell 还是一个功能强大的编程语言,易编写、易调试、灵活性强。
二:常识2.1 Linux 提供的 Shell 解释器1234567[root@hadoop100 ~]# cat /etc/shells /bin/sh/bin/bash/usr/bin/sh/usr/bin/bash/bin/tcsh/bin/csh
2.2 bash 和 sh 的关系12345[root@hadoop100 ~]# ls -l /bin/ | grep bash-rwxr-xr-x. 1 root root 964536 4月 1 2020 bashlrwxrwxrwx. 1 root root 10 12月 3 13:16 bashbug -> bashbug-64-rwxr-xr-x. 1 root root 6964 4月 1 2020 bashbug-64lrwxrwxrwx. 1 root root 4 12月 3 ...


