none
Tips on changing a bash shell script to something that'll run on Windows Server 2012 RRS feed

  • Question

  • Not sure if anyone here is familiar with both Linux scripting and batch/vbscript/something for Windows, but I'm having a hard time trying to figure out how to convert commands like this into something that will work in Windows.   Anyone have any ideas?

    ls -tr /my/path/ToSaveInto/*.png | tail -1

    /usr/bin/find $path -daystart -mtime +31 -type f -exec rm {} \;

    Wednesday, April 13, 2016 3:32 PM

Answers

  • So you would need to find software that works similarly on the Windows side - a tool that can "convert" a png into a pdf file from the command line. Windows doesn't have a tool like that built-in. You would need to research that part yourself.

    PowerShell can easily find files and remove files with old timestamps. Here's an example of that:


    Get-ChildItem "D:\Path\*.png" |
      Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } |
      Remove-Item -WhatIf
    



    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 13, 2016 5:10 PM
    Moderator

All replies

  • You'd be better off explaining what you actually want to do instead of pasting code.

    Wednesday, April 13, 2016 3:45 PM
  • Sorry, let me break down what i'm trying to do.

    Run command line #1, with a few switches to it, one being a file name that would have to have the timestamp of when it ran appended to the filename (for instance 20160312230001 for 3/12/16 11pm, so the file would be named capture-20160312230001.png)

    Run command line #2, which runs a command against that PNG created from command line #1 (I used that ls thing to find the latest png in the folder but i guess you don't need to if you can just pass the file directly), to make it a PDF in the same folder as the PNG.

    Delete the PNG.

    then scan the folder and delete any PDFs older than 31 days

    I have all the executables and switches I need, it's just scripting it somehow so i can make it a scheduled task where I'm obviously clueless here on the Windows side.  and nice signature BTW!

    Wednesday, April 13, 2016 3:57 PM
  • Nothing in your bash commands contains a conversion command that would convert a png into a pdf file.

    Also, if this is working on Linux, why do you need it on Windows?


    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 13, 2016 4:09 PM
    Moderator
  • Correct.  here's the whole script that contains all the commands you were looking for.  I have all the commands' Windows version downloaded and installed on my Windows box.  It's just I'm not sure how to put it all together.

    #!/bin/bash
    cdate=`date +%Y%m%d%H%M%S`
    site=http://MySite.com
    filename=MySite.com-$cdate.png
    path=/my/path/ToSaveInto/
    /usr/bin/phantomjs /home/myUser/rasterize.js $site $path$filename
    mogrify -format pdf "`ls -tr /my/path/ToSaveInto/*.png | tail -1`"
    rm -f "`ls -tr /my/path/ToSaveInto/*.png | tail -1`"
    /usr/bin/find $path -daystart -mtime +31 -type f -exec rm {} \;

    And to answer your other question, because the server's hardware is dying, and hoping I can move all the processing to my brand new Windows box rather than having to spin up a brand new Linux box just for this.

    Wednesday, April 13, 2016 4:13 PM
  • So you would need to find software that works similarly on the Windows side - a tool that can "convert" a png into a pdf file from the command line. Windows doesn't have a tool like that built-in. You would need to research that part yourself.

    PowerShell can easily find files and remove files with old timestamps. Here's an example of that:


    Get-ChildItem "D:\Path\*.png" |
      Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } |
      Remove-Item -WhatIf
    



    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 13, 2016 5:10 PM
    Moderator
  • Hi Bill.  I lucked out in that there are Windows versions of all the Linux programs I was using for the converting, so I have those.

    It's more the finding and removing files at the end, and the doing of the filenames dynamically parts where I'm at a loss.

    Thanks for the PowerShell example for the find/remove part!

    Wednesday, April 13, 2016 5:23 PM
  • So do you still have a question then?

    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 13, 2016 5:26 PM
    Moderator
  • I'll try seeing what I can find on the web as an example for the filename stuff based on current time.  So unless someone else replies regarding that, I think i'm in better shape.
    Wednesday, April 13, 2016 5:28 PM
  • Here is an example for creating filename:

    $domain   = 'MySite.com'
    $datetime = (Get-Date -format yyyyMMddHHmmss)
    $filename = '{0}-{1}.png' -f $domain, $datetime


    Wednesday, April 13, 2016 5:34 PM
  • Thanks Leif!
    Wednesday, April 13, 2016 5:44 PM
  • I'll try seeing what I can find on the web as an example for the filename stuff based on current time.

    Leif-Arne's example should get you what you're after. This might also be helpful:

    http://ss64.com/ps/syntax-dateformats.html

    and nice signature BTW!

    Thanks. It's clickable too, so make sure you click and enjoy the rest of the day with some tunes.

    =]


    Wednesday, April 13, 2016 5:46 PM
  • Thanks for the link, and the tunes!

    Have a good one guys, really appreciate all the guidance.

    Wednesday, April 13, 2016 5:50 PM
  • Here's a little bit shorter version:


    $filename = "mysite.com-{0:yyyyMMddHHmmss}.png" -f (Get-Date)
    


    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 13, 2016 6:06 PM
    Moderator
  • cool, thanks.
    Wednesday, April 13, 2016 6:07 PM
  • Everything appears to work as expected in powershell, now i just have to figure out how to schedule a ps1, and I even figured out to remove the -WhatIf to get it to actually delete haha.

    Thanks again for the help everybody.

    Wednesday, April 13, 2016 7:30 PM