linux常用指令和shell脚本


一、虚拟机的安装

    略过可以用vamware或者vgrant,vgrant可以参考博主的谷粒商城中的安装

二、linux常用基础命令

 2.1文件查看相关

2.1.1pwd 列出当前目录的路径

查看当前所在的路径

[root@localhost ~]# pwd

2.1.2 ls 列出当前目录下的所有文件

[root@localhost ~]# ls
[root@VM-0-16-centos ~]# ll -a
总用量 200
dr-xr-x---. 16 root root  4096 913 15:11 .
dr-xr-xr-x. 21 root root  4096 913 15:48 ..
-rw-r--r--   1 root root  7824 530 18:51 \
-rw-------   1 root root 81667 914 04:59 .bash_history
-rw-r--r--.  1 root root    18 1229 2013 .bash_logout
-rw-r--r--.  1 root root

ll --help 查看ls用法,–help是一个帮助命令

 2.2创建、重命名文件\文件夹

#创建一个空文件
[root@VM-0-16-centos test]# touch hello.txt
[root@VM-0-16-centos test]# ll
总用量 0
-rw-r--r-- 1 root root 0 914 05:01 hello.txt
[root@VM-0-16-centos test]# 


#mkdir -p 目标目录存在也不报错
[root@localhost ~]# mkdir  -p  abc


# mv重命名文件\文件夹
[root@VM-0-16-centos test]# ll
总用量 0
-rw-r--r-- 1 root root 0 914 05:01 xixi.txt
[root@VM-0-16-centos test]# 

 2.3 链接文件

#linux有两种链接:硬链接、符号(软)链接,简单理解就是软连接相当于快捷方式,硬链接相当于复制。注意不能对目录使用硬链接
[root@VM-0-16-centos test]#  ln hello.txt hlink
[root@VM-0-16-centos test]# ll
总用量 0
-rw-r--r-- 2 root root 0 914 05:07 hello.txt
-rw-r--r-- 2 root root 0 914 05:07 hlink
-rw-r--r-- 1 root root 0 914 05:01 xixi.txt
[root@VM-0-16-centos test]# 

#软连接
[root@VM-0-16-centos test]# ln -s hello.txt vlink
[root@VM-0-16-centos test]# ll
总用量 0
-rw-r--r-- 2 root root 0 914 05:07 hello.txt
-rw-r--r-- 2 root root 0 914 05:07 hlink
lrwxrwxrwx 1 root root 9 914 05:08 vlink -> hello.txt
-rw-r--r-- 1 root root 0 914 05:01 xixi.txt


 2.4 切换目录

cd … 去上一级目录
cd / 去根目录
通过在cd后面指定目录,可以切换到指定目录
cd ~ 去当前用户主()目录
cd xxx/xxx 直接跳转到某个目录

 2.5删除文件\文件夹(目录)

[root@VM-0-16-centos test]# rm hlink
rm:是否删除普通空文件 "hlink"?y
[root@VM-0-16-centos test]# ll
总用量 0
-rw-r--r-- 1 root root 0 914 05:07 hello.txt
lrwxrwxrwx 1 root root 9 914 05:08 vlink -> hello.txt
-rw-r--r-- 1 root root 0 914 05:01 xixi.txt
[root@VM-0-16-centos test]#

