AnswerBun.com

Capture output of powershell command in makefile on windows

I’m trying to create a Makefile that will work on both windows and unix-based operating systems. (For reference, the windows version of make it should work with can be found here). As part of the Makefile, I need to be able to split a path (contained in an environment variable) to get just the filename (with extension). I use the command "basename" for non-windows OS’s, and am currently trying to use the Powershell command "Split-Path" to get the file name from the path on Windows. Running this command in the command line or in a make recipe gives the desired output. However, I can’t get it to work so that I can capture the results of the Powershell command within the Makefile to a variable. My current setup in the Makefile to detect the operating system and get the file from the path can be seen below:

ifeq ($(OS),Windows_NT)
    # Windows
    # The following line doesn't capture the output as expected
    FILENAME_I_WANT:=$(powershell -noprofile Split-Path $(PATH_ENVIRONMENT_VARIABLE) -leaf)
else
    # Unix based OS
    FILENAME_I_WANT:=$(shell basename $(PATH_ENVIRONMENT_VARIABLE))
endif

Does anyone see anything I’m doing wrong, or have any suggestions for how to fix this?

Thanks!

Super User Asked by qwertycomputer on December 26, 2020

1 Answers

One Answer

Why are you not using the environment variables for OS, and just dot referencing the basename of the filename?

# Get the host OS
$env:OS
# Results
<#
Windows_NT
#>

# Get the target 5th in the folder and only show the basename
((Get-ChildItem -Path 'D:Temp')[5]).BaseName
# Results
<#
IEDriverServer_Win32_3.150.1
#>

Answered by postanote on December 26, 2020

Add your own answers!

Related Questions

Creating a symbolic link to mapped network drive in Windows

8  Asked on December 31, 2020 by vegard-larsen

   

Bash files open terminal when opened

1  Asked on December 31, 2020 by chris-smith

   

S3 command not working in cron

1  Asked on December 31, 2020 by kargirwar

       

Scale to fit option

6  Asked on December 30, 2020 by steven-penny

 

After downloading Tweepy I don’t know where to locate it

1  Asked on December 30, 2020 by candle

 

Excel COUNTIF with non-contiguous named ranges

1  Asked on December 30, 2020 by malan88

 

How to bypass Windows 10 PIN complexity requirement

1  Asked on December 29, 2020 by some-user

   

Resume uploading with built-in Windows FTP client

1  Asked on December 29, 2020 by vlad-novakovsky

   

What is the Windows analog of the Linux watch command?

15  Asked on December 29, 2020 by peterjclaw

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.