Linux基础(三)

1. 环境变量和shell脚本

环境变量

三种变量:环境变量,自定义变量,系统变量

环境变量可用于 shell 及子 shell,相当于 C 语言中的全局变量
自定义变量只用于当前 shell,相当 C 语言中的自动变量
系统变量是由系统自动生成的,以 $ 打头的变量,$0 表示当前命令,$1 第一个参数,...,另外 $*, $@

farsight@ubuntu:~$ env   # 查询当前 SHELL 的环境变量
...
PWD=/home/farsight      # 当前工作目录
HOME=/home/farsight     # 当前用户的主目录
SHELL=/bin/bash         # 当前的 shell
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin  # 命令的路径
TERM=xterm-256color     # 终端类型
USER=farsight           # 用户
shell脚本

脚本:命令的集合,形成的一个文件。   
写一个脚本 1.sh:

echo "-----------------------------"
echo

ls -l

echo "-----------------------------"
echo

ifconfig ens33

echo "-----------------------------"
echo
farsight@ubuntu:~/test$ chmod a+x 1.sh    # 所有人都有执行权限
farsight@ubuntu:~/test$ ls -l 1.sh 
-rwxrwxr-x 1 farsight farsight 155 7月  14 09:42 1.sh
farsight@ubuntu:~/test$ 1.sh     # 不带路径执行,报错,因为找不到这个文件
1.sh: command not found

如何将 1.sh 变成系统命令?
方法1,复制 1.sh 到 PATH 环境变量的路径下,如 /usr/local/bin 下

farsight@ubuntu:~/test$ ls /usr/local/bin/1.sh     # 验证一下原来是否已存在同名的文件
ls: cannot access '/usr/local/bin/1.sh': No such file or directory
farsight@ubuntu:~/test$ sudo cp 1.sh /usr/local/bin   # 复制 1.sh 到 /usr/local/bin 下面
[sudo] password for farsight: 
farsight@ubuntu:~/test$ ls /usr/local/bin/1.sh     # 验证复制成功
/usr/local/bin/1.sh
farsight@ubuntu:~/test$ 1.sh    # 不带路径执行命令

方法2,将命令的路径放入 PATH 环境变量中。使用 export 命令导出变量为环境变量:

farsight@ubuntu:~/test$ export PATH=$PATH:/home/farsight/test

此方法只在当前终端有效,其他终端无效。为了打开终端即生效,可以将这个命令保存到启动文件中。
启动文件:~/.bashrc,~/.profile。.profile 调用了 .bashrc

farsight@ubuntu:~/test$ vim ~/.bashrc       # 加入 export 命令
farsight@ubuntu:~/test$ source ~/.bashrc    # 让 .bashrc 中的环境变量生效
farsight@ubuntu:~/test$ . ~/.bashrc         # . 是 source 的同义词

2.find,locate,grep

find

以表达式在目录中查找文件

farsight@ubuntu:~$ find . -name "1.sh"      # 在当前目录下按名称查找 1.sh 文件
./test/1.sh
locate

以名称查找文件,locate 不是要磁盘中查找,它在 /var/lib/mlocate/mlocate.db 数据库中查找,此文件会在系统启动时更新一次,之后除非执行 updatedb 命令,否则不会更新。因此有时间效性,即在这个文件更新后新增的文件找不到,删除的文件却能找到。如果要查找最新文件情况,就先执行 updatedb。

farsight@ubuntu:~/test$ locate 3.txt    # 查找 3.txt 文件
grep

文件中搜索满足匹配条件的行