# 删除目录需要指定r参数,否则会提示不能删除
# r是给rm加入递归(recursion)特性,也就是目标为文件夹时删除文件夹下所有数据
# rm -f 强制删除
# f给rm加入强制(force)特性,也就是遇到删除时不需要询问即可直接删除
[root@VM-0-16-centos test]# mkdir test2
[root@VM-0-16-centos test]# ll -a
总用量 12
drwxr-xr-x   3 root root 4096 914 05:15 .
dr-xr-x---. 17 root root 4096 914 05:00 ..
-rw-r--r--   1 root root    0 914 05:07 hello.txt
drwxr-xr-x   2 root root 4096 914 05:15 test2
lrwxrwxrwx   1 root root    9 914 05:08 vlink -> hello.txt
-rw-r--r--   1 root root    0 914 05:01 xixi.txt
[root@VM-0-16-centos test]# rm -rf test2/
[root@VM-0-16-centos test]# ll -a
总用量 8
drwxr-xr-x   2 root root 4096 914 05:15 .
dr-xr-x---. 17 root root 4096 914 05:00 ..
-rw-r--r--   1 root root    0 914 05:07 hello.txt
lrwxrwxrwx   1 root root    9 914 05:08 vlink -> hello.txt
-rw-r--r--   1 root root    0 914 05:01 xixi.txt
[root@VM-0-16-centos test]# 

 2.6复制\粘贴\剪切

[root@VM-0-16-centos test]# cp xixi.txt xixicopy.txt
[root@VM-0-16-centos test]# ll -a
总用量 12
drwxr-xr-x   3 root root 4096 914 05:19 .
dr-xr-x---. 17 root root 4096 914 05:00 ..
-rw-r--r--   1 root root    0 914 05:07 hello.txt
drwxr-xr-x   2 root root 4096 914 05:19 my
lrwxrwxrwx   1 root root    9 914 05:08 vlink -> hello.txt
-rw-r--r--   1 root root    0 914 05:19 xixicopy.txt
-rw-r--r--   1 root root    0 914 05:01 xixi.txt


# cp -r 复制&粘贴文件或目录 复制目录,需要指定r参数

[root@VM-0-16-centos test]# cp -r my my2
[root@VM-0-16-centos test]# ll -a
总用量 16
drwxr-xr-x   4 root root 4096 914 05:21 .
dr-xr-x---. 17 root root 4096 914 05:00 ..
-rw-r--r--   1 root root    0 914 05:07 hello.txt
drwxr-xr-x   2 root root 4096 914 05:19 my
drwxr-xr-x   2 root root 4096 914 05:21 my2
lrwxrwxrwx   1 root root    9 914 05:08 vlink -> hello.txt
-rw-r--r--   1 root root    0 914 05:19 xixicopy.txt
-rw-r--r--   1 root root    0 914 05:01 xixi.txt

# 移动命令
[root@localhost ~]# mv xyz abc

# scp命令用于在网络中不同主机之间复制文件或目录。
显示进度在scp后添加-v
复制目录在scp后添加-r
静默复制模式在scp后添加-q
[root@localhost ~]# scp /root/hello.txt 192.168.182.130:/root/

 2.7文件属性

在这里插入图片描述

  • 第一段:权限字符每3个一组(rwx),读(r)、写(w)、执行(x)第一组:文件所有者的权限是读、写和执行
    第二组:与文件所有者同一组的用户的权限
    第三组:不与文件所有者同组的其他用户的权限
    也可用数字表示为:r=4,w=2,x=1,如:权限6可以表示为r+w=6
  • 第二段:目录/链接个数
    对于目录文件,表示它的第一级子目录的个数
  • 第三段:所属用户
  • 第四段:所属组
  • 第五段:文件大小(字节)
  • 第六段:最后修改时间
  • 第七段:文件\文件夹名称

 2.8分配权限

# chmod u+x xxx.txt 给当前所有者添加执行权限
[root@VM-0-16-centos test]# chmod u+x hello.txt 
[root@VM-0-16-centos test]# ll
总用量 8
-rwxr--r-- 1 root root    0 914 05:07 hello.txt
drwxr-xr-x 2 root root 4096 914 05:19 my
drwxr-xr-x 2 root root 4096 914 05:21 my2
lrwxrwxrwx 1 root root    9 914 05:08 vlink -> hello.txt
-rw-r--r-- 1 root root    0 914 05:19 xixicopy.txt
-rw-r--r-- 1 root root    0 914 05:01 xixi.txt

# chmod 777 xxx.txt 添加rwxrwxrwx权限
给hello.txt添加777权限
[root@VM-0-16-centos test]# chmod 777 hello.txt

