linux知识点小结

  1. PATH环境变量,记录了所有可以直接执行的二进制命令的原件或者链接
    harvey@ubuntu:/etc$ echo $PATH
    /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

    PATH这个作用域是可执行的二进制命令:which---搜索整个PATH路径,查找当前搜索的命令是否存在,或具体是那个路径下的文件(如find) ; type---有些命令虽然可以执行但是which搜索不到命令的位置,可以使用type查看是否是内置命令(如cd);whereis---可以搜索所有的包含的二进制文件的文件、文件夹和man手册,如果这里搜索不到man位置,那么肯定就不存在指定的man手册。

    harvey@ubuntu:/etc$ which find #用which所有PATH找到位置
    /usr/bin/find
    harvey@ubuntu:/etc$ which cd #内置命令没有返回结果
    harvey@ubuntu:/etc$ type cd  #type发现是内嵌命令
    cd 是 shell 内嵌
    harvey@ubuntu:/etc$ type find #type也有which的效果,但是关键还透露是被哈希
    find 已被哈希 (/usr/bin/find)
    harvey@ubuntu:/etc$ whereis cd #whereis没有找到cd的man压缩文件
    cd:
    harvey@ubuntu:/etc$ man cd #所以man手册提示没有页目
    没有 cd 的手册页条目
    harvey@ubuntu:/etc$ man find
    harvey@ubuntu:/etc$ whereis find #找到find位置和man压缩文件
    find: /usr/bin/find /usr/bin/X11/find /usr/share/man/man1/find.1.gz
    harvey@ubuntu:/etc$ whereis init #找到所有有init二进制的文件 文件夹和man压缩文件
    init: /usr/src/linux-headers-3.11.0-15/init /usr/src/linux-headers-3.11.0-15-generic/init /sbin/init /etc/init.d /etc/init /lib/init /usr/share/man/man8/init.8.gz /usr/share/man/man5/init.5.gz
  2. which type wheris都是和二进制的执行命令相关,还有两个全能的查找命令。find---查找磁盘 location,locate--所有/var/lib/locatedb数据库
  3. 在计算机一起可以运行的数据都是在进程中的,进程都是有进程号码的,怎么查看当前进程的号?
     1 #shell脚本的特殊变量
     2 #!/bin/bash
     3 #写脚本时候经常使用额变量研究
     4 #参考资料:百度空间
     5 #作者:骞文 QQ:1402115331
     6 echo "当前文件的文件名是$0";
     7 echo "当前shell脚本的第1个参数是$1";
     8 echo "当前shell脚本额所有参数为$*";
     9 echo "当前shell脚本的所有参数的个数是$#";
    10 echo "当前shell脚本的进程号码$$";
    11 echo "执行上一个指令的进程号$!";
    12 echo "执行的上衣个指令的返回值$? 返回值为0表示成功否则为失败";
    13 echo "可以用$$和$!比较上一个指令和当前指令是否在一个进程里!" 

    直接在Terminal中使用:可以直接echo 当前用户的/bin/bash/PID

  4. exec命令的作用是以新的进程替换原来的进程,只是替换上下文并不替换PID
     1 harvey@ubuntu:/$ echo $$ #查看当前用户的PID
     2 3115
     3 harvey@ubuntu:/$ content="This is the old Content"; #给3115一个上下文content
     4 harvey@ubuntu:/$ echo $content
     5 This is the old Content
     6 harvey@ubuntu:/$ echo $$
     7 3115
     8 harvey@ubuntu:/$ bash #直接执行bash命令
     9 harvey@ubuntu:/$ echo $$ #发现PID变了说明换了新的子进程
    10 3395
    11 harvey@ubuntu:/$ echo $content #新的进程没有3115的上下文
    12 
    13 harvey@ubuntu:/$ content="This is the new content";#重新给3395一个上下文
    14 harvey@ubuntu:/$ echo $content
    15 This is the new content
    16 harvey@ubuntu:/$ echo $$
    17 3395
    18 harvey@ubuntu:/$ exec bash #执行exec bash命令
    19 harvey@ubuntu:/$ echo $$ #发现这次pid没有变
    20 3395
    21 harvey@ubuntu:/$ echo $content #但是新的上下文被清空了
    22 
    23 harvey@ubuntu:/$ 
    24 
    25 #结论:直接执行bash会产生新的进程(新的PID新的上下文)
    26       执行exec bash产生新的上下文(旧的PID新的上下文)
  5. /proc/${PID}---当前进程的所有信息(包括进程权限,环境变量等)
    harvey@harvey-PC ~
    $ cd /proc/$$
    #切换到进程信息目录
    harvey@harvey-PC /proc/4604
    $ ll
    总用量 0
    -r--r--r-- 1 harvey None 0 三月 17 11:36 cmdline
    -r--r--r-- 1 harvey None 0 三月 17 11:36 ctty
    lrwxrwxrwx 1 harvey None 0 三月 17 11:36 cwd -> /proc/4604
    lrwxrwxrwx 1 harvey None 0 三月 17 11:36 exe -> /usr/bin/bash
    -r--r--r-- 1 harvey None 0 三月 17 11:36 exename
    dr-xr-xr-x 2 harvey None 0 三月 17 11:32 fd
    -r--r--r-- 1 harvey None 0 三月 17 11:36 gid
    -r--r--r-- 1 harvey None 0 三月 17 11:36 maps
    -r--r--r-- 1 harvey None 0 三月 17 11:36 mountinfo
    -r--r--r-- 1 harvey None 0 三月 17 11:36 mounts
    -r--r--r-- 1 harvey None 0 三月 17 11:36 pgid
    -r--r--r-- 1 harvey None 0 三月 17 11:36 ppid
    lrwxrwxrwx 1 harvey None 0 三月 17 11:36 root -> /
    -r--r--r-- 1 harvey None 0 三月 17 11:36 sid
    -r--r--r-- 1 harvey None 0 三月 17 11:36 stat
    -r--r--r-- 1 harvey None 0 三月 17 11:36 statm
    -r--r--r-- 1 harvey None 0 三月 17 11:36 status
    -r--r--r-- 1 harvey None 0 三月 17 11:36 uid
    -r--r--r-- 1 harvey None 0 三月 17 11:36 winexename
    -r--r--r-- 1 harvey None 0 三月 17 11:36 winpid
    #查看进行信息文件
  6. /proc/self/fd/---文件描述符列表

    harvey@harvey-PC ~
    $ ll /proc/self/fd
    总用量 0
    lrwxrwxrwx 1 harvey None 0 三月 17 11:46 0 -> /dev/pty0
    lrwxrwxrwx 1 harvey None 0 三月 17 11:46 1 -> /dev/pty0
    lrwxrwxrwx 1 harvey None 0 三月 17 11:46 2 -> /dev/pty0
    lrwxrwxrwx 1 harvey None 0 三月 17 11:46 3 -> /proc/3896/fd
    #查看默认的全部文件操作符
    harvey@harvey-PC ~
    $ exec 4<~/test.log
    #为读取文件创造一个文件描述符
    #可以为截断模式写入文件创造一个文件描述符 5>~/test.log
    # 为追加模式写入文件创造一个文件描述符 6>>~/test.log
    #参考资料<LINUX SHELL脚本攻略>
    harvey@harvey
    -PC ~ $ cat <&4 这是一个测试文件 #&4表示的是一个文件,,所以用cat命令 harvey@harvey-PC ~ $ ll /proc/self/fd 总用量 0 lrwxrwxrwx 1 harvey None 0 三月 17 11:47 0 -> /dev/pty0 lrwxrwxrwx 1 harvey None 0 三月 17 11:47 1 -> /dev/pty0 lrwxrwxrwx 1 harvey None 0 三月 17 11:47 2 -> /dev/pty0 lrwxrwxrwx 1 harvey None 0 三月 17 11:47 3 -> /proc/6652/fd lrwxrwxrwx 1 harvey None 0 三月 17 11:47 4 -> /home/harvey/test.log #常看文件操作符列表
  7. shell脚本的执行方式
    1.用sh命令执行
    sh ./shelltest.sh
    
    2.直接运行
    chmod u=rwx ./shelltest.sh#先给创建者运行权限
    ./shelltest.sh #运行

    #第二种方式运行的时候一定要给x权限,否则会提示一下错误:-bash: ./foreach.sh: Permission denied

参考资料:Linux的五个查找命令:find,locate,whereis,which,type 

 linux shell 特殊变量----轻松获取系统信息

shell中exec解析

 

                                                                                                                                        参考图书:LINUX SHELL脚本攻略

 

原文地址:https://www.cnblogs.com/zhanghaiyublog/p/3599991.html