<i id='hhqd9'><div id='hhqd9'><ins id='hhqd9'></ins></div></i>

  • <span id='hhqd9'></span>

    <dl id='hhqd9'></dl>
    <acronym id='hhqd9'><em id='hhqd9'></em><td id='hhqd9'><div id='hhqd9'></div></td></acronym><address id='hhqd9'><big id='hhqd9'><big id='hhqd9'></big><legend id='hhqd9'></legend></big></address>
    <fieldset id='hhqd9'></fieldset>
    <i id='hhqd9'></i>

    <code id='hhqd9'><strong id='hhqd9'></strong></code>

    <ins id='hhqd9'></ins>

          1. <tr id='hhqd9'><strong id='hhqd9'></strong><small id='hhqd9'></small><button id='hhqd9'></button><li id='hhqd9'><noscript id='hhqd9'><big id='hhqd9'></big><dt id='hhqd9'></dt></noscript></li></tr><ol id='hhqd9'><table id='hhqd9'><blockquote id='hhqd9'><tbody id='hhqd9'></tbody></blockquote></table></ol><u id='hhqd9'></u><kbd id='hhqd9'><kbd id='hhqd9'></kbd></kbd>
          2. Linux系统中获取路径的文件名的方法

            • 时间:
            • 浏览:13
            • 来源:124软件资讯网

                这篇文章主要先容了Linux系统中获取路径的文件名的要领,文中总结出了两条,需要的朋侪可以参考下

                代码如下:

                [root@dabu.info ]#basename /root/aaa/bbb/dabu.txt

                显示:

                代码如下:

                dabu.txt #获取路径的文件名

                shell剧本中怎样获得剧本文件所在路径?

                要领一:

                代码如下:

                [root@dabu.info ]#DIR=$(cd "$(dirname "$0")"; pwd)

              [root@dabu.info ]#echo $DIR

                可是像这种dirname "$0"这种写法  ,在遇到source下令时会获得错误的效果  。

                要领二:

                代码如下:

                [root@dabu.info ]#echo "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

                上面一行下令可以获得剧本的绝对轮径 ,无论你在那边挪用这个剧本  。

                可是若是含有软链接  ,就无法使用了  。以是  ,我们为了能准确剖析指向剧本的软链接  ,可以使用下面的多行下令:

                代码如下:

                SOURCE="${BASH_SOURCE[0]}"

                while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink

                DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

                SOURCE="$(readlink "$SOURCE")"

                [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located

                done

                DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

                也可与source,bash -c下令使用

                可是  ,若是你在剧本中使用先cd切换到其他目录  ,在运行时上面的下令片断时 ,则上面的下令不能等到准确的效果  。可以参考关于$CDPATH 陷阱的文章  。想明白它怎样其作用的  ,可以运行下面的代码:

                代码如下:

                #!/bin/bash

              SOURCE="${BASH_SOURCE[0]}"

                while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink

                TARGET="$(readlink "$SOURCE")"

                if [[ $SOURCE == /* ]]; then

                echo "SOURCE '$SOURCE' is an absolute symlink to '$TARGET'"

                SOURCE="$TARGET"

                else

                DIR="$( dirname "$SOURCE" )"

                echo "SOURCE '$SOURCE' is a relative symlink to '$TARGET' (relative to '$DIR')"

                SOURCE="$DIR/$TARGET" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located

                fi

                done

                echo "SOURCE is '$SOURCE'"

                RDIR="$( dirname "$SOURCE" )"

                DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

                if [ "$DIR" != "$RDIR" ]; then

                echo "DIR '$RDIR' resolves to '$DIR'"

                fi

                echo "DIR is '$DIR'"