farsight@ubuntu:~/test$ grep -nH "printf" /usr/include/*.h    # 在 /usr/include/ 下的头文件中查找包含 printf 的行,并打印文件名和行号

结合使用 find 和 grep 命令

farsight@ubuntu:~/test$ find /usr/include/ -name "*.h" | xargs grep -nH "printf"   # 典型用法

xargs 命令将 | 管道传过来的文件名作为参数传递给 grep 命令。

3.file,date

file

打印文件类型

farsight@ubuntu:~/test$ file 1.sh
1.sh: ASCII text                          # 文本文件
farsight@ubuntu:~/test$ file /dev/pts/0 
/dev/pts/0: character special (136/0)     # 字符设备文件
farsight@ubuntu:~/test$ file d2-ln
d2-ln: symbolic link to d2                # 符号链接
farsight@ubuntu:~/test$ file /lib/x86_64-linux-gnu/libc-2.27.so    # 动态库文件
date

显示修改日期时间

farsight@ubuntu:~/test$ date    # 显示日期时间
2023年 07月 14日 星期五 11:09:56 CST
farsight@ubuntu:~/test$ sudo date -s "2023-07-15 12:00:00"; date    # -s 设置日期时间,需要超级权限
[sudo] password for farsight: 
2023年 07月 15日 星期六 12:00:00 CST
2023年 07月 15日 星期六 12:00:00 CST

4.压缩/解压缩

gzip, gunzip
farsight@ubuntu:~/test$ gzip stdio*.h    # 压缩多个文件,每个文件后缀自动加上 .gz,并删除原文件

-rw-rw-r-- 1 farsight farsight 6645 7月  14 11:17  stdio2.h.gz
-rw-rw-r-- 1 farsight farsight 8030 7月  14 11:17  stdio3.h.gz
-rw-r--r-- 1 farsight farsight 6248 7月  14 11:17  stdio.h.gz

farsight@ubuntu:~/test$ gunzip stdio3.h.gz     # 解压
farsight@ubuntu:~/test$ gunzip stdio.h.gz stdio2.h.gz 
bzip2, bunzip2
farsight@ubuntu:~/test$ bzip2 stdio3.h   # 文件后缀自动加上 .bz2,并删除原文件
farsight@ubuntu:~/test$ bzip2 stdio*.h   # 压缩多个文件
farsight@ubuntu:~/test$ bunzip2 stdio*.bz2   # 解压缩

5.tar

 只打包,或先打包再压缩

c 打包
x 解包
j 以 bzip2 工具压缩/解压缩
z 以 gzip 工具压缩/解压缩
J 以 xz 工具压缩/解压缩
f 打包/解包为文件
v 显示打包/解包的信息

farsight@ubuntu:~/test$ tar czfv stdio.tar.gz stdio*.h   # gzip 压缩
farsight@ubuntu:~/test$ tar cjfv stio.tar.bz2 stdio*.h   # bzip2 压缩
farsight@ubuntu:~/test$ tar cJfv stio.tar.xz stdio*.h    # xz 压缩

farsight@ubuntu:~/test$ tar xzf stdio.tar.gz -C d11      # gzip 解压
farsight@ubuntu:~/test$ tar xjf stio.tar.bz2 -C d12      # bzip2 解压
farsight@ubuntu:~/test$ tar xJf stio.tar.xz -C d13       # xz 解压

6.wheris,which

wheris 显示命令的源文件、目标文件、帮助文档

farsight@ubuntu:~/test$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz

which 显示命令的路径

farsight@ubuntu:~/test$ which ls
/bin/ls

7.who, whoami, w

显示系统用户

farsight@ubuntu:~/test$ who
farsight :0           2023-07-14 09:28 (:0)
farsight@ubuntu:~/test$ whoami   # 当前用户
farsight
farsight@ubuntu:~/test$ w        # 查看系统中登录的用户情况
 11:43:01 up  2:33,  1 user,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
farsight :0       :0               09:28   ?xdm?  58.72s  0.00s /usr/lib/gdm3/g

8.time,ps

time

显示进程的花费时间

farsight@ubuntu:~/test$ time ls    # 执行 ls 命令,统计它的进程花费的时间
 1.sh    2.txt     d11   d13   d2-ln      stdio3.h   stdio.tar.gz   stio.tar.xz
 1.txt  '3.txt!'   d12   d2    stdio2.h   stdio.h    stio.tar.bz2

real    0m0.001s    # 实际时间,即进程开始到结束共花费的时间
user    0m0.001s    # 系统在用户空间运行花费的时间
sys    0m0.000s        # 系统运行这个进程在内核空间花费的时间,即系统调用花费的时间

ps

进程状态

farsight@ubuntu:~/test$ ps aux    # 显示所有进程状态
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.5 225500  5120 ?        Ss   09:09   0:02 /sbin/init spl
root          2  0.0  0.0      0     0 ?        S    09:09   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        I<   09:09   0:00 [rcu_gp]
farsight   2680  0.0  0.5  24988  5164 pts/1    Ss+  10:17   0:00 bash
root       3089  0.0  0.0      0     0 ?        I    11:15   0:00 [kworker/u256:
root       3272  0.0  0.0      0     0 ?        I    11:22   0:00 [kworker/u256:
farsight   3398  0.0  0.4  41420  3764 pts/0    R+   11:51   0:00 ps aux

USER 用户
PID  进程号
%CPU 占用 CPU 的百分比
%MEM 占用内存的百分比
TTY  终端,脱离了控制终端的用 ? 号表示

farsight@ubuntu:~/test$ ps axjf    # 查看进程树

9.top

动态查看进程状态,默认每隔 3 秒刷新一次。可以使用 -d 选项更改。

farsight@ubuntu:~/test$ top
top - 14:05:11 up  4:55,  1 user,  load average: 0.00, 0.00, 0.00   # load 是 1分钟,5分钟,15分钟内系统负载
Tasks: 312 total,   2 running, 246 sleeping,   0 stopped,   0 zombie
%Cpu(s):  3.0 us,  1.0 sy,  0.0 ni, 96.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   902772 total,    76448 free,   677192 used,   149132 buff/cache
KiB Swap:   969960 total,   395496 free,   574464 used.    84972 avail Mem 

占用 CPU 比例说明:
           us, user    : time running un-niced user processes   高优先级用户进程
           sy, system  : time running kernel processes          内核进程
           ni, nice    : time running niced user processes      低优先级用户进程
           id, idle    : time spent in the kernel idle handler  内核空闲进程
           wa, IO-wait : time waiting for I/O completion        等待 IO 完成
           hi : time spent servicing hardware interrupts        服务硬件中断
           si : time spent servicing software interrupts        服务软件中断
           st : time stolen from this vm by the hypervisor      系统 VM "偷走"

farsight@ubuntu:~/test$ top -d 5 -u farsight   # 间隔 5 秒刷新,查看 farsight 用户的进程
farsight@ubuntu:~/test$ top -d 5 -u 1000       # 间隔 5 秒刷新,查看 ID 为 1000 的用户的进程
farsight@ubuntu:~/test$ top -d 5 -p 1          # 间隔 5 秒刷新,查看进程号为 1 的进程

10.kill

farsight@ubuntu:~/test$ kill -l    # 显示系统支持的信号
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX

32 以下的信号是不可靠信号,33 以上的信号是可靠信号

farsight@ubuntu:~/test$ kill -9 -p 3720         # 向 3720 进程发送 9 号信号,-9 可以替代为 -KILL,或 -SIGKILL
farsight@ubuntu:~/test$ kill -STOP -p 3724      # 向 3724 进程发送 STOP 暂停信号
farsight@ubuntu:~/test$ kill -SIGCONT -p 3724   # 向 3724 进程发送 SIGCONT 恢复运行信号

获取 a.out 程序的进程号:

farsight@ubuntu:~/test$ ps aux | grep "a.out"

11.mount, umount

挂载,卸载

farsight@ubuntu:~/test$ df   # 查看文件系统
farsight@ubuntu:~/test$ umount /media/farsight/ŷ   # 卸载 U 盘
farsight@ubuntu:~/test$ sudo fdisk -l     # 磁盘格式化、分区命令,-l 列出磁盘及分区
farsight@ubuntu:~/test$ sudo mount -o iocharset=utf8 /dev/sdb1 upan   # 挂载 U 盘到 ~/test/upan 目录
farsight@ubuntu:~/test$ sudo mount -o iocharset=utf8 /dev/sdb1 upan2  # 还可以同时挂载到另一个目录

-o iocharset=utf8 解决查看 U 盘内容时乱码问题

解压文件系统命令:

farsight@ubuntu:~/test$ sudo tar xzf filesystem.tar.gz -C /opt/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值