zoukankan      html  css  js  c++  java
  • dirname

    shell中dirname和pwd的笔记及用法

    1.dirname

    输入

    dirname是一种shell命令,参数只能有一个,可以是任意字符串(";"除外),这里要注意,dirname不会检查参数是不是文件或目录。比如:

    [root@xx /]# dirname .;
    [root@xx /]# dirname !;
    [root@xx /]# dirname c.txt;
    [root@xx /]# dirname a/b/c.txt;
    [root@xx /]# dirname a/b/c;
    [root@xx /]# dirname a/b/c/;
    [root@xx /]# dirname a/b/c/ ;
    [root@xx /]# dirname a/b/c/.;
    [root@xx /]# dirname a/b/c\d;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    输出

    感受一下

    [root@xx /]# .
    [root@xx /]# .
    [root@xx /]# .
    [root@xx /]# a/b
    [root@xx /]# a/b
    [root@xx /]# a/b
    [root@xx /]# a/b
    [root@xx /]# a/b/c
    [root@xx /]# a/b
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    特点

    我猜dirname处理输入的字符串(";“作为命令截止符不会处理)时,是从字符串的末尾往前处理,并将末尾到第一个”/“之间的所有字符(包括”/“字符)全部删除,然后返回剩余的字符;如果没有遇到”/“字符,那就返回”."。

    2.pwd

    输入

    pwd是不需要任何参数的,但如果非要加上参数也是能执行的,但参数会表示自己没有任何存在感,如:

    [root@xx /]# pwd
    [root@xx /]# pwd a
    [root@xx /]# pwd a b
    
    • 1
    • 2
    • 3

    输出

    感受一下

    [root@xx /]# /
    [root@xx /]# /
    [root@xx /]# /
    
    • 1
    • 2
    • 3

    特点

    特点就是简单,返回当前目录或者工作目录的绝对路径。这里有个需要注意的地方,假设目录/home/a/b/c下有个c.sh脚本,里面实现的是打印pwd命令的结果,而/home/a/b下有个b.sh脚本,里面实现的是调用c.sh脚本,那么打印的结果是/home/a/b

    3.总结

    在脚本文件中,pwd直接获取当前脚本文件对应的绝对路径时是有风险的,因为当其他脚本文件调用你时,pwd就不是你所在的绝对目录了,所以我们的$(cd “$(dirname $0)”; pwd)就应运而生啦!

  • 相关阅读:
    easyui-filebox上传图片到阿里
    easyUI-filebox图片上传和预览
    抓网页__第3方库选择_01
    HttpClient示例01
    JSON01_资料
    指定library路径
    Jni_Linux_01_转
    JNI简单步骤01
    JDK_环境变量
    Redis_01
  • 原文地址:https://www.cnblogs.com/xuanbjut/p/14218239.html
Copyright © 2011-2022 走看看