首页 \ 问答 \ Bash:检索给定相对的绝对路径(Bash: retrieve absolute path given relative)

Bash:检索给定相对的绝对路径(Bash: retrieve absolute path given relative)

有没有一个命令来检索给定相对路径的绝对路径?

例如,我想要$ line包含dir ./etc/中每个文件的绝对路径

find ./ -type f | while read line; do
   echo $line
done

Is there a command to retrieve the absolute path given the relative path?

For example I want $line to contain the absolute path of each file in dir ./etc/

find ./ -type f | while read line; do
   echo $line
done

原文:https://stackoverflow.com/questions/4175264
更新时间:2023-07-22 12:07

最满意答案

这个问题的答案可以在Microsoft.Common.targets文件中找到,可以找到(取决于你使用的是64位或32位的框架): C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.target为64位, C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets为32位运行时。 此文件定义了项目构建所遇到的所有步骤。 引用来源:

<!--
============================================================
                                    Build

The main build entry point.
============================================================
-->
<PropertyGroup>
    <BuildDependsOn>
        BeforeBuild;
        CoreBuild;
        AfterBuild
    </BuildDependsOn>
</PropertyGroup>

该代码很好地解释了在两个目标的注释中使用BeforeBuildAfterBuild目标。

<!--
============================================================
                                    BeforeBuild

Redefine this target in your project in order to run tasks just before Build
============================================================
-->
<Target Name="BeforeBuild"/>

<!--
============================================================
                                    AfterBuild

Redefine this target in your project in order to run tasks just after Build
============================================================
-->
<Target Name="AfterBuild"/>

之后是CoreBuild目标的定义:

<PropertyGroup>
    <CoreBuildDependsOn>
        BuildOnlySettings;
        PrepareForBuild;
        PreBuildEvent;
        ResolveReferences;
        PrepareResources;
        ResolveKeySource;
        Compile;
        UnmanagedUnregistration;
        GenerateSerializationAssemblies;
        CreateSatelliteAssemblies;
        GenerateManifests;
        GetTargetPath;
        PrepareForRun;
        UnmanagedRegistration;
        IncrementalClean;
        PostBuildEvent
    </CoreBuildDependsOn>
</PropertyGroup>

因此, Build目标只是CoreBuild目标下的一个包装器,可让您在CoreBuild目标之前或之后执行自定义步骤。 如上所述, PreBuildEventPostBuildEvent被列为CoreBuild目标的依赖关系。 Compile目标的依赖关系定义如下:

<PropertyGroup>
    <CompileDependsOn>
        ResolveReferences;
        ResolveKeySource;
        SetWin32ManifestProperties;
        _GenerateCompileInputs;
        BeforeCompile;
        _TimeStampBeforeCompile;
        CoreCompile;
        _TimeStampAfterCompile;
        AfterCompile
    </CompileDependsOn>
</PropertyGroup>

在代码中注释了BeforeCompileAfterCompile

<!--
============================================================
                                    BeforeCompile

Redefine this target in your project in order to run tasks just before Compile.
============================================================
-->
<Target Name="BeforeCompile"/>

<!--
============================================================
                                    AfterCompile

Redefine this target in your project in order to run tasks just after Compile.
============================================================
-->
<Target Name="AfterCompile"/>

鉴于这些信息,我不知道为什么AppHarbor不支持Pre-, PostBuildEventBuild可以使用Before-, AfterBuild进行修改。

选择要替换哪个Target取决于您希望执行给定任务的构建过程中的那一刻。 目标没有具体的限制和/或好处,他们可以完成什么。 除了它们可以适应以前步骤定义/填充的ItemGroup或属性的事实。

在构建尝试解决项目依赖关系之前,使用nuget引入包可能最好执行。 所以BeforeCompile不是这种行动的好选择。

我希望这件事可以说明一些事情。 在MSDN上找到了另一个很好的解释


The answer to this question can be found in the Microsoft.Common.targets file which can be found (depending on wether you're using the 64-bit or 32-bit framework) at: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.target for 64-bit and C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets for the 32-bit runtime. This file defines all the steps a build of your project undergoes. Quoting the source:

<!--
============================================================
                                    Build

The main build entry point.
============================================================
-->
<PropertyGroup>
    <BuildDependsOn>
        BeforeBuild;
        CoreBuild;
        AfterBuild
    </BuildDependsOn>
</PropertyGroup>

The code is nice enough to explain the use of the BeforeBuild and AfterBuild target in the comments for both targets.

<!--
============================================================
                                    BeforeBuild

Redefine this target in your project in order to run tasks just before Build
============================================================
-->
<Target Name="BeforeBuild"/>

<!--
============================================================
                                    AfterBuild

Redefine this target in your project in order to run tasks just after Build
============================================================
-->
<Target Name="AfterBuild"/>

This is followed by the definition of the CoreBuild target:

<PropertyGroup>
    <CoreBuildDependsOn>
        BuildOnlySettings;
        PrepareForBuild;
        PreBuildEvent;
        ResolveReferences;
        PrepareResources;
        ResolveKeySource;
        Compile;
        UnmanagedUnregistration;
        GenerateSerializationAssemblies;
        CreateSatelliteAssemblies;
        GenerateManifests;
        GetTargetPath;
        PrepareForRun;
        UnmanagedRegistration;
        IncrementalClean;
        PostBuildEvent
    </CoreBuildDependsOn>
</PropertyGroup>

So the Build target is just a wrapper around the CoreBuild target to enable you to perform custom steps just before or after the CoreBuild target. As can be seen above the PreBuildEvent and PostBuildEvent are listed as dependencies of the CoreBuild target. The dependencies of the Compile target are defined as follows:

<PropertyGroup>
    <CompileDependsOn>
        ResolveReferences;
        ResolveKeySource;
        SetWin32ManifestProperties;
        _GenerateCompileInputs;
        BeforeCompile;
        _TimeStampBeforeCompile;
        CoreCompile;
        _TimeStampAfterCompile;
        AfterCompile
    </CompileDependsOn>
</PropertyGroup>

Again BeforeCompile and AfterCompile are commented in the code:

<!--
============================================================
                                    BeforeCompile

Redefine this target in your project in order to run tasks just before Compile.
============================================================
-->
<Target Name="BeforeCompile"/>

<!--
============================================================
                                    AfterCompile

Redefine this target in your project in order to run tasks just after Compile.
============================================================
-->
<Target Name="AfterCompile"/>

Given this information I do not know why AppHarbor does not support Pre-, PostBuildEvent while the Build can be modified using Before-, AfterBuild.

Choosing which Target to override for which scenario depends on the moment during the build at which you wish to perform your given task. The targets do not have specific restrictions and/or benefits as to what they can accomplish. Apart from the fact that they can adapt ItemGroup's or properties that were defined/filled by previous steps.

Using nuget to bring in packages is probably best performed before the build tries to resolve the projects dependencies. So BeforeCompile is not a good candidate for this kind of action.

I hope this sheds some light on the matter. Found another nice explanation on MSDN

相关问答

更多