chmod -R 777 xxx 给指定目录递归添加rwxrwxrwx权限
给abc目录及其子目录中的所有内容添加777权限
[root@VM-0-16-centos test]# chmod -R 777 abc

 2.9 内容查看

#cat 显示文本内容,类似windows中的type(顺序输出)
[root@192 clickhouse-server]# cat users.xml 
<?xml version="1.0"?>
<yandex>
    <!-- Profiles of settings. -->
cat -b 显示行号输出
用一次显示一屏,没有显示完时最后一行显示进度。回车显示下一行,按b显示上一页,空格显示下一
页,q退出

 2.10 压缩、解压

  • -z 是否同时具有 gzip 的属性?亦即是否需要用 gzip 压缩?
  • -c 创建一个压缩文件的参数指令(create 的意思);
  • -x 解开一个压缩文件的参数指令!
  • f 使用档案名字,这个参数是最后一个参数,后面只能接档案名!
tar -zcvf 打包及压缩(gzip方式)
tar -zxvf 解压(gzip包)

 2.11 输出显示


echo:将内容输出到设备,类似java里面的system.out.println()
常见用法:
echo “hello\t\t world!” 不解析转义字符
echo -e “hello\t\t world!” 解析转义字符
echo $PATH 输出环境变量
注意:在打印变量信息的时候,使用echo ${PATH} 也可以,效果是一样的

 2.12 软件的安装和卸载

第一种:压缩包安装方式,直接解压,配置相应的环境变量即可使用
第二种:在线安装,使用yum
yum集成了连接网络,软件安装,删除,更新等功能,yum在配置好repo后,机器只要连网,就能智能
化安装软件,使用yum 安装的好处在于可以自动安装软件需要的依赖包
安装
yum install -y 安装
升级
yum update 不跟则更新全部
查找和显示
yum info 显示包信息
yum list 不跟则显示已安装或可安装包
删除程序
yum remove 
清除缓存
yum clean all 清除所有缓存(包含文件、旧软件)

 2.13 查看操作历史和磁盘,内存使用

history保留了最近执行的命令记录,默认可以保留1000。
历史清单从0开始编号到最大值。
常见用法:
history N 显示最近N条命令
history -c 清除所有的历史记录
history -w xxx.txt 保存历史记录到文本xxx.txt

使用df命令查看硬盘使用情况

查看内存使用情况
free 查看内存和交换空间的使用情况
常见用法:
free -m:显示内存单位为MB
free -h:根据值的大小,显示易于识别的单位

关机重启快捷命令
shutdown -h now 关机
reboot -h now 重启
exit 退出当前登录状态

三、linux常用高级命令

 3.1 vi编辑器相关

  • 查找字符串:在命令模式下,输入/,然后再输入你想要查询的字符串,最后按回车键就可以进行查询了
  • 查找某一行内容:按shift和:,在这里输入具体的行号然后按回车即可,在这里我们希望跳转到第10行。如果不知道行号,它里面有一个快捷方式可以显示出来行号,先按shift和: 然后输入 set nu 这个时候就可以看到文件中显示了行号
  • 复制粘贴:把光标移动到希望复制的那一行内容上面,然后连按yy,这样就把这一行内容复制上了,然后按p就会把刚才复制的内容粘贴到下一行
  • 快速删除:进入命令模式,把光标定位到想要删除的那一行内容上面,连按dd,就可以删除当前行的内容,如果想要清空当前行下的所有内容,先连按999,然后再连按dd,这样就可以清空光标所在行下的所有内容了。
  • 快速跳到文件首行和末行:在命令模式下,通过大写的G可以快速将光
    标移动到最后一行。在命令模式下输入小写的gg即可快速跳转到第一
    行。
    ps: 如果我们修改了未保存就关闭了 会出现下次打卡会有警告产生了临时文件,解决这个问题最直接最暴力的方式就是找到这个临时文件,把它干掉就可以一劳永逸了。ll -a 找到.swap的临时文件

 3.2 文件内容统计相关命令

  • wc:统计
