<code id='0uj5g'><strong id='0uj5g'></strong></code>
      <fieldset id='0uj5g'></fieldset>
    1. <tr id='0uj5g'><strong id='0uj5g'></strong><small id='0uj5g'></small><button id='0uj5g'></button><li id='0uj5g'><noscript id='0uj5g'><big id='0uj5g'></big><dt id='0uj5g'></dt></noscript></li></tr><ol id='0uj5g'><table id='0uj5g'><blockquote id='0uj5g'><tbody id='0uj5g'></tbody></blockquote></table></ol><u id='0uj5g'></u><kbd id='0uj5g'><kbd id='0uj5g'></kbd></kbd>
      1. <acronym id='0uj5g'><em id='0uj5g'></em><td id='0uj5g'><div id='0uj5g'></div></td></acronym><address id='0uj5g'><big id='0uj5g'><big id='0uj5g'></big><legend id='0uj5g'></legend></big></address>

        <span id='0uj5g'></span>
          <dl id='0uj5g'></dl>

          <i id='0uj5g'><div id='0uj5g'><ins id='0uj5g'></ins></div></i>

          <i id='0uj5g'></i>
          <ins id='0uj5g'></ins>

          1. Linux系统中获取路径的文件名的方法

            • 时间:
            • 浏览:5
            • 来源: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'"