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)就应运而生啦!

  • 相关阅读:
    暑假学习日记2013/7/20
    java二维码之利用谷歌的zxing生成二维码,解析二维码
    数组去重
    MVC VS2012 Code First 数据库迁移教程
    WIN8 MTK驱动不能安装解决办法
    洛谷 P1943 LocalMaxima_NOI导刊2009提高(1)
    BZOJ 1572 USACO 2009 Open 工作安排
    BZOJ 1724 USACO 2006 Nov. 切割木板
    BZOJ 1666 USACO 2006 Oct. 奶牛的数字游戏
    BZOJ 4094 USACO 2013 Dec. Optimal Milking
  • 原文地址:https://www.cnblogs.com/xuanbjut/p/14218239.html
Copyright © 2011-2022 走看看