[root@192 clickhouse-server]# wc -c hello.txt 
77 hello.txt
[root@192 clickhouse-server]# wc -m hello.txt
77 hello.txt
[root@192 clickhouse-server]# wc -l hello.tx
wc: hello.tx: 没有那个文件或目录
[root@192 clickhouse-server]# wc -l hello.txt
5 hello.txt

这里面的-c是表示获取文件内容的字节数量
-m表示获取字符数量
-l表示是行数
-L是获取最长的一行内容的长度
-w 表示文件中单词的个数,默认使用空白符切割

ps:wc -c 和-m返回的都是78, 但是wc -L返回的是12,也就是说最长的一行内容是12个字符,一共有6行,126=72,这个值和wc -m返 回的值不相等,主要原因是wc -m在统计的时候把每行后面默认的换行符也统计到了,但是wc -L统计的时候没有包含换行符,所以 126+6 = 78

  • uniq:检查重复的行列

[root@localhost ~]# uniq hello.txt
hello world!

-c参数,这个参数表示在输出行的前面加上数据在文件中重复出现的次数
-u 返回不重复的行
[root@localhost ~]# uniq -c hello.txt
[root@localhost ~]# uniq -u hello.txt

去重
[root@localhost ~]# sort test.txt | uniq
  • head:取前N条数据
[root@localhost ~]# cat num.txt 
3
2
9
10
1
[root@localhost ~]# head -3 num.txt 
3
2
9

[root@localhost ~]# sort -nr num.txt 
10
9
3
2
1
#和sort搭配使用
[root@localhost ~]# sort -nr num.txt | head -3
10
9
3

 3.3 日期相关

# date命令默认获取系统当前时间
[root@192 clickhouse-server]# date
2021年 09月 14日 星期二 03:50:01 EDT

#格式化日期
[root@192 clickhouse-server]# date +"%Y-%m-%d %H:%M: %S"
2021-09-14 03:49: 39

#获取时间戳
# 这里面的%s表示获取自1970-01-01 00:00:00以来的秒数 不支持毫秒
[root@192 clickhouse-server]# date +%s
1631605844
# 转化为毫秒
[root@192 clickhouse-server]# date +%s"000"
1631605988000

#指定时间
[root@192 clickhouse-server]# date --date="2026-01-01 00:00:00"
2026年 01月 01日 星期四 00:00:00 EST
#获取指定时间的时间戳
[root@192 clickhouse-server]# date --date="2026-01-01 00:00:00" +%s
1767243600

#获取时间的前几天,这个需求也需要使用–date参数实现 date --date=“1 days ago”
[root@192 clickhouse-server]#  date --date="1 days ago"
2021年 09月 13日 星期一 03:55:59 EDT

#获取一个月多少天,没有直接方法先指定今年的3月1日,然后再获取前一天的时间,对返回的数据进行格式化,只返回day即可。
[root@192 clickhouse-server]# date --date="2026-03-01 1 days ago" +%d
28

 3.4 进程相关

  • ps:显示进程信息
  • netstat:显示端口信息
  • jps:显示Java进程信息
  • top:动态监控进程信息
[root@192 clickhouse-server]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 01:41 ?        00:00:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root          2      0  0 01:41 ?        00:00:00 [kthreadd]

#配合管道查询
[root@192 clickhouse-server]# ps -ef | grep java
root      11129   7419  0 04:00 pts/0    00:00:00 grep --color=auto java

