Translating OS X Bash Script for Windows

好久不见. 提交于 2020-04-07 01:19:55

问题


I use Hedge to transfer Magic Lantern video files shot on my Canon 5D Mark III.

On OS X, I'm able to use Automator to set up a bash script, to execute an mlv_dump to transfer the files from MLV into cDNG sequences.

The script I use currently is:

cd "$(dirname "$1")"
for f in "$@"; do
    if [[ -f $f ]]; then
        filename=${f##*/};
        folder=${filename%.*}
        mkdir "${folder}";

        ~/mlv_dump --dng $f -o ${folder}/${folder}_;
    fi
done

Can this easily translate into a Windows equivalent?

Thanks,
Thomas


回答1:


As with any translation between programming languages, there's (a) an as-literal-as-possible approach, which contrasts with (b), an not-immediately-obvious-but-in-the-spirit-of-the-target-language approach.
(b) is always preferable in the long run.

Use PowerShell, because it is the - far superior - successor to the "Command Prompt" (cmd.exe) and its batch files.
The code below is an attempt at (b), in PowerShell (v3+ syntax).

I encourage you to study the code and post an explanation of it in an answer of your own, so that others may benefit too.

To help with the analysis, consider the following resources:

  • The official Windows PowerShell get-started page and the equivalent for PowerShell Core.

  • This series of articles is a great, recipe-oriented introduction to PowerShell.

  • http://hyperpolyglot.org/shell juxtaposes the syntax of POSIX-like shells such as bash with that of cmd.exe and PowerShell in concise, tabular form.

PowerShell-idiomatic translation of your code:

param(
  [Parameter(Mandatory, ValueFromRemainingArguments)]
  [System.IO.FileInfo[]] $LiteralPath
)

$outputBaseFolder = Split-Path -Parent $LiteralPath[0].FullName
foreach ($f in $LiteralPath) {
  if ($f.exists) {
    $outputFolder = Join-Path $outputBaseFolder $f.BaseName
    New-Item -ItemType Directory $outputFolder
    & "$HOME/mlv_dump" --dng $f.FullName -o "$outputFolder/$($f.BaseName)_"
  } else {
    Write-Warning "Item doesn't exist or is not a file: $($f.FullName)"
  }
}



回答2:


Easy, is a relative answer specific to the skills of the individual, As the others have commented, there is no out of the box thing / tool you can use or buy to do this. It's all manual work, using the pointers and others who have tried to see what you can glean to accomplish you use case.

For example, this, in your block...

    filename=${f##*/};
    folder=${filename%.*}
    mkdir "${folder}";

... is easily translated into...

    $filename='PathToFileName'
    $foldername='PathToFolder'
    mkdir 'FolderName'

Now, that translation is really simplistic, and obviously not complete. That is something you'll have to figure out, using the resources pointed to and the PowerShell built-in help file and those examples.

There are several posts on this very forum on this conversion topic and how others have come to a consensus oh what / if anything can be done.

For example the below will highlight the efforts you'll need to traverse to accomplish X or Y.

Convert simple Bash script to PowerShell?

I am looking to port this bash code to PowerShell. Can anyone shed some PowerShell light on this one?

Convert simple Bash script to PowerShell?

Convert bash script into Windows script

I have the following Unix shell script. I would like to convert this into a Windows .bat file (I know I can use Cygwin instead of adapting it to a Windows environment. But Cygwin is not an option for me).

Convert bash script into Windows script

Convert xargs Bash command to PowerShell?

I've got a simple Bash command to resize some images automatically on a low-traffic website using ImageMagick - I'd like to convert this to a PowerShell command so I don't have to install Cygwin on my webserver.

Convert xargs Bash command to PowerShell?

As noted, there are paid for avenues / even online ones who can potentially be an avenue.

But, you should really read up a bit more on how to do specific things, like..

  • Creating files and folders
  • Accessing/Reading files, importing files, filtering files
  • Loops
  • Operators (comparison and assignment)
  • Navigating the file system/PSDrive


来源:https://stackoverflow.com/questions/48490407/translating-os-x-bash-script-for-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!