#netstat的常见用法是 netstat -anp需要安装 yum install -y net-tools
# 我们可以借助这个查看端口的占用情况
[root@192 clickhouse-server]# netstat -anp | grep 22
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7143/sshd           
tcp        0      0 192.168.174.133:22      192.168.174.1:51815     ESTABLISHED 7432/sshd: root@not 
tcp        0     52 192.168.174.133:22      192.168.174.1:51811     ESTABLISHED 7415/sshd: root@pts 
tcp        0      0 192.168.174.133:22      192.168.174.1:64394     ESTABLISHED 11346/sshd: root@no 
tcp6       0      0 :::22                   :::*                    LISTEN      7143/sshd           
unix  2      [ ]         DGRAM                    22337    3128/systemd-udevd   
unix  3      [ ]         STREAM     CONNECTED     22260    3128/systemd-udevd   
unix  3      [ ]         DGRAM                    22365    3128/systemd-udevd   
unix  3      [ ]         STREAM     CONNECTED     22138    3120/lvmetad         
unix  3      [ ]         STREAM     CONNECTED     22261    1/systemd            /run/systemd/journal/stdout
unix  3      [ ]         DGRAM                    22366    3128/systemd-udevd   
unix  3      [ ]         STREAM     CONNECTED     22139    1/systemd            /run/systemd/journal/stdout

#jps:类似ps命令,不同的是ps是用来显示所有进程信息的,而jps只显示Java进程

# top命令的使用

# kill -9 pid杀掉进程

 3.5 三剑客(grep,sed,awk)

  • grep:查找
  • sed:编辑
  • awk:分析
  1. grep
# grep 常用于查找文件里符合条件的字符串 支持正则表达式

[root@192 clickhouse-server]# cat hello.txt 
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
cat cc
[root@192 clickhouse-server]# grep cat hello.txt 
cat cc
[root@192 clickhouse-server]# grep ^c hello.txt 
cat cc

# -i忽略大小写
[root@192 clickhouse-server]# grep -i ABC hello.txt 

# -n快速定位到行
[root@192 clickhouse-server]# grep -i -n ABC hello.txt 

#过滤其本身
[root@localhost ~]# ps -ef | grep java | grep -v grep
  1. sed(批量修改文件)
# 比如想在第三行添加一段话
[root@192 clickhouse-server]# sed '2a\haha' hello.txt 
hello world!
hello world!
haha
hello world!
hello world!
hello world!
hello world!
cat cc

#sed ‘1a\haha’ hello.txt 此操作会将数据添加到第一行下面(也就是第二行的位置)
#sed ‘0a\haha’ hello.txt 此操作会报错,行号是从1开始的 我们可以用-i参数
[root@localhost ~]# sed '1i\haha' hello.txt

# 往最后一行添加数据
[root@localhost ~]# sed '$i\haha' hello.txt

# 删除第七行的数据
[root@localhost ~]# sed '7d' hello.txt
[root@localhost ~]# sed '$d' hello.txt

 sed ‘s/l/a/1’ hello.txt
sed后面的参数格式为[address]s/pattern/replacement/flags
这里的address 表示指定要操作的具体行,是一个可选项,s 表示替换操作,pattern 指的是需要替换的内容,replacement 指的是要替换的新内容,flags有多种
第一种就是flags可以表示为1~512之间的任意一个数字,表示指定要替换的字符串在这一行中出现第几
次时才进行替换
第二种就是flags可以直接表示为g,这样的意思就是对每一行数据中所有匹配到的内容全部进行替换
如果flags位置的值为空,则只会在第一次匹配成功时做替换操做
[root@localhost ~]# sed 's/l/a/1' hello.txt 
healo world!
healo world!
healo world!
healo world!
healo world!
healo world!
abc

#替换指定行
[root@localhost ~]# sed -i '61s/127.0.0.1/192.168.182.130/1' redis.conf
  1. awk
awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时, 显得尤为强大,简单来说awk就是把文件逐行的读入,以空白字符为默认分隔符将每行内容切片,
切开的部分再进行各种分析处理。
awk的基本格式:awk [option] programe file
这里的option是一个可选项,一般在这里来指定文件中数据的字段分隔符
programe 是具体的处理逻辑
file表示我们要操作的文件

$1表示是文本中的第1个数据字段
$2表示是文本中的第2个数据字段
以此类推。
还有一个特殊的 $0 它代表整个文本行的内容
[root@localhost ~]# cat hello.txt 
hello world!
heaao worad!
hello world!
hello world!
hello world!
hello world!
abc
[root@localhost ~]# awk '{print $1}' hello.txt 
hello
heaao
hello
hello
hello
hello
abc
[root@localhost ~]# awk '{print $2}' hello.txt 
world!
worad!
world!
world!
world!
world!

四、shell脚本

 4.1 常用配置相关

 4.1.1 配置静态ip

  • 问题:我们在用xshell连接虚拟机的时候很有可能一关机,在开机虚拟机的ip地址就变了,因为现在的ip地址是自动获取的,理论上来说,Linux虚拟机每次 重启之后都可能会重新获取到新的ip地址。
  • /etc/sysconfig/network-scripts/ifcfg-ens33 初始配置如下图
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="9a0df9ec-a85f-40bd-9362-ebe134b7a100"
DEVICE="ens33"
ONBOOT="yes"
  • 修改BOOTPROTO="static"

  • 新增三个配置

IPADDR=192.168.174.100
GATEWAY=192.168.174.2
DNS1=192.168.174.2
  • 最后一步重启配置 service network restart
    ps:192.168.182都是取自虚拟机中虚拟网络编辑器中子网地址的值,最后的100值可以取3~254之间的任意一个数值,关于上面最后三个值的设置 大家可以根据自己的实际情况来
    在这里插入图片描述
    在这里插入图片描述
  • GATEWAY的值是取自虚拟网络编辑器中NAT设置里面的网关的值。
    在这里插入图片描述

 4.1.2 设置别名

       针对hostname的设置分为两种,一种是临时设置,立刻生效,但是机器重启之后就失效了。还有一种是永久设置,但需要重启之后才生效。
所以在实际工作中这两个要结合起来使用,临时+永久设置就可以实现立刻生效、永久有效。

[root@192 ~]# hostname
192.168.174.100

#第一种立即生效 但是重启就没了
[root@192 ~]# hostname lujunstudy
[root@192 ~]# hostname
lujunstudy

#直接修改/etc/hostname文件(永久生效)
[root@192 ~]# vi /etc/hostname
[root@192 ~]# cat /etc/hostname
lujunstudy
 reboot -h now #重启机器 发现变了
 [root@lujunstudy ~]# 

 4.1.3防火墙相关

查看防火墙状态

firewall-cmd    --state

关闭防火墙

systemctl  stop   firewalld.service


开启防火墙

systemctl  start   firewalld.service


禁止开机启动启动防火墙

systemctl   disable   firewalld.service

 4.2 shell脚本

 4.2.1 helloworld

  • shell是什么?

    前面我们学习了Linux中的一些常见命令的使用,这些命令可以在命令行里执行,这个命令行其实就可以称之为shell,我们可以定义一些列的命令集合去干一些事情

#!/bin/bash #声明
echo hello world

[root@lujunstudy myshell]# sh hello.sh 
hello world
[root@lujunstudy myshell]# 

#如果我们在用 ./hello.sh就会发现没有权限 可以用chmod u+x赋权
[root@lujunstudy myshell]# ./hello.sh 
-bash: ./hello.sh: 权限不够

[root@lujunstudy myshell]# chmod u+x hello.sh
[root@lujunstudy myshell]# ./hello.sh 
hello world

# 直接指定的文件名称,前面没有带任何路径信息,那么按照linux的 查找规则,它会到PATH这个环境变量中指定的路径里面查找
[root@lujunstudy myshell]# hello.sh 
-bash: hello.sh: 未找到命令

#看一下环境变量 如果我们需要直接名字来运行可以这样修改,
# 编辑/etc/profile文件,在最后一行添加export PATH=.:$PATH,保存文件即可
[root@lujunstudy myshell]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
#刷新配置
[root@lujunstudy myshell]#  source /etc/profile
[root@lujunstudy myshell]# hello.sh 
hello world

# bash的调试我们可以简单里面为debugger +表示下一步要执行的
[root@lujunstudy myshell]#  bash -x hello.sh
+ echo hello world
hello world

 4.2.2 shell中的变量

ps:只能使用数字、字母和下划线,且不能以数字开头,赋值是通过"="进行赋值,在变量、等号和值之间不能出现空格!

[root@lujunstudy myshell]# name=lujun

# 打印变量通过echo $和${}唯一区别就是字符拼接的时候
[root@lujunstudy myshell]# echo $name
name
[root@lujunstudy myshell]# echo ${name}heh
lujunheh
[root@lujunstudy myshell]# echo $nameheh

变量的分类

  • 本地变量
  • 环境变量
  • 位置变量
  • 特殊变量
  1. 本地变量的格式是VAR_NAME=VALUE,就是上面例子的,这种变量一般 用于在shell脚本中定义一些临时变量,只对当前shell进程有效。对shell进程的子进程和其它shell进程无效
# 什么是子进程呢?我们可以开两个窗口 通过pstree
[root@lujunstudy myshell]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─agetty
        ├─auditd───{auditd}
        ├─chronyd
        ├─clckhouse-watch───clickhouse-serv───156*[{clickhouse-serv}]
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───6*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd─┬─sshd───2*[bash]
        │      ├─sshd─┬─bash───pstree
        │      │      ├─bash───top
        │      │      └─bash───sleep
        │      ├─2*[sshd───6*[sftp-server]]
        │      └─sshd─┬─bash
        │             ├─bash───top
        │             └─bash───sleep
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─vmtoolsd

# 也可以直接进到这个子线程 输出名字发现是没有的
[root@lujunstudy myshell]# bash
[root@lujunstudy myshell]# echo $name

  1. 环境变量
    这里的环境变量类似于windows中的环境变量,例如在windows中设 置JAVA_HOME环境变量它的格式为:export VAR_NAME=VALUE你关闭当前shell进程之后环境变量就消失了,还有就是对子shell进程有效,对其它shell进程无效 永久的办法就是添加到/etc/profile文件中,这样可以保证对所有用户都生效
    vi /etc/profile

    export age=18
[root@lujunstudy myshell]# exit
exit
[root@lujunstudy myshell]# export age=18
[root@lujunstudy myshell]# echo $age
18
[root@lujunstudy myshell]# bash
[root@lujunstudy myshell]# echo $age
18

  1. 位置变量
        在写shell脚本的时候我们想写参数的时候
#!/bin/bash
echo hello world
echo $0
echo $1
# 发现0是这个脚本的名称
[root@lujunstudy myshell]# sh hello.sh a b
hello world
hello.sh
a


  1. 特殊变量
# $? 标识上条令的返回状态码,状态码在0~255之间
[root@lujunstudy myshell]# ll
总用量 4
-rwxr--r--. 1 root root 45 915 02:32 hello.sh
[root@lujunstudy myshell]# echo $?
0
[root@lujunstudy myshell]# slsls
bash: slsls: 未找到命令
[root@lujunstudy myshell]# echo $?
127

#$#标识 shell脚本的所有参数的个数

#变量和引号的使用 单引号不解析变量,双引号解析变量

#反引号是执行并引用命令的执行结果,在这里反引号是获取到了name变量的值,然后去执行
[root@lujunstudy myshell]# name=lujun
[root@lujunstudy myshell]# echo `$name`
bash: lujun: 未找到命令
[root@lujunstudy myshell]# name=pwd
[root@lujunstudy myshell]# echo `$name`
/root/myshell

 4.2.3 shell中的循环和判断

  • 循环
 基本语法
 1	for((i=0;i<10;i++))
 	do
 	循环体
 	done
 2 for i in 1 2 3
	do
	循环体...
	done
 3 	whlie 测试条件
 	条件两个格式字符和整数 整数用gt lt 字符用=或者!=
	do
	循环体...
	done
#!/bin/bash
while test 2 -gt 1
do
echo yes
sleep 1
done


#!/bin/bash
echo hello world
for((i=0;i<10;i++))
do
echo xixi
done

[root@lujunstudy myshell]# sh hello.sh 
hello world
xixi
xixi
xixi
xixi
xixi
xixi
xixi
xixi
xixi

  • 判断
     - 单分支
if 测试条件
then
 选择分支
fi

#!/bin/bash
flag=$1
if [ $flag -eq 1 ]
then
echo one
fi

 - 双分支

if 测试条件
then
 选择分支1
else
 选择分支2
fi
#!/bin/bash
if [ $# -lt 1 ]
then
 echo "not found param"
 exit 100
fi

flag=$1
if [ $flag -eq 1 ]
then
 echo "one"
else
 echo "not support"
fi

 - 多分支

if 测试条件1
then
 选择分支1
elif 测试条件2
then
 选择分支2
 ...
else
 选择分支n
fi

 4.2.4 shell的常见场景

  • 想后台启动shell [root@bigdata01 shell]# sh while2.sh & 但是这样退出窗口的话会停止
  • [root@bigdata01 shell]# nohup sh while2.sh & 使用nohup就可以避免了
  • 标准输出、标准错误输出和重定向
  • 标准输出:表示是命令或者程序输出的正常信息
  • 标准错误输出:表示是命令或者程序输出的错误信息
[root@lujunstudy myshell]# ll
总用量 4
-rwxr--r--. 1 root root 99 915 02:52 hello.sh
[root@lujunstudy myshell]# lss
bash: lss: 未找到命令
[root@lujunstudy myshell]# 

这里的ll执行成功了,所以下面输出的信息就是标准输出
这里的lss是一个不存在的命令,执行失败了,所以下面输出的信息就是标准错误输出
标准输出可以使用文件描述符1来表示,标准错误输出可以使用文件描述符2来表示
我们可以吧这些输出信息保存到文件里面去
# 注意>是覆盖操作 追加 用>>
[root@lujunstudy myshell]# ll 1>a.txt
[root@lujunstudy myshell]# ll
总用量 8
-rw-r--r--. 1 root root 111 915 03:03 a.txt
-rwxr--r--. 1 root root  99 915 02:52 hello.sh
[root@lujunstudy myshell]# cat a.txt 
总用量 4
-rw-r--r--. 1 root root  0 915 03:03 a.txt
-rwxr--r--. 1 root root 99 915 02:52 hello.sh
[root@lujunstudy myshell]# nohup hello.sh >/dev/null 2>&1&

nohup和&:可以让程序一直在后台运行
/dev/null:是linux中的黑洞,任何数据扔进去都找不到了
>/dev/null:把标准输出重定向到黑洞中,表示脚本的输出信息不需要存储
2>&1 :表示是把标准错误输出重定向到标准输出中
并且把脚本的所有输出都扔到黑洞里面
  • 定时器
crontab的格式是这样的: * * * * * user-name command
ps:crontab的日志在/var/log/cron文件中,
*:分钟(0-59)
*:小时(0-23)
*:一个月中的第几天(1-31)
*:月份(1-12)
*:星期几(0-7) (星期天为0)
user-name:用户名,用哪个用户执行
command:具体需要指定的命令

查看crontab服务状态:systemctl status crond 确定服务ok我们就可以操作了
[root@lujunstudy myshell]#  vi /etc/crontab

案例:每隔1分钟打印一次当前时间,时间格式为年月日 时分秒
这个需求需要写到脚本中,然后在crontab中直接调用脚本即可。
#!/bin/bash
showTime=`date "+%Y-%m-%d %H:%M:%S"`
echo $showTime

# 然后在/etc/crontab文件中配置
* * * * * root sh /root/shell/showTime.sh
# 上面这样只是执行了 我们看不到执行结果
* * * * * root sh /root/shell/showTime.sh >> /root/shell/mylog.